【问题标题】:Android TableLayout: Stretch one column and have two with equal widthAndroid TableLayout:拉伸一列并有两个等宽
【发布时间】:2020-01-22 20:56:29
【问题描述】:

我需要this, where two columns have the same width (0 and 2) and another one stretches to fill the remaining space (col 1) 之类的东西。但是,两个相等的列的内容经常变化,所以有时第一列会比另一列大,反之亦然。我的第一个想法是使用 TableLayout 和 android:layout_weight,但这并没有多大帮助,因为我将 android:layout_weight="1"android:layout_weight="2" 设置为中间的。

<TableRow
    android:id="@+id/firstRow"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/0"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="0" />

    <TextView
        android:id="@+id/1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:gravity="center"
        android:text="1" />

    <TextView
        android:id="@+id/2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="2" />
</TableRow>

有没有办法只使用布局或以编程方式做到这一点?

【问题讨论】:

    标签: android android-constraintlayout tablelayout android-tablelayout


    【解决方案1】:

    您可以使用ContraintLayout 和使用app:layout_constraintHorizontal_weight 属性的水平链。

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
    
      <TextView
        android:id="@+id/1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/2"
        app:layout_constraintHorizontal_weight="2.5"/>
    
      <TextView
        android:id="@+id/2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        app:layout_constraintRight_toRightOf="@+id/3"
        app:layout_constraintRight_toLeftOf="@+id/1"
        app:layout_constraintHorizontal_weight="5"/>
    
      <TextView
        android:id="@+id/3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        app:layout_constraintLeft_toRightOf="@+id/2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_weight="2.5"/>
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 感谢您的回答。我觉得我说的不够具体。左右文本视图应该有一个灵活的宽度,这是两者都需要的最大值。 |abcde|----middle----| 2 |
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    相关资源
    最近更新 更多