Android布局权重分布

Android布局权重分布,android,android-layout,user-interface,Android,Android Layout,User Interface,我正在做这件事。这是关于Android用户界面的 作为课程的一部分,我使用了XML可视化工具 现在,在实验之后,我输入了以下代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="ma

我正在做这件事。这是关于Android用户界面的

作为课程的一部分,我使用了XML可视化工具

现在,在实验之后,我输入了以下代码

<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\u weight,整个父布局将按照优先级进行划分并相应地分配,如果是这样,则在图像和“You's invested!”之间分配后的空间应该只有足够的剩余空间,以填写“海滩篝火”

那为什么在“海滩篝火”下面有一个空的地方呢

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

更新
当我在“海滩篝火”中添加android:layout_weight=“0”时,下面没有空白。有人能解释为什么会发生这种情况,以及为什么在前面的案例中会有空白吗。这是使用的代码

以前

之后

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


使用下面的示例理解它

权重定义与
线性布局中的其他视图相比,视图将消耗多少空间

当您希望将一个组件与另一个组件进行比较时,将使用权重

关键属性

  • 是所有子视图权重的总和。如果未指定
    权重总和
    ,系统将自行计算所有权重之和

  • 指定总权重和之外的空间量 小部件将占用空间

代码:

<?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>
现在你问如果你没有给android:layout\u权重,默认权重是零。零表示视图将不显示,空的空间将保留在那里

既然你不相信你能读懂


编辑:2

既然你这么说,我就经历了你用的 你有没有注意到

“第6行:此处不支持属性
android:weight\u sum
。”

底部的东西

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

现在,如果您想要确认实际发生了什么,那么也可以查看一下android studio/eclipse,它们是IDES

这是安卓工作室

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

可疑大小:这将使视图不可见

因为没有
布局,所以给出了权重,我们添加了0高度


现在你可以接受答案:)

你没有在海滩文本视图的篝火中使用重量。如果你使用它,它就会工作。将height=0dp和weight=1add
android:layout\u weight=“1”
属性放入第二个文本视图yes,但默认情况下,布局权重值为0。是的,我知道身高和体重的0dp比1有效。我们想知道的是空间是如何在父布局中分布的。为什么在“海滩篝火”下面有空间@Srinadh kch你现在明白了吗?可能是Ok的复制品我把高度设为0dp部分,但是你能详细说明一下为什么在“海滩篝火”下面有空间吗。谢谢:)如果您想知道上面的图像是如何显示的,它会出现在左下方的可用图像中。@Srinadh将您的
LinearLayout
android:weightSum=3
,并在两个视图上使用
android:layou weight=“1”
,请参见(我在根目录中添加了行android:weightSum=“4”,甚至尝试了3 t,但仍然保持不变。)(海滩篝火下有空间。我还为海滩篝火设置了android:layout\u height=“0dp”,但仍然会显示空间。如果我在“海滩篝火”中设置了android:layou weight=“1”然后,所有这些子项将平均分配空间。当权重为0(默认值)时,我无法获得零件下方的为什么空白空间。
<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>