Space Recyclerview物品均匀(水平)

时间:2017-08-30 12:04:43

标签: android android-recyclerview

我有什么:

enter image description here

我想要的是什么:

enter image description here

片段布局:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_profile_selection_dim_bg"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000">

<android.support.constraint.ConstraintLayout 
    android:id="@+id/"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="320dp"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:clipChildren="false">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:elevation="1dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:paddingBottom="16dp"
            android:text=".."
            android:textColor=".."
            android:textSize="17.3sp" />
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="20dp"
        android:descendantFocusability="afterDescendants"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        >

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

</android.support.constraint.ConstraintLayout>
</RelativeLayout>

Recyclerview片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:layout_marginTop="15dp"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="10dp"/>

    <TextView
        android:id="@+id/"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="11.6sp"/>
</LinearLayout>

我正在使用recyclerviews GridManagerLayout(3列)。如果可能的话,我想使用xml属性来做这件事,但我当然可以接受其他想法。而且,当然,一些解释也会很好 - 我想要理解我的问题不仅要解决它。 :)

4 个答案:

答案 0 :(得分:1)

您必须将textview宽度设置为与ImageView相同

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_marginTop="15dp"
android:focusable="true"
android:focusableInTouchMode="true">

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginBottom="10dp"/>

<TextView
    android:id="@+id/"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:textSize="11.6sp"/>

答案 1 :(得分:0)

您必须在 recyclerView 行中将TextView宽度(wrap_content更改为50dp)。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="50dp"
        android:layout_height="50dp"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:layout_marginTop="15dp"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="10dp"/>

    <TextView
        android:id="@+id/"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        android:textSize="11.6sp"/>
</LinearLayout>

答案 2 :(得分:0)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:layout_marginTop="15dp"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="10dp"/>

    <TextView
        android:id="@+id/"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    `/>
</LinearLayout>

Change your parent layout width as a 50dp
Hope this will help you

答案 3 :(得分:0)

这个答案对我有用,我将宽度除以3 https://stackoverflow.com/a/37660180/9612786

查看我的代码:

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view;
    LayoutInflater inflater = LayoutInflater.from(mContext);
    view = inflater.inflate(R.layout.activity_item, parent, false);

    //int height = parent.getMeasuredHeight();
    int width = parent.getMeasuredWidth() / 3;

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    params.setMargins(0, 16,0, 16);

    view.setLayoutParams(params);


    return new MyViewHolder(view);
}