【问题标题】:Button fill the whole row in TableLayout?按钮填充TableLayout中的整行?
【发布时间】:2020-07-30 21:48:15
【问题描述】:

我有一个垂直方向的LinearLayout,在第一行我添加了一个填充父级的按钮。

我想使用 TableLayout 实现同样的功能。
到目前为止我尝试过的是:

<TableLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout">
<TableRow 
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:id="@+id/firstRow">

<Button
     android:id="@+id/button1"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Button" />
</TableRow >
</TableLayout>

TableRow 填充父级,但行内的按钮不会填充整行。

如何让按钮像这样填充父级?


编辑:

以及如何让它像这样垂直填充父级:

【问题讨论】:

    标签: android android-layout android-tablelayout


    【解决方案1】:

    使用android:layout_weight="1"

       <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TableRow>
    
            <Button
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Why is doing layouts on Android" />
        </TableRow>
    
        <TableRow>
    
            <Button
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="damn" />
        </TableRow>
    
    </TableLayout>
    

    看起来像:

    【讨论】:

    • 是的,使用权重来解决问题。 +1
    • @Caffè 我很高兴我能帮上忙 :) 祝你好运,玩得开心 :D 顺便说一句,如果我的回答对你有帮助,请标记是并正确 :) 我会很高兴 :D跨度>
    • @MatejŠpilár:我有一个小问题,如果我想让damn 按钮垂直呢?
    • @Caffè 垂直怎么样?它将占据第一行下方的整个屏幕吗? :)
    • @MatejŠpilár:是的,在第一行下方但不占据整个碎石,只占据一列。
    【解决方案2】:
    <TableLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill"
            android:layout_weight="1">
    
            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Button" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Button" />
        </TableRow>
    

    【讨论】:

    • 很简单,看到填充列使用 android:layout_weight="1" 然后填充列只需将按钮的高度或 TextView 的高度设置为 match_parent, android:layout_height="match_parent ”。看到这么简单,祝你好运!
    猜你喜欢
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 2011-10-03
    • 2019-04-02
    相关资源
    最近更新 更多