簡體   English   中英

使用LinearLayoutManager集中化RecyclerView

[英]Centralizing RecyclerView with LinearLayoutManager

我正在嘗試將RecyclerView列表集中在屏幕中。 它將是一個列表,因此我將使用LinearLayoutManager (LLM)。 但是,使用LLM不會將項目集中,並且在橫向模式下看起來像這樣: LLM橫向模式

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來解決它。 但是看起來很丑。

有人知道發生了什么嗎? 謝謝。

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

  1. 將“ centerHorozontal”屬性添加為“ true”到您的框架布局。
  2. 將框架布局更改為線性布局,並將主線性布局的重力設置為“中心”。

希望它能工作,如果不能,那么只給我發送您在布局中使用的圖像,我將嘗試使用其他技術來修復它,並向您發送更新的代碼。

用相對布局替換約束布局

然后,使圖像居中,使用此

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

然后還要使每個文本成為中心

android:textAlignment="center"

如果您這樣寫,請檢查您的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,因此,您應該進行如下更改: @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, parent, false)); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, parent, false)); }希望幫助你。

如果我是你,我將獲得顯示尺寸的寬度,並相應地為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);

我不太確定這個確切的代碼是否可以解決您的問題,但是您可以輕松地對此進行更改。

在您的FrameLayout中編寫此代碼

    android:layout_gravity="center"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM