【问题标题】:Problem with filling 5th column in GridLayout在 GridLayout 中填充第 5 列的问题
【发布时间】:2020-09-17 04:43:33
【问题描述】:

当我添加第 5 列时,带有数字 5 一半的按钮爬出屏幕。当我为按钮添加值:android:layout_columnWeight="1" 时,编号为 1 的按钮会伸出屏幕。

如何在屏幕上显示所有 5 列?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rowCount="5"
        android:columnCount="5">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="1"
            android:textSize="25dp"
            android:textColor="@color/white"
            android:background="@color/black"
            android:layout_row="0"
            android:layout_column="0"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="2"
            android:textSize="25dp"
            android:textColor="@color/white"
            android:background="@color/black"
            android:layout_row="0"
            android:layout_column="1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="3"
            android:textSize="25dp"
            android:textColor="@color/white"
            android:background="@color/black"
            android:layout_row="0"
            android:layout_column="2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="4"
            android:textSize="25dp"
            android:textColor="@color/white"
            android:background="@color/black"
            android:layout_row="0"
            android:layout_column="3"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="5"
            android:textSize="25dp"
            android:textColor="@color/white"
            android:background="@color/black"
            android:layout_row="0"
            android:layout_column="4"/>
    </GridLayout>
</RelativeLayout>

【问题讨论】:

  • 你能在 Llinearlayout 中试试这个,然后它会在屏幕中正确显示。如果它只是一个按钮
  • 减少 android:textSize 。 GridLayout 强制?
  • 它就像计算器键。

标签: android xml grid-layout android-gridlayout


【解决方案1】:

在 GridLayout 中设置 android:layout_width="match_parent"

【讨论】:

【解决方案2】:

试试这个代码:

 <LinearLayout
    android:id="@+id/footer"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal"
    android:visibility="gone">

    <LinearLayout
        android:id="@+id/lay1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1"
            android:textSize="25dp"
            android:textColor="@color/colorAccent"
            android:background="@color/cardview_shadow_start_color"

            android:layout_row="0"
            android:layout_column="0"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/lay2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="2"
            android:textSize="25dp"
            android:textColor="@color/colorAccent"
            android:background="@color/cardview_shadow_start_color"

            android:layout_row="0"
            android:layout_column="0"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lay3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="3"
            android:textSize="25dp"
            android:textColor="@color/colorAccent"
            android:background="@color/cardview_shadow_start_color"

            android:layout_row="0"
            android:layout_column="0"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/lay4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="4"
            android:textSize="25dp"
            android:textColor="@color/colorAccent"
            android:background="@color/cardview_shadow_start_color"

            android:layout_row="0"
            android:layout_column="0"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lay5"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="5"
            android:textSize="25dp"
            android:textColor="@color/colorAccent"
            android:background="@color/cardview_shadow_start_color"

            android:layout_row="0"
            android:layout_column="0"/>
    </LinearLayout>


</LinearLayout>

【讨论】:

  • 不,这将是一个意大利面条代码,如果我添加14个按钮,那将是一团糟
猜你喜欢
  • 2014-10-09
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
  • 2020-10-23
  • 2011-08-21
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
相关资源
最近更新 更多