android - 了解安卓 :layout_weight

标签 android xml android-layout android-studio android-layout-weight

为什么下面的 list 只显示第二个 TextView(红色)?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="11111"
        android:background="#00FF00" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0"
        android:background="#FF0000"
        android:text="00000"/>
</LinearLayout>

我知道如果我设置 android:layout_height="0px" 它只会显示第一个 TextView(绿色),我理解这种行为。

但为什么当我设置 android:layout_height="match_parent" 时,第一个 TextView 完全从屏幕上消失了。

最佳答案

来自 https://developer.android.com/guide/topics/ui/layout/linear.html

它消失了,因为它的第二个获得了给定的完整空间

android:layout_weight="0"

android:layout_height="match_parent"** 如上面的链接所述。

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.

For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured.

If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.

关于android - 了解安卓 :layout_weight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39508909/

相关文章:

android - 是否必须使用延迟的 Facebook 深层链接来跟踪应用程序安装

Android动态创建按钮并填充布局

android - 使用 ViewPager 的带有图标的选项卡,如何布局它们?通过附加一些布局文件

android - 使 Android 按钮宽度相同时出现奇怪的视觉问题

javax.servlet.ServletException : Could not resolve view with name 'index.html' in servlet with name 'dispatcher'

java - 格式化包含日期的字符串

java - 从 android 发送时 token 显示已过期

android - Android 上的 Card.io 在 Release模式下不显示扫描选项

xml - XSLT - 需要复制多个命名空间

java - 在 java 中查询 xml 的最快方法