Android 布局权重分布

标签 android android-layout user-interface

我正在做 following course at udacity .这是关于 Android 用户界面的。

作为类(class)的一部分,我使用了 XML 可视化工具。

http://labs.udacity.com/android-visualizer/#/android/linear-layout-weight

现在,在进行实验时,我输入了以下代码

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

    <ImageView
        android:src="@drawable/ocean"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:scaleType="centerCrop"
        android:layout_weight = "1"/>

    <TextView
        android:text="You're invited!"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:textColor="@android:color/white"
        android:textSize="54sp"
        android:layout_weight = "1"
        android:background="#009688" />

    <TextView
        android:text="Bonfire at the beach"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="34sp"

        android:background="#009688" />

</LinearLayout>

据我了解,通过使用 android:layout_weight 整个父布局按优先级划分并相应分配,如果是这样,分配后的空间在图像和“你被邀请了!”剩下的空间应该只够填写“海边的篝火”。

那为什么“海边的篝火”下面是空的呢?

(如果可能的话,任何人都可以解释控制如何在 XML 代码之间流动)。

更新
当我在“Bonfire at the beach”中添加 android:layout_weight = "0"时,下面没有空白区域。任何人都可以解释为什么会发生这种情况以及为什么在前一个案例中有空间。这是使用的代码。

之前 enter image description here

之后

enter image description here

甚至尝试将高度设置为 0dp

enter image description here

最佳答案

使用下面的例子理解它

权重定义一个 View 与 LinearLayout 中的其他 View 相比将占用多少空间。

当你想给一个组件相对于其他组件特定的屏幕空间时,使用权重。

关键属性:

  • weightSum是所有 subview 的权重总和。如果您不指定weightSum,系统将自行计算所有权重的总和。

  • layout_weight指定总重量和的空间量 小部件将占用。

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="4">

    <EditText
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Type Your Text Here" />

    <Button
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Text1" />

    <Button
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Text1" />

</LinearLayout>

输出是:

enter image description here

现在即使设备尺寸更大,EditText 也会占用屏幕空间的 2/4。因此,您的应用在所有屏幕上的外观都是一致的。

[这个如果在你编辑你的问题之前可能现在不相关了

现在在你的Bonfire at the beach 中没有重量和它的wrap_content 所以没有它会占用剩余空间的受让人!添加后可以保留的空间会因设备的屏幕尺寸而异]

注意: 这里 layout_width 保持 0dp 因为 widget 空间是水平划分的。如果小部件要垂直对齐,layout_height 将设置为 0dp

这样做是为了提高代码的效率,因为在运行时系统不会尝试分别计算宽度或高度,因为这是由权重管理的。如果您改为使用 wrap_content,系统将尝试先计算宽度/高度,然后再应用导致另一个计算周期的重量属性。


让我们转向您的 XML 看看我是如何使用它们的

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

    <ImageView
        android:src="@drawable/mc"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        android:layout_weight = "1"/> //<-------------------

    <TextView
        android:text="You're invited!"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:textColor="@android:color/white"
        android:textSize="54sp"
        android:layout_weight = "1" //<-------------------
        android:background="#009688" />

    <TextView
        android:layout_weight = "1" //<------------------- if you remove this , this text view will be gone cuz its 0 by default
        android:text="Bonfire at the beach"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:textColor="@android:color/white"
        android:textSize="34sp"
        android:background="#009688" />

</LinearLayout>

现在你问如果你没有给出 android:layout_weight ,默认权重是怎么办。零表示不会显示 View ,空白区域将保留在那里

既然你不相信你可以阅读documentation enter image description here


编辑:2

既然你这么说,我就通过了android-visualizer你用的 你有没有注意到这个……

"Line 6: The attribute android:weight_sum is not supported here."

底部的东西。

这意味着他们没有提供调整布局边界的功能。它只是一个简单的在线工具。我是 不是说不推荐使用,只是我个人的想法是,你 如果你使用它,就无法触及 android 的深度。

现在,如果您想确认实际发生了什么,请查看 android studio/eclipse 以及读取的 IDE s

这是安卓工作室

enter image description here

你能看到任何 View 包含你的文本“海滩篝火”吗?不 相反,a.studio 在 XML 中显示一条红线。

Suspicious size: this will make the view invisible

因为没有给出layout_weight,我们加了0高度

现在你可以接受答案了:)

关于Android 布局权重分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41333680/

相关文章:

java - 标记未显示在谷歌地图上

java - 谁能解释我 “GeofenceErrorMessages”类发生了什么?

java - 按钮只打印一个数字

android - 尝试在空对象引用上调用虚拟方法 'void android.widget.Editor$InsertionPointCursorController.show()'

android - 使用退出按钮关闭应用程序

javascript - 用于获取用户输入并将其传递给批处理文件的 GUI

python - 如何在我自己的方法中使用`wx.ProgressDialog`?

android - 自动调整两个 TextView 的大小以显示尽可能多的内容

java - 在 Intellij 中添加 Maven 依赖

android - 在 Android 上一次触摸两个 View