gpt4 book ai didi

java - 线性布局对于空 View 的行为如何?

转载 作者:行者123 更新时间:2023-12-02 04:51:00 27 4
gpt4 key购买 nike

在线性布局中,嵌套的小部件根据其宽度/高度水平/垂直排列。
我的问题如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffff0000"
/>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff00ff00"
/>

</LinearLayout>

</LinearLayout>

为什么我只看到第一个 View ?我期待因为它们是wrap_content,要么都显示,要么都不显示。但我在设计预览中看到: enter image description here

此外,如果我在第一个 View 中添加 android:layout_weight="2" 并在第二个 View 中添加 android:layout_weight="1"即使我在封闭布局中添加weight_sum=3,第二个 View 也会获得 2/3 的空间

最佳答案

您只能看到一个 View ,因为您没有为 View 指定数字宽度。因此,第一个 View 占据了整个屏幕,而另一个 View 根本看不到 - 它位于右侧,红色 View 旁边。

如果您希望它们具有相同的宽度,只需给每个它们相同的权重,这样它们就会彼此相等并且适合屏幕高度或宽度,具体取决于屏幕的布局方向容器。

来自文档:

android:weightSum - Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children.

关于java - 线性布局对于空 View 的行为如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29241110/

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