gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:26:58 25 4
gpt4 key购买 nike

我想让所有 CardView 的高度等于它们的宽度,而且我希望每个 CardView 都占据用户设备屏幕的相同部分。我怎样才能做到这一点?我的代码是这样的:

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

关于java - 如何在 Android 中使布局高度等于其宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53299673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com