如何制作正方形的 CardView(安卓)

6

如何制作正方形的 CardView

当我在 CardView 上设置 layout_weight 时,它并不能像在 CardView 上一样被设置为正方形。

请给我一个解决方案:(不要设置固定的高度和宽度)

请参见下面的图片: enter image description here

代码:

<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:weightSum="3">

                    <android.support.v7.widget.CardView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_margin="4dp"
                        android:layout_weight="2"
                        app:cardBackgroundColor="@color/silver"
                        tools:ignore="NestedWeights">


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

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

                        <android.support.v7.widget.CardView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_margin="4dp"
                            android:layout_weight="0.5"
                            app:cardBackgroundColor="@color/silver">


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

                        <android.support.v7.widget.CardView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_margin="4dp"
                            android:layout_weight="0.5"
                            app:cardBackgroundColor="@color/silver">


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

                    </LinearLayout>

                </LinearLayout>

喜欢正方形吗?在几何学上?只需为宽度和高度设置相同的值,您还可以给它一些高度.. 如果您仍然卡住,请发布您的XML。 - Taslim Oseni
您可以在CardView上使用固定的高度和宽度。看起来您只有两种尺寸,左侧是大的,右侧是小的。如果想要支持不同的屏幕尺寸,布局权重并不是最佳选择。 - ravi
嵌套权重对性能不利,应使用ConstraintLayout代替。 - Pycpik
2个回答

17

您可以使用ConstraintLayout代替多个带有权重的LinearLayout

添加

implementation 'com.android.support.constraint:constraint-layout:1.1.2'

在你的build.gradle文件中添加以下内容,然后你可以尝试像这样设置(其中dimensionRatio设置为1):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">

<android.support.v7.widget.CardView
    android:id="@+id/card1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/no_picture" />
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    android:id="@+id/card2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="4dp"
    app:layout_constraintBottom_toTopOf="@id/card3"
    app:layout_constraintDimensionRatio="1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/card1"
    app:layout_constraintTop_toTopOf="@id/card1">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/no_picture" />
</android.support.v7.widget.CardView>


<android.support.v7.widget.CardView
    android:id="@+id/card3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="4dp"
    app:layout_constraintBottom_toBottomOf="@id/card1"
    app:layout_constraintDimensionRatio="1"
    app:layout_constraintEnd_toEndOf="@id/card2"
    app:layout_constraintStart_toStartOf="@id/card2"
    app:layout_constraintTop_toBottomOf="@id/card2">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/no_picture" />
</android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

结果:

在此输入图片描述


了解了 ConstraintLayout 的优点后,它是我唯一使用的布局!ConstraintLayout 的必须知道的功能包括 Barriers、Groups、Chains 等等... - Kathir

4
您可以使用自定义的CardView,并覆盖onMeasure()方法来强制实现1:1的宽高比。
public class SquareCardView extends CardView {

    public SquareCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int ignoredHeightMeasureSpec) {
        int newHeightMeasureSpec = widthMeasureSpec;
        super.onMeasure(widthMeasureSpec, newHeightMeasureSpec);
    }

}

然后在你的XML中使用这段代码。
<com.example.view.SquareCardView
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1">
  <ImageView
    android:src="@drawable/square1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/main_dark"
    android:adjustViewBounds="true" />
</com.example.view.SquareCardView>

1
但它没有设置为1:1的纵横比。 - Prince Dholakiya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接