卡未正确对齐

时间:2016-07-25 18:03:20

标签: android android-cardview

我想要有四张卡片。我希望前两张卡片彼此相邻水平对齐,然后两张卡片需要在下方,并且水平对齐。

我使用两个单独的LinearLayout分别用于一行两张牌(即每行两张牌)。

问题是只有前两张牌可见而另外两张牌不可见。

  

XML代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="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:orientation="vertical"
    tools:context=".MainActivity"
    android:background="#bcd4d4"
    android:weightSum="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
       >
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view1"
        android:layout_width="180"
        android:layout_height="200dp"
        card_view:cardCornerRadius="10dp"
        android:foregroundGravity="center"
        android:layout_marginBottom="20dp"
        card_view:cardElevation="15dp"
        card_view:cardBackgroundColor="#52bf90"
        card_view:cardUseCompatPadding="true"
        android:onClick="missed"

        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="MISSED"
            android:textSize="35sp"
            android:textAllCaps="true"
            android:textAlignment="gravity"
            android:layout_marginTop="40dp"
            >

        </TextView>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view2"
        android:layout_width="180dp"
        android:layout_height="200dp"
        card_view:cardCornerRadius="10dp"
        android:foregroundGravity="center"
        card_view:cardElevation="15dp"
        card_view:cardBackgroundColor="#52bf90"
        card_view:cardUseCompatPadding="true"
        android:onClick="received"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="RECEIVED"
            android:textSize="30sp"
            android:textAllCaps="true"
            android:layout_marginTop="40dp"
            >
        </TextView>

        <!--/>-->
    </android.support.v7.widget.CardView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
       >
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view3"
        android:layout_width="180dp"
        android:layout_height="200dp"
        card_view:cardCornerRadius="10dp"
        android:foregroundGravity="center"

        card_view:cardElevation="15dp"
        card_view:cardBackgroundColor="#52bf90"
        card_view:cardUseCompatPadding="true"
        android:onClick="dialled"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="DIALLED"
            android:textSize="40sp"
            android:textAllCaps="true"
            android:layout_marginTop="40dp"
            >
        </TextView>

    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view4"
        android:layout_width="180dp"
        android:layout_height="200dp"
        card_view:cardCornerRadius="10dp"
        android:foregroundGravity="center"

        card_view:cardElevation="15dp"
        card_view:cardBackgroundColor="#52bf90"
        card_view:cardUseCompatPadding="true"
        android:onClick="stats"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="STATS"
            android:textSize="40sp"
            android:layout_marginTop="40dp"
            android:textAllCaps="true"
            >
        </TextView>
    </android.support.v7.widget.CardView>
</LinearLayout>

1 个答案:

答案 0 :(得分:2)

您需要将LinerLayout的方向设置为horizontal,将layout_height设置为wrap_content

  

如下所示。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="20dp">
相关问题