gpt4 book ai didi

java - Android GridLayout - 屏幕的一半

转载 作者:行者123 更新时间:2023-12-02 00:41:13 24 4
gpt4 key购买 nike

我有一个包含 2 列的 GridLayout。我希望 ImageButtons 的宽度是屏幕宽度的一半,而高度应该与宽度相同,因为图像是正方形。

这就是我所拥有的,并且我已经使用了这些配置,但我无法做到。 Android 开发新手。

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2">

<ImageButton
android:layout_width="match_parent"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_height="match_parent"
android:background="@drawable/food0t"/>

<ImageButton
android:layout_width="match_parent"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_height="match_parent"
android:background="@drawable/food0t"/>


</GridLayout>
</ScrollView>

这是预期的结果: Expected

最佳答案

您可以使用 ConstraintLayout 进行此设计

<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".activities.SplashActivity">




<ImageButton
android:id="@+id/img1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/colorPrimaryDark"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/img2"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginRight="5dp"
app:layout_constraintTop_toTopOf="parent" />

<ImageButton
android:id="@+id/img2"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/black"
android:layout_marginLeft="5dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/img1"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/img3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:background="@color/colorPrimaryDark"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/img4"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginRight="5dp"
app:layout_constraintTop_toBottomOf="@id/img1" />

<ImageButton
android:id="@+id/img4"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/black"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/img3"
app:layout_constraintTop_toBottomOf="@id/img2" />


</androidx.constraintlayout.widget.ConstraintLayout>

关于java - Android GridLayout - 屏幕的一半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57939558/

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