【问题标题】:Dynamic height of TextView within a GridLayoutGridLayout 中 TextView 的动态高度
【发布时间】:2012-08-27 17:23:54
【问题描述】:

我在使用 GridLayout 使用库兼容性时遇到问题(没有尝试过)。我使用的是app:layout_gravity="fill_horizontal" 而不是android:layout_gravity="fill_horizontal",但是TextView 中的所有内容都没有显示出来。为了显示所有内容,我必须设置TextView“标题”的高度,但我想要一个动态高度,而不是设定高度。

有什么想法吗?

【问题讨论】:

    标签: android textview clipping grid-layout


    【解决方案1】:

    您必须为 TextView 设置 layout_width="0dp"layout_gravity="fill_horizontal"

    <TextView
        android:layout_width="0dp"
        android:layout_gravity="fill_horizontal" />
    

    请在此处查看完整示例:https://groups.google.com/d/msg/android-developers/OmH3VBwesOQ/ZOGR0SGvC3cJ 或此处:http://daniel-codes.blogspot.com/2012/01/gridlayout-view-clipping-issues.html

    【讨论】:

    • 注意 **app:**layout_gravity 很重要。我正在设置样式中的值,但它 lint 并没有警告我,我花了一段时间才弄清楚问题。
    • 如果位于第二行则取上一行的宽度
    【解决方案2】:

    GridLayout 中使用TextView 是有问题的,但是一种同时使用两者的好方法。

    这是示例布局的样子:

    这是完整的布局xml,重要的行用***标记。

    <?xml version="1.0" encoding="utf-8"?>
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="3"              *   this example uses 3 columns
        android:orientation="horizontal" >   *** use "horizontal"
    
    <TextView                                *   just a normal view
        android:layout_column="0"
        android:layout_row="0"
        android:background="#666666"
        android:text="A"
        android:textColor="#afafaf"
        android:textSize="60sp"
        android:textStyle="bold" />
    
    <TextView                                *   this text will not be cut!
        android:layout_width="0dp"           *** important: set width to 0dp
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_columnSpan="2"        *   colspan does also work with this
        android:layout_gravity="fill_horizontal|bottom"        *** set to "fill*"!
        android:layout_row="0"
        android:text="This view has 2 columns. Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
        android:textColor="#666666" />
    
    </GridLayout>
    

    根据您的需要,这种组合也可以使用:

        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="fill"
        android:gravity="bottom"
    

    请注意,您不必使用除 android 之外的任何命名空间即可。

    【讨论】:

    • 谢谢,它有效。但我很好奇你是怎么发现这个把戏的? (layout_width 为 0dp,layout_gravity 为填充属性)
    • 很好地发现了这一点。很难让我的 TextView 在我的 GridLayout 中正确包装文本。遵循有关同一问题的许多主题的所有其他建议和答案,但只有您的有效。非常感谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 2015-07-11
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多