Dynamic height of TextView within a GridLayout

后端 未结 2 390
北荒
北荒 2020-12-10 06:04

I have a problem using GridLayout using library compatibility (not tried without). I am using app:layout_gravity=\"fill_horizontal\" instead of android:la

相关标签:
2条回答
  • 2020-12-10 06:41

    Using TextView inside GridLayout is problematic, but there is a good way to use both together.

    This is what the example layout looks like:

    TextView inside GridLayout

    And this is the complete layout xml, the important lines are marked with ***.

    <?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>
    

    Depending on your needs, also this combination will work:

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

    Note that you don't have to use any namespace other than android for this to work.

    0 讨论(0)
  • 2020-12-10 06:44

    You have to set layout_width="0dp" and layout_gravity="fill_horizontal" for the TextView.

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

    Please, see full example here: https://groups.google.com/d/msg/android-developers/OmH3VBwesOQ/ZOGR0SGvC3cJ or here: http://daniel-codes.blogspot.com/2012/01/gridlayout-view-clipping-issues.html

    0 讨论(0)
提交回复
热议问题