0

我的应用程序中有一个卡片视图(使用支持库),如果卡片不能全部放在一行中,我希望卡片可以环绕。目前,它们只是垂直堆叠。如果它们是列表中唯一的元素,有没有办法让它们不仅包装,而且填充整个父元素?

理想情况下,我希望每行有两张卡片,然后让它们在该点换行到新行。

包含 recyclerview 的视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_rootlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/my_window"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:orientation="vertical">

    <TextView
        android:id="@+id/my_headertext"
        style="@style/roboto_header_text_black"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_weight=".15"
        android:gravity="center"
        android:padding="@dimen/device_padding"
        android:text="@string/en_us_my_header"
        android:textIsSelectable="false" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".85"

        />
</LinearLayout>

卡片视图:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:orientation="vertical"
card_view:cardCornerRadius="2dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/image_thumbnail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:src="@drawable/default_image"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="#99000000"
        android:gravity="center_vertical"
        android:paddingLeft="8dp">

        <TextView
            android:id="@+id/my_listitem_title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="This is my title"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:fontFamily="sans-serif-light"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:paddingTop="4dp"
            android:paddingBottom="4dp" />

    </LinearLayout>
</RelativeLayout>

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="8dp"
        android:indeterminate="false"
        style="?android:progressBarStyleHorizontal"
        android:progress="45" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="2dp"
        android:paddingBottom="2dp">
    <TextView
        android:id="@+id/code_text_block"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="14sp"
        android:text="ABC" />

    <TextView
    android:id="@+id/my_listitem_id_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="#123456"
        android:textColor="@color/black"
    android:layout_marginLeft="8dp" />
    </LinearLayout>

    <TextView
        android:id="@+id/my_listitem_date_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
        android:text="Nov 5 - Nov 12, 2016"
        android:textColor="#7D7D7D" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="4dp"
        android:paddingBottom="4dp">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Download"
            android:textAllCaps="true"
            android:fontFamily="sans-serif"
            android:textStyle="bold"
            android:id="@+id/download_button"
            android:background="@color/white"
            android:textColor="@color/black"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:paddingTop="8dp"
            android:paddingBottom="8dp" />
    </LinearLayout>

</LinearLayout>

4

2 回答 2

1

是的,您必须GridLayout像@random6174所说的那样使用 a ,将此代码用作指南,它完全符合您的要求。

注意这部分,它声明了RecyclerView LayoutManager (MainActivity.java)

mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);

我希望这可以帮助你。

于 2015-02-22T16:28:58.580 回答
0

我想你可能想在这里使用流布局。据我所知,Android 没有内置的流布局类,但其他地方有很多东西可以找到;比如这个

但是,由于您想要显示可能都具有相同大小的卡片,也许您也可以使用 aGridLayout并以编程方式调整列数以满足您的需要。

祝你好运!

/edit:没关系,我刚刚了解了 CardView 是什么。我以为你想展示扑克牌。也许流布局仍然适合您,但我肯定误解了这个问题。我的错。

于 2014-10-23T20:21:29.750 回答