代码之家  ›  专栏  ›  技术社区  ›  Scrin

如何设计CardView,使其彼此相邻?

  •  0
  • Scrin  · 技术社区  · 4 年前

    enter image description here

    你好!正如标题所暗示的那样,我试图将这两个CardView放在一起。可以这样做吗?目前这就是我的xml的样子。代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="@color/homeBackground"
        android:orientation="vertical"
        tools:context=".Home">
    
        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/layout_user_information"
    
            android:weightSum="4"
            android:padding="8dp"
            android:background="@color/colorButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:id="@+id/home_image"
                app:srcCompat="@drawable/userboy"
                android:layout_width="60dp"
                android:layout_height="60dp" />
    
            <LinearLayout
                android:orientation="vertical"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="8dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <TextView
                    android:id = "@+id/home_username"
                    android:text = "Username"
                    android:textColor="@android:color/white"
                    android:textSize="22sp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                </TextView>
    
    
            </LinearLayout>
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="8dp"
            android:weightSum="4">
    
            <androidx.cardview.widget.CardView
                android:id="@+id/card_view_booking"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="8dp"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                app:cardCornerRadius="8dp"
                android:layout_weight="1">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
    
                    <ImageView
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center_horizontal"
                        app:srcCompat="@drawable/home_booking" />
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:fontFamily="@font/poppinsbold"
                        android:text="Booking" />
    
                </LinearLayout>
    
    
            </androidx.cardview.widget.CardView>
    
        </LinearLayout>
    
        <LinearLayout
            android:orientation="horizontal"
            android:weightSum="4"
            android:padding="8dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <androidx.cardview.widget.CardView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view_history"
                android:layout_marginRight="8dp"
                android:layout_width="0dp"
                android:layout_weight="1"
                app:cardCornerRadius="8dp"
                android:layout_height="wrap_content"
                >
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                    <ImageView
                        app:srcCompat="@drawable/home_history"
                        android:layout_width="50dp"
                        android:layout_gravity="center_horizontal"
                        android:layout_height="50dp" />
    
                    <TextView
                        android:text="History"
                        android:fontFamily="@font/poppinsbold"
                        android:layout_gravity="center_horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
    
                </LinearLayout>
    
    
            </androidx.cardview.widget.CardView>
    
        </LinearLayout>
    
    </LinearLayout>
    
    

    我已经有了 match_parent 对于第一个LinearLayout的布局宽度和高度。但那里运气不佳。有什么办法可以解决这个问题吗?

    2 回复  |  直到 4 年前
        1
  •  4
  •   MRazaImtiaz    4 年前

    用另一个线性布局包裹包含CardView的两个布局

    android:orientation="horizontal"
    

    整个代码看起来像

    <?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/homeBackground"
        android:orientation="vertical"
        tools:context=".Home">
    
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="8dp"
                android:weightSum="4">
    
                <androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/card_view_booking"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="8dp"
                    android:layout_weight="1"
                    app:cardCornerRadius="8dp">
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
    
                        <ImageView
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_gravity="center_horizontal"
                            app:srcCompat="@drawable/home_booking" />
    
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:fontFamily="@font/poppinsbold"
                            android:text="Booking" />
    
                    </LinearLayout>
    
    
                </androidx.cardview.widget.CardView>
    
            </LinearLayout>
    
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="8dp"
                android:weightSum="4">
    
                <androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/card_view_history"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="8dp"
                    android:layout_weight="1"
                    app:cardCornerRadius="8dp">
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
    
                        <ImageView
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_gravity="center_horizontal"
                            app:srcCompat="@drawable/home_history" />
    
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:fontFamily="@font/poppinsbold"
                            android:text="History" />
    
                    </LinearLayout>
    
    
                </androidx.cardview.widget.CardView>
    
            </LinearLayout>
    
    
        </LinearLayout>
    
    
    </LinearLayout>
    
        2
  •  1
  •   Mokshda Gangrade    4 年前

    使用方向为“水平”的线性布局,并将卡片视图放置在其下方