简体   繁体   中英

How to set my button width to "wrap_content" inside the GridLayout?

My buttons are being overflow inside the gridlayout because their length are being long from the start unnecessarily. I just want to set them wrap_content to all of them. I was doing this tutorial. Here's my code.

<GridLayout
    android:id="@+id/characterLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/keyOptionLayout"
    android:layout_below="@id/tvLotteryNumber"
    android:background="#33000000"
    android:columnCount="5"
    android:orientation="horizontal">

    <Button
        android:layout_row="0"
        android:text="a" />

    <Button
        android:layout_row="0"
        android:text="b" />

    <Button
        android:layout_row="0"
        android:text="c" />

    <Button
        android:layout_row="0"
        android:text="d" />

    <Button
        android:layout_row="0"
        android:text="e" />

</GridLayout>

Removing android_layout_row="0" lines doesn't change anything. Here's how they displayed.

在此处输入图片说明

You can add

android:layout_width="0dp"
 android:layout_height="0dp'

to your buttons and add

grid:layout_columnWeight="1"
grid:layout_rowWeight="1"

to your buttons. Don't forget to add xmlns:android="http://schemas.android.com/apk/res/android" namepsace to your GridLayout

Using android.support.v7.widget.GridLayout solved the issue.

  1. In Gradle, compile 'com.android.support:gridlayout-v7:25.3.1'
  2. I changed my code as the correct answer of this question.

Result:

在此处输入图片说明

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