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

发布于 2024-08-24 07:41:51 字数 996 浏览 4 评论 0原文

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

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

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

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

到目前为止,Android 的文档总体来说都很棒,但是有一些像这样的场景,我遇到了无形的墙!感谢您的帮助!

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_parent for the layout width properties, but to no avail, they still only occupy roughly half of the screen.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳哥哥 2024-08-31 07:41:51

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

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

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.

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

九局 2024-08-31 07:41:51

在 tableRow 视图中使用 android:layout_width="match_parent"

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

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

值“0”或其他整数 ->与行中的整数对应的第一个或元素将填充该行。另一个视图的宽度与wrap_content的宽度相同。

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

不设置android:stretchColumns

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

android:stretchColumns =“*”

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

android:stretchColumns =“0 “

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

android:stretchColumns=”1”

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

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

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

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

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

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

Without setting android:stretchColumns:

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

android:stretchColumns="*"

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

android:stretchColumns="0"

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

android:stretchColumns="1"

[|TextView|      EditText    |]
[|TextView|      EditText    |]
[|TextView|      EditText    |]
煮酒 2024-08-31 07:41:51

在 TableRow 的定义中尝试一下:

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

Try this in the definition of your TableRow :

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文