java - 布局权重不起作用 CardView

标签 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>

有人可以帮忙吗?

提前致谢!

最佳答案

<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

关于java - 布局权重不起作用 CardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35325525/

相关文章:

java - Tomcat 6 中的 Quartz 调度程序,线程不会停止

android-layout - 如何防止键盘将操作栏推离屏幕

android - 检查 View 是否属于线性布局

java - 片段着色器中的状态相关渲染

java - 扫描仪将指针重置到上一行

android - 如何自定义一个TimePickerDialog?

android - 带 Google map 的 PhoneGap/Android 应用程序的域白名单

javascript - 无法导航到 jquerymobile/phonegap 中动态添加的页面

android-layout - "end edge"、 "right edge"、 "toEndOf"、 "toRightOf"之间的XML Android差异

java - 排除具有某些扩展名的文件名的正则表达式