Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使CardView宽度与高度匹配?_Android_Layout - Fatal编程技术网

Android 如何使CardView宽度与高度匹配?

Android 如何使CardView宽度与高度匹配?,android,layout,Android,Layout,我有一个线性布局的CardView。布局为CardView保留1/5的屏幕,为其他内容保留4/5的屏幕 我希望cardview是方形的 我将CardView的高度设置为“匹配父项”。但是我如何设置它的宽度来匹配它的高度呢 我必须创建一个自定义视图并使用OnMeasure(),还是有更简单的方法 以下是我的视图的XML: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schem

我有一个线性布局的CardView。布局为CardView保留1/5的屏幕,为其他内容保留4/5的屏幕

我希望cardview是方形的

我将CardView的高度设置为“匹配父项”。但是我如何设置它的宽度来匹配它的高度呢

我必须创建一个自定义视图并使用OnMeasure(),还是有更简单的方法

以下是我的视图的XML:

<?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:orientation="vertical"
    tools:context=".MainActivity">

    LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4" >
<!--        Other stuff goes here-->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <androidx.cardview.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:clickable="true"
            android:foreground="?selectableItemBackground"
            app:cardBackgroundColor="#81C784"
            app:cardCornerRadius="12dp"
            app:cardElevation="3dp"
            app:contentPadding="4dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/card_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textColor="#000000"
                    android:textSize="15sp"
                    android:text="TEST"/>

                <TextView
                    android:id="@+id/card_desc"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textColor="#000000"
                    android:textSize="12sp"
                    android:text="123"/>

                <ImageView
                    android:id="@+id/card_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:padding="3dp"
                    android:scaleType="fitCenter"
                    android:src="@mipmap/ic_launcher"
                    />
            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </LinearLayout>
</LinearLayout>

线性布局
android:layout\u width=“匹配父项”
android:layout\u height=“0dp”
android:layout_weight=“4”>

如果您正在寻找一种xml方法来实现这一点,那么可以使用
ConstraintLayout
来完成。您需要将cardView的父布局设置为constraintlayout而不是LinearLayout,然后设置约束和要设置的最重要部分

app:layout_constraintDimensionRatio="1:1"
就是这样,您只需要为高度和宽度指定适当的值,这将确保它始终保持1:1的纵横比

所以你的代码应该是这样的

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4" >
        <!--        Other stuff goes here-->
    </LinearLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">


        <!--  match_parent also works, but use 0dp in constraintLayout as below--> 
        <androidx.cardview.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="5dp"
            android:clickable="true"
            android:foreground="?selectableItemBackground"
            app:cardBackgroundColor="#81C784"
            app:cardCornerRadius="12dp"
            app:cardElevation="3dp"
            app:contentPadding="4dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="W,1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/card_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="TEST"
                    android:textColor="#000000"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/card_desc"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="123"
                    android:textColor="#000000"
                    android:textSize="12sp" />

                <ImageView
                    android:id="@+id/card_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:padding="3dp"
                    android:scaleType="fitCenter"
                    android:src="@mipmap/ic_launcher" />
            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

因此,选择最适合您的图像。

添加所需输出的图像。检查我的答案,如果需要更改任何内容,请回复。我会帮你实现的。屏幕截图增加了一个宽度,因为cardview的高度可能会根据屏幕的大小而改变。谢谢!我认为必须有一种方法来实现这一点,而不必求助于自定义视图。
CardView cardView = findViewById(R.id.card_view);
ViewGroup.LayoutParams params = cardView.getLayoutParams();
params.height = params.width;
cardView.setLayoutParams(params);