Java 在Android中如何使布局高度等于其宽度?

Java 在Android中如何使布局高度等于其宽度?,java,android,xml,layout,Java,Android,Xml,Layout,我想让我所有的cardwiew的高度等于它们的宽度,并且我想让每个cardwiew占据用户设备屏幕的相同部分。我该怎么做?我的代码如下: <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layo

我想让我所有的
cardwiew
的高度等于它们的宽度,并且我想让每个
cardwiew
占据用户设备屏幕的相同部分。我该怎么做?我的代码如下:

<android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp">

                <android.support.v7.widget.CardView
                    android:id="@+id/tool1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="3dp"
                    android:layout_marginRight="3dp"
                    android:foregroundGravity="center"
                    app:layout_constraintHorizontal_weight="1"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toLeftOf="@id/tool2">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/too_4_background"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:paddingBottom="10dp"
                        android:paddingTop="10dp">

                        <LinearLayout
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:background="@drawable/emoji_background"
                            android:gravity="center">

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:gravity="center"
                                android:text="&#x1F633;"
                                android:textSize="15dp" />

                        </LinearLayout>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="4dp"
                            android:gravity="center"
                            android:text="ارسال رایگان"
                            android:textColor="#ffffff"
                            android:textSize="13dp" />

                    </LinearLayout>


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

                <android.support.v7.widget.CardView
                    android:id="@+id/tool2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="3dp"
                    android:layout_marginRight="3dp"
                    app:layout_constraintHorizontal_weight="1"
                    app:layout_constraintLeft_toRightOf="@id/tool1"
                    app:layout_constraintRight_toLeftOf="@id/tool3">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/too_4_background"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:paddingBottom="10dp"
                        android:paddingTop="10dp">

                        <LinearLayout
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:background="@drawable/emoji_background"
                            android:gravity="center">

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:gravity="center"
                                android:text="&#x1F633;"
                                android:textSize="15dp" />

                        </LinearLayout>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="4dp"
                            android:gravity="center"
                            android:text="ارسال رایگان"
                            android:textColor="#ffffff"
                            android:textSize="13dp" />

                    </LinearLayout>


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

                <android.support.v7.widget.CardView
                    android:id="@+id/tool3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="3dp"
                    android:layout_marginRight="3dp"
                    app:layout_constraintHorizontal_weight="1"
                    app:layout_constraintLeft_toRightOf="@id/tool2"
                    app:layout_constraintRight_toLeftOf="@id/tool4">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/too_4_background"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:paddingBottom="10dp"
                        android:paddingTop="10dp">

                        <LinearLayout
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:background="@drawable/emoji_background"
                            android:gravity="center">

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:gravity="center"
                                android:text="&#x1F633;"
                                android:textSize="15dp" />

                        </LinearLayout>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="4dp"
                            android:gravity="center"
                            android:text="ارسال رایگان"
                            android:textColor="#ffffff"
                            android:textSize="13dp" />

                    </LinearLayout>

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

                <android.support.v7.widget.CardView
                    android:id="@+id/tool4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="3dp"
                    android:layout_marginRight="3dp"
                    app:layout_constraintHorizontal_weight="1"
                    app:layout_constraintLeft_toRightOf="@id/tool3"
                    app:layout_constraintRight_toRightOf="parent"
                    >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/too_4_background"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:paddingBottom="10dp"
                        android:paddingTop="10dp">

                        <LinearLayout
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:background="@drawable/emoji_background"
                            android:gravity="center">

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:gravity="center"
                                android:text="&#x1F633;"
                                android:textSize="15dp" />

                        </LinearLayout>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="4dp"
                            android:gravity="center"
                            android:text="ارسال رایگان"
                            android:textColor="#ffffff"
                            android:textSize="13dp" />

                    </LinearLayout>

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


            </android.support.constraint.ConstraintLayout>

如果问题是设置相同的高度和宽度,则代码如下

<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="wrap_content">

<android.support.design.card.MaterialCardView
    app:layout_constraintDimensionRatio="1:1"
    android:layout_width="0dp"
    android:layout_height="0dp">

</android.support.design.card.MaterialCardView>

</android.support.constraint.ConstraintLayout>

如果问题是设置相同的高度和宽度,则代码如下

<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="wrap_content">

<android.support.design.card.MaterialCardView
    app:layout_constraintDimensionRatio="1:1"
    android:layout_width="0dp"
    android:layout_height="0dp">

</android.support.design.card.MaterialCardView>

</android.support.constraint.ConstraintLayout>

您需要自定义CardView,比如SquareCardView:

public class SquareCardView extends CardView {
  public SquareCardView(Context context) {
    super(context);
  }

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

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

  public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
    setMeasuredDimension(size, size);
  }
}
然后,在布局中使用该自定义卡片视图:

<com.yourPackageName.SquareCardView
    android:id="@+id/tool1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:foregroundGravity="center"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/tool2">

您需要自定义CardView,比如SquareCardView:

public class SquareCardView extends CardView {
  public SquareCardView(Context context) {
    super(context);
  }

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

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

  public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
    setMeasuredDimension(size, size);
  }
}
然后,在布局中使用该自定义卡片视图:

<com.yourPackageName.SquareCardView
    android:id="@+id/tool1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:foregroundGravity="center"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/tool2">


您想制作方形卡片视图吗?您应该计算设备屏幕宽度,减去填充和边距(如果有)后,计算卡片的宽度。现在您已经有了宽度,只需使用LayoutParams@Ümañgßürmån设置高度即可Yes@HarishKamboj我知道,但我想把这件事做完xml@VladyslavMatviienko是 啊您必须尽快掌握ConstraintLayout,因为这将有助于降低您的开发时间。是否要制作square cardview?您应该计算设备屏幕宽度,并在减去填充和边距(如果有)后计算卡的宽度。现在您已经有了宽度,只需使用LayoutParams@Ümañgßürmån设置高度即可Yes@HarishKamboj我知道,但我想把这件事做完xml@VladyslavMatviienko是的,您必须尽快掌握ConstraintLayout,因为这将有助于降低您的开发时间。使用ConstraintLayout,我们可以将高度和宽度设置为“0dp”并添加app:layout\u constraintDimensionRatio=“1:1”以获得相同的结果。谢谢。这是一种方法,但不是最好的方法。使用ConstraintLayout,我们可以将高度和宽度设置为“0dp”,并添加app:layout\u constraintDimensionRatio=“1:1”以获得相同的结果。谢谢。这是一种方法,但不是最好的方法。