0

我正在尝试将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 的预览版上,它看起来不错,但在应用程序上,它不是。我设法通过将 LLM 更改为GridLayoutManager仅包含一列的 a 来修复它。但它看起来很丑。

有谁知道发生了什么?谢谢。

4

5 回答 5

0

尝试将主布局的内容重力设置为中心。有两种方法可以做到这一点:

  1. 将“centerHorozontal”属性添加到“true”到您的框架布局。
  2. 将框架布局更改为线性布局,并将主要线性布局的重力设置为“中心”。

希望它会起作用,如果它不起作用,那么只需将您在布局中使用的图像发送给我,我将尝试使用其他一些技术来修复它,并将更新的代码发送给您。

于 2017-06-04T22:14:16.237 回答
0

如果你这样写,请检查你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)); } 希望对您有所帮助。

于 2017-06-05T01:15:26.767 回答
0

在您的 FrameLayout 中编写此代码

    android:layout_gravity="center"
于 2017-06-06T05:01:33.830 回答
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);

我不确定这个确切的代码是否能解决您的问题,但您可以轻松更改它,这样就可以了。

于 2017-06-05T22:09:42.233 回答
0

用相对布局替换约束布局

然后,使图像居中,使用这个

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

然后也使这些文本中的每一个都成为中心

android:textAlignment="center"
于 2017-06-05T00:33:13.143 回答