【问题标题】:How to avoid TextView resizing in GridLayout v7?如何避免在 GridLayout v7 中调整 TextView 的大小?
【发布时间】:2018-05-07 02:48:05
【问题描述】:

我想使用 GridLayout v7 创建一个 3x3 网格。在我的代码中,TextView 的单元格会根据其内容自动调整大小。此外,它会影响其他单元格的大小,尽管它们位于 2 个不同的行上。

这是一个带有 2x2 网格的示例代码。 TextView 位于顶部,跨越 2 列。每个按钮应占据可用屏幕的一半(1 列)。如何修复 TextView 和其他组件的大小?谢谢!

我当然可以尝试嵌套的 LinearLayout,但这不是推荐的做法。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:grid="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    grid:columnCount="2"
    grid:rowCount="2">

    <TextView
        android:text="How to make this box stop growing while adding some text to it?"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        grid:layout_columnSpan="2"
        grid:layout_rowSpan="1"/>

    <Button
        android:text="some button"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        grid:layout_columnSpan="1"
        grid:layout_rowSpan="1"/>

    <Button
        android:text="some button"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        grid:layout_columnSpan="1"
        grid:layout_rowSpan="1"/>

</android.support.v7.widget.GridLayout>

【问题讨论】:

    标签: android textview android-gridlayout


    【解决方案1】:

    我认为你应该设置 layout_height 和 layout_width。 如下所示。

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    

    android 支持一些选项“match_parent”或“wrap_content”。

    https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

    【讨论】:

    • 不,你的解决方案不能解决我的问题:(而且我认为这根本没有意义。我想做的是按百分比分配空间,所以 match_parent 会拉伸一切到 GridLayout。或者我错过了什么?
    【解决方案2】:

    测试一下:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:grid="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        grid:columnCount="2"
        android:orientation="horizontal">
    
        <TextView
            android:text="How to make this box stop growing while adding some text to it?"
            grid:layout_columnSpan="2"
            grid:layout_gravity="fill"/>
    
        <Button
            android:text="some button"/>
    
        <Button
            android:text="some button"/>
    
    </android.support.v7.widget.GridLayout>
    

    【讨论】:

      猜你喜欢
      • 2014-10-02
      • 2019-04-05
      • 2011-11-11
      • 2018-09-02
      • 2018-04-30
      • 1970-01-01
      • 2015-06-02
      • 2016-08-25
      • 2015-11-12
      相关资源
      最近更新 更多