如何让视图在我的 Android 应用程序中填满其父级的整个宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2405614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:44:15  来源:igfitidea点击:

How do I make Views fill the full width of their parent in my Android app?

androidlayoutwidthparent

提问by Alexander Trauzzi

I have the following layout defined for one of my Activities:

我为我的一项活动定义了以下布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <TableRow android:id="@+id/TableRow01" android:layout_height="wrap_content" android:layout_width="fill_parent">
        <EditText android:text="Resource Name" android:id="@+id/ResourceName" android:lines="1" android:isScrollContainer="false"></EditText>
    </TableRow>
    <TableRow android:id="@+id/TableRow02" android:layout_height="wrap_content" android:layout_width="fill_parent">
        <Button android:id="@+id/Tile" android:text="Tile"></Button>
    </TableRow>
</TableLayout>

The layout renders almost correctly, the only problem is that my text box and my button aren't occupying the full width of their respective rows.

布局几乎正确呈现,唯一的问题是我的文本框和我的按钮没有占据它们各自行的全宽。

I've tried specifying fill_parentfor the layout width properties, but to no avail, they still only occupy roughly half of the screen.

我试过指定fill_parent布局宽度属性,但无济于事,它们仍然只占屏幕的大约一半。

Documentation overall for Android so far has been great, but there are a few scenarios like this one where I hit an invisible wall! Thanks for all the help!

到目前为止,Android 的整体文档非常好,但有一些场景就像我碰到了一面隐形墙!感谢所有的帮助!

回答by Rahul

Try using android:stretchColumns="0"(when you define the TableLayout) or what ever is the index of the column that you would want to stretch.

尝试使用android:stretchColumns="0"(当您定义TableLayout)或您想要拉伸的列的索引。

For more information - http://developer.android.com/guide/tutorials/views/hello-tablelayout.html

有关更多信息 - http://developer.android.com/guide/tutorials/views/hello-tablelayout.html

回答by macfnl

Use android:layout_width="match_parent"in your tableRow views.

使用android:layout_width="match_parent"您的tablerow的意见。

Now you have different possibilities in your TableLayout seting the parameter android:stretchColumnsto:

现在,您可以在 TableLayout 中将参数设置android:stretchColumns为:

Value "*"views will have same width and will fill the row occupying the same space

Value "*"视图将具有相同的宽度并将填充占据相同空间的行

Value "0" or other integer-> First or element corresponding with the integer in a row will fill the row. The other view will have width in a same way of wrap_content

Value "0" or other integer-> 第一个或与一行中的整数对应的元素将填充该行。另一个视图的宽度与 wrap_content 相同

Example with a tablelayout of 3 tableRow with 2 views each:

具有 3 个 tableRow 的表格布局的示例,每个表格有 2 个视图:

Without setting android:stretchColumns:

没有设置android:stretchColumns

[|TextView|EditText|          ]
[|TextView|EditText|          ]
[|TextView|EditText|          ]

android:stretchColumns="*"

机器人:stretchColumns="*"

[|  TextView    |  EditText  |]
[|  TextView    |  EditText  |]
[|  TextView    |  EditText  |]

android:stretchColumns="0"

机器人:stretchColumns="0"

[|    TextView      |EditText|]
[|    TextView      |EditText|]
[|    TextView      |EditText|]

android:stretchColumns="1"

机器人:stretchColumns =“1”

[|TextView|      EditText    |]
[|TextView|      EditText    |]
[|TextView|      EditText    |]

回答by Sephy

Try this in the definition of your TableRow :

在你的 TableRow 的定义中试试这个:

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">