简体   繁体   中英

How to change the Button size in a GridLayout to “match_parent”?

I currently have this:

在此处输入图片说明

And I'm trying to get this:

在此处输入图片说明

I have tried for over an hour now, but cannot seem to get it working. Please take a look at my XML below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
android:background="@drawable/png1"
    android:layout_height="match_parent">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:text="Button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/button1" />

        <Button
            android:text="Button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/button2" />

        <Button
            android:text="Button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/button3"
            android:layout_row="1"
            android:layout_column="1" />
        <Button
            android:text="Button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/button4"
            android:layout_row="1"
            android:layout_column="0" />
        <Button
            android:text="Button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/button5"
            android:layout_row="2"
            android:layout_column="0" />


    </GridLayout>
</LinearLayout>

How can i change the last Button that he takes the whole place, like "match_parent"? Thanks

please try to use this

android:layout_width="match_parent"

instead of android:layout_width="100dp"

Add android:layout_columnSpan="2" to android:id="@+id/button5" . This will make the button span 2 columns.

   <Button
        android:text="Button"
        android:layout_columnSpan="2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:id="@+id/button5"
        android:layout_row="2"
        android:layout_column="0" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM