Android GridLayout-屏幕的一半

时间:2019-09-14 21:56:41

标签: 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

1 个答案:

答案 0 :(得分:0)

您可以在此设计中使用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>