2017-06-04 8 views
0

RecyclerViewリストを画面に集中させようとしています。単一のリストになるので、LinearLayoutManager(LLM)を使用します。 しかしその項目を一元化ないLLMを使用して、そしてそれがランドスケープモードで次のようになります。 はLandscape mode with LLMLinearLayoutManagerを使用したRecyclerViewの集中化

XMLコードは次のようになります。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" android:layout_width="match_parent" 

    android:layout_height="match_parent"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/recipe_card" 
     android:layout_gravity="center" 
     android:layout_width="400dp" 
     android:layout_height="186dp" 
     style="@style/CardViewStyle"> 

     <android.support.constraint.ConstraintLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 


      <ImageView 
       android:id="@+id/recipe_image" 
       android:layout_width="120dp" 
       android:layout_height="163dp" 
       app:srcCompat="@mipmap/ic_pan" 
       app:layout_constraintTop_toTopOf="parent" 
       android:layout_marginTop="8dp" 
       app:layout_constraintBottom_toBottomOf="parent" 
       android:layout_marginBottom="8dp" 
       android:layout_marginLeft="8dp" 
       app:layout_constraintLeft_toLeftOf="parent" 
       tools:layout_editor_absoluteY="3dp" /> 

      <TextView 
       android:id="@+id/recipe_name" 
       android:layout_width="250dp" 
       android:layout_height="119dp" 
       android:text="TextView" 
       app:layout_constraintLeft_toRightOf="@+id/recipe_image" 
       android:layout_marginLeft="8dp" 
       app:layout_constraintTop_toTopOf="parent" 
       android:layout_marginTop="16dp" /> 

      <TextView 
       android:id="@+id/recipe_servings" 
       android:layout_width="164dp" 
       android:layout_height="32dp" 
       android:text="TextView" 
       android:layout_marginTop="8dp" 
       app:layout_constraintTop_toBottomOf="@+id/recipe_name" 
       app:layout_constraintBottom_toBottomOf="parent" 
       android:layout_marginBottom="8dp" 
       app:layout_constraintLeft_toRightOf="@+id/recipe_image" 
       android:layout_marginLeft="94dp" 
       app:layout_constraintVertical_bias="0.0" /> 
     </android.support.constraint.ConstraintLayout> 

    </android.support.v7.widget.CardView> 

</FrameLayout> 

のAndroid Studioのプレビューでは、それが正常に見えますしかし、アプリでは、それはしません。 私はのLLMを1列だけ変更することで修正できます。しかし、それは醜いように見えます。

誰が何が起こっているのか分かりませんか?おかげさまで

+0

デビッドでこのコードは、ダビデが私の答えは非常にどこでもあなたは、あなたがそれをチェックアウトした中でのuを助けることができる私の答え – scienticious

+0

をチェック! – pouya

答えて

0

メインレイアウトの内容重心を中央に設定してください。

  1. "centerHorozontal"プロパティをフレームレイアウトに追加します。
  2. フレームレイアウトを線形レイアウトに変更し、メイン線形レイアウトの重力を「中心」に設定します。

あなたのレイアウトで使用している画像を私に送信しないとうまくいけば、私はいくつかの他のテクニックを使って修正しようとします。

0

、そして、相対レイアウトで

を制約レイアウトを交換した画像は、中央可能にする、この

<ImageView 
     android:id="@+id/image" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:clickable="true" 
     android:scaleType="centerCrop" /> 

が続いても、これらのテキストの各中央

android:textAlignment="center" 
0

もmakeを使用あなたのを確認してくださいpublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)、あなたがそのように書くなら:

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, null)); 
} 

これにより、FrameLayoutが無効になり、CardViewがViewHolderのitemViewになるので、次のように変更する必要があります。

0

私はあなたが私のディスプレイサイズの幅を得て、それに応じて私のViewHolderを設定します。

これは私のアプリケーションクラスでは私のコードです:

public class MyApp extends Application { 
public static Point displaySize = new Point(); 
public static float density = 1; 
public static Context appContext = null; 
public static int height; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    ActiveAndroid.initialize(this); 
    appContext = getApplicationContext(); 
    checkDisplaySize(); 
    density = appContext.getResources().getDisplayMetrics().density; 
} 

public static void checkDisplaySize() { 
    try { 
     WindowManager manager = (WindowManager) 
appContext.getSystemService(Context.WINDOW_SERVICE); 
     if (manager != null) { 
      Display display = manager.getDefaultDisplay(); 
      if (display != null) { 
        display.getSize(displaySize); 
      } 
      height = (int) (displaySize.x/1.777f); //Aspect Ratio 16:9 
     } 
    } catch (Exception e) { 
    } 
} 
} 

とあなただけの次の手順を実行ホルダービューを取得する場所すべてこの後、あなたのアダプタで:

int marginWidth = (MyApp.desplaySize.x - itemView.getMeasuredWidth())/2; 
LayoutParams params = new LayoutParams(
    LayoutParams.WRAP_CONTENT,  
    LayoutParams.WRAP_CONTENT 
); 
params.setMargins(marginWidth, 0, marginWidth, 0); 
itemView.setLayoutParams(params); 

私は本当にわかりませんこの正確なコードはあなたの問題を解決しますが、簡単にこれを変更することができます。

0

書き込みあなたでframeLayout

android:layout_gravity="center" 
関連する問題