簡體   English   中英

CardView包裝

[英]CardView wrapping

我在我的應用程序中有一張卡片視圖(使用支持庫),並且如果卡片不能全部排成一排,我希望它們可以繞起來。 目前,它們只是垂直堆疊。 如果它們是列表中的唯一元素,是否有辦法使它們不僅包裝,還可以填充整個父級?

理想情況下,我希望每行兩張卡,然后在那一點將它們包裝到新行。

包含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>

是的,您必須使用@ random6174 所說GridLayout ,將此代碼用作指南 ,它確實可以實現您想要的功能。

注意這一部分,在這里聲明了RecyclerView LayoutManager (MainActivity.java)

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

我希望這可以幫助你。

我認為您可能要在此處使用流布局 據我所知,Android沒有內置的流程布局類,但是在其他地方有很多東西可以找到。 例如這個

但是,然后,由於您要顯示的卡片可能都具有相同的大小,因此也許還可以使用GridLayout並以編程方式調整列數來滿足您的需求。

祝好運!

/ edit:沒關系,我剛剛了解了CardView是什么。 我以為你想顯示紙牌。 也許流程布局仍然適合您,但是我肯定會誤解這個問題。 我的錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM