CardView应该使用wrap_content作为列表项而不是match_parent。

3

卡片视图将内容包装而不是匹配父布局。 所有列表项都包裹在内,而不是匹配其宽度。 屏幕截图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout      
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="@dimen/card_margin"
        android:elevation="3dp"
        card_view:cardCornerRadius="@dimen/card_album_radius">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:id="@+id/Album_Art"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:src="@drawable/ic_google_music" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/song_name"
                android:id="@+id/Sname"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/Album_Art"
                android:layout_toEndOf="@+id/Album_Art" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/artist_album"
                android:id="@+id/Sdata"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                android:layout_toRightOf="@+id/Album_Art"
                android:layout_toEndOf="@+id/Album_Art"/>

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

手动设置宽度是有效的。当我将其设置为较大的 dp 值时,它可以正常工作,但列表项超出了窗口范围。我不明白为什么 match_parent 不起作用。如果有任何解决方法,请告诉我,谢谢。


2
我猜你在Adapter中膨胀布局时,将父级参数传递为null。 - Mike M.
@MikeM。是的,我需要传递什么参数? - Prashanth Ambati
1
在你膨胀布局的方法中传递的ViewGroup。可能被命名为parent - Mike M.
尝试在xml中将TextView设置为match_parent - Mykhailo
1
@MikeM. 你真是个救命稻草! - Ahsan
2个回答

3

您是否使用了recyclerView? 如果是这样,您应该像这样重写layoutmanager:

    layoutManager = new LinearLayoutManager(getActivity()) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
    }

3

如果您正在使用RecyclerViewAdapter,请确保在onCreateViewHolder中使用inflate方法的第一种签名而不是第二种。

1. inflate(LayoutInflater inflater, ViewGroup root, boolean attachToRoot)  

2. inflate(LayoutInflater inflater)

同时,确保将parent作为根传递,并将true/false作为attachToRoot参数。


谢谢,看来这就是我的问题所在。 - Abinash Dash

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接