java - Android GridLayout - 屏幕的一半

标签 java android

我有一个包含 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/

相关文章:

java - 在 android canvas 中拖动旋转文本无法按预期工作

java - Jersey 将空列表和单元素列表序列化为数组

java - 在读取操作中使用 Hibernate 的 StaleObjectStateException?

java - 将 JVM 和代码合并到一个文件中

java - 如何使用 Java 驱动程序获取 MongoDB 中嵌入文档数组的特定值

java - Java 作业。找出连续五位数字的最大乘积

java - 枚举抛硬币程序卡住了

android - 全屏 Activity 向导 Activity 。当我与设备交互时,如何停止显示操作栏?

android - 具有 scale 属性的 ObjectAnimator 使 bg 变黑?

android - 在 Arraylist 的 ListView 的列表项上显示值