我的应用程序中有一个卡片视图(使用支持库),如果它们并非全部适合一行,我希望卡片能够环绕。目前,它们只是垂直堆叠。有没有办法让它们不仅包装,而且如果它们是列表中唯一的元素,还要填充整个父元素?
理想情况下,我希望每行有两张卡,然后让它们在那一点换行到新的一行。
包含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>
CardView:
<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>
答案 0 :(得分:1)
是的,您必须使用GridLayout
作为@ random6174 said,使用此code as guide,它完全符合您的要求。
注意这部分,它声明了RecyclerView LayoutManager (MainActivity.java)
:
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
我希望这会对你有所帮助。
答案 1 :(得分:0)
我想你可能想在这里使用流程布局。据我所知,Android没有内置的流程布局类,但在其他地方有很多东西要找;例如this one。
但是,由于您希望显示可能具有相同尺寸的卡片,您可以使用GridLayout
并以编程方式调整列数以满足您的需求。
/ edit:没关系,我刚学会了CardView是什么。我以为你想要显示扑克牌。也许流量布局仍然适合你,但我肯定误解了这个问题。我的坏。