2017-06-04 101 views
0

我想在屏幕上集中RecyclerView列表。它將成爲一個單子,所以我將使用LinearLayoutManager(LLM)。 但是使用其不集中的項目的LLM,和它看起來像這樣的風景模式: Landscape mode with LLM使用LinearLayoutManager集中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的預覽,它看起來不錯,但在應用程序,它不。 我設法通過只更改一列GridLayoutManager的LLM來修復它。但它看起來很醜。

有誰知道發生了什麼事? 謝謝。

+0

大衛檢查我的回答 – scienticious

+0

大衛·我的回答能幫助ü大大無處不在你檢查一下嗎? – pouya

回答

0

嘗試將主佈局的內容重心設置爲居中。有兩種方法可以做到這一點:

  1. 將「centerHorozontal」屬性添加到您的框架佈局的「true」。
  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

請檢查您的如果你這樣寫:

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

這將使您的FrameLayout被禁用,CardView將成爲您ViewHolder的itemView,所以您應該改變如下: @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, parent, false)); } 希望幫助你。

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" 
相關問題