布局重量不起作用CardView

时间:2016-02-10 20:43:46

标签: java android android-layout android-cardview

我希望我的屏幕显示总共3张卡片均匀分布占据了整个空间,但我尝试的任何东西似乎都在工作。这些卡正在使用RecyclerView / viewHolder加载。 我尝试了两种解决方案。

解决方案1:

<?xml version="1.0" encoding="utf-8"?>

<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="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/card_view_disciplines"
    card_view:cardCornerRadius="4dp"

    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3"
    >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/disciplineName"
            android:textSize="25sp"
            android:layout_weight="1"
            />
</LinearLayout>
    </RelativeLayout>

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

和解决方案2:

<?xml version="1.0" encoding="utf-8"?>

<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="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/card_view_disciplines"
    card_view:cardCornerRadius="4dp"

    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="3"
    >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/disciplineName"
            android:textSize="25sp"
            android:layout_weight="1"
            />
</LinearLayout>
    </RelativeLayout>

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

有人可以帮忙吗?

提前致谢!

2 个答案:

答案 0 :(得分:1)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:weight_sum="3"
    android:padding="16dp">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:cardBackgroundColor="#000">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="40sp"
                android:text="Card 1"
                android:textColor="#FFF"
                android:layout_gravity="center"/>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:padding="16dp"
            android:background="#000">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="40sp"
                android:text="Card 2"
                android:layout_gravity="center"/>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:cardBackgroundColor="#000">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="40sp"
                android:text="Card 3"
                android:textColor="#FFF"
                android:layout_gravity="center"/>
    </android.support.v7.widget.CardView>
    </LinearLayout>

Result

答案 1 :(得分:1)

我希望这会按照您的预期运作:

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#888888"
    android:orientation="vertical"
    android:weightSum="3">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:layout_weight="1"
        card_view:cardBackgroundColor="#000088"
        card_view:cardCornerRadius="5dp"
        card_view:cardElevation="5dp">


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

    <android.support.v7.widget.CardView
        android:id="@+id/cardView2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:layout_weight="1"
        card_view:cardBackgroundColor="#008800"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="5dp">

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

    <android.support.v7.widget.CardView
        android:id="@+id/cardView3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:layout_weight="1"
        card_view:cardBackgroundColor="#880000"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="5dp">

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

MainActivity.java

CardView cardView1 = (CardView) findViewById(R.id.cardView1);
        CardView cardView2 = (CardView) findViewById(R.id.cardView2);
        CardView cardView3 = (CardView) findViewById(R.id.cardView3);

        cardView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "CardView1", Toast.LENGTH_SHORT).show();
            }
        });

        cardView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "CardView2", Toast.LENGTH_SHORT).show();
            }
        });

        cardView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "CardView3", Toast.LENGTH_SHORT).show();
            }
        });