gpt4 book ai didi

android - LinearLayout 中具有相同权重的两个 View

转载 作者:行者123 更新时间:2023-11-29 16:07:02 24 4
gpt4 key购买 nike

在下面的代码中,我在 LinearLayout 中使用了 layout_weight=1 和 layout_width(或 layout_height)等于 0 的组合。一个问题是为什么在第二个 LinearLayout 中,第二个按钮和第一个按钮占用的空间不相等?它们具有相同的权重 (layout_weight=1),因此它们应该具有相同的空间。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.0"
android:text="wreoiwe roiwej roiwejr weoirjweoirjwoeirjoweijrowerjowejorjweoirjwoeiwoi" >
</TextView>

<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<Button
android:id="@+id/Button01"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="Submit" >
</Button>

<Button
android:id="@+id/Button02"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="Cancelrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr" >
</Button>
</LinearLayout>
</LinearLayout>

最佳答案

这是因为第二个 LinearLayoutwrap_contentlayout_weight 不是这样工作的。

来自文档:

A larger weight value allows it to expand to fill any remaining space in the parent view.

如果您的 LinearLayout 位于 wrap_content 且未填满整个屏幕,则父 View 中不再有“剩余空间”。一切都只是它应该的那么大。它不会扩展较小的按钮,因为父级位于 wrap_content 时没有更多空间可以扩展。

您可以通过将第二个 LinearLayout 更改为 match_parent 来对此进行测试,您会看到每个按钮都占据了屏幕的一半。

关于android - LinearLayout 中具有相同权重的两个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17389081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com