【问题标题】:How to make textViews fill a row width?如何使 textViews 填充行宽?
【发布时间】:2017-12-30 01:28:12
【问题描述】:

我正在准备一个井字游戏: 最初所有9个单元格都是空的。当我输入数字开始游戏时,单元格的宽度会动态变化。如何防止这种情况发生?

我的 9 个单元格的 xml 代码:

<GridLayout
    android:id="@+id/grid_layout_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginHorizontal="20dp"
    android:background="@android:color/holo_red_dark"
    android:rowCount="3"
    android:columnCount="3"
    android:padding="10dp"
    android:theme="@style/MyTextViewStyle">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        tools:text="1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        tools:text="2" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        tools:text="" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        tools:text="4" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        tools:text="5" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        tools:text="" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        tools:text="7" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        tools:text="8" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        tools:text="" />

</GridLayout>

PS:我不想使用表格布局、水平和垂直线性布局集或任何其他视图。请告诉我如何在 GridLayout 本身中做到这一点。

【问题讨论】:

    标签: android android-gridlayout


    【解决方案1】:

    在每个TextView 中,将layout_width 设置为0dp。最初,TextView 的大小为 0 像素,但由于以下原因,它们将同样填充剩余空间:android:layout_columnWeight="1"

    【讨论】:

      【解决方案2】:

      请看答案here。您只需要在每个 TextView 上更改以下部分:

      android:layout_width="0dp"
      

      【讨论】:

        【解决方案3】:

        您需要在行的每一列上放置一个固定的权重。 为此,您必须在本例中为每个组件分配 Textview, 他们将在行中拥有的 pes。如果您的行将有 2 个组件,您可以分配给 weight_sum = "1" 中权重总和的值,然后在每个组件中分配您希望它们相等时所拥有的权重,例如 layout_weight = 0.5 .因此,通过这种方式,您将固定容器内的每个组件宽度。

        希望对你有所帮助。

        您的代码:

        <GridLayout
            android:id="@+id/grid_layout_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginHorizontal="20dp"
            android:background="@android:color/holo_red_dark"
            android:rowCount="3"
            android:columnCount="3"
            weight_sum="3"
            android:padding="10dp"
            android:theme="@style/MyTextViewStyle">
        
            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_weight="1"
                android:layout_rowWeight="1"
                tools:text="1" />
        
            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_weight="1"
                android:layout_rowWeight="1"
                tools:text="2" />
        
            <TextView
                android:id="@+id/textView3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_weight="1"
                android:layout_rowWeight="1"
                tools:text="" />
        
            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                tools:text="4" />
        
            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                tools:text="5" />
        
            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                tools:text="" />
        
            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                tools:text="7" />
        
            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                tools:text="8" />
        
            <TextView
                android:id="@+id/textView9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                tools:text="" />
        
        </GridLayout>
        

        【讨论】:

          【解决方案4】:

          我可以看到您使用了android:layout_columnWeight="1",但也使用了android:layout_width="wrap_content"。正如post 中提到的,不需要在 GridLayout 中使用 layout_width 参数,因此应该避免。

          尝试将您的 layout_width 和 layout_height 设置为任何固定值,例如 10 dp,然后 android 宁愿使用 Layout_columnWeight 并为每个 textview 分配相等的部分。另外,请阅读此post 以允许 layout_width 向后兼容。

          【讨论】:

            猜你喜欢
            • 2023-04-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-08-30
            • 2011-01-15
            • 1970-01-01
            • 2022-01-20
            • 2014-07-04
            相关资源
            最近更新 更多