<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:layout_weight="2">
<TextView
android:id="@+id/centerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
android:textSize="28sp"
android:text="HALLO ALLE"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/centerText"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:layout_weight="8"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Btn 1"
android:layout_weight="2"/>
</LinearLayout>
大家好,这是我的代码,我对此有疑问,我在第一个weightSum
设置了LinearLayout
我设置了weightSum
为10,{{1}有2/10而另一个(RelativeLayout
)有8/10,正如我在IDE(Android Studio)的预览中看到的那样,LinearLayout
在这里是更大的一个&#39; s我的问题:
价值较低的RelativeLayout
会更大吗?
答案 0 :(得分:1)
较大的权重值允许它展开以填充父视图中的任何剩余空间
这里的关键部分是&#34;剩余空间&#34;
这意味着,如果RelativeLayout
高度超过其父高度的一半,则它将大于LinearLayout
,无论您设置其android:layout_weight
属性的值是什么
答案 1 :(得分:1)
不,layout_weight
的较大值应该是较大的值。
使用layout_weight
时,您应该将高度或宽度设置为0dp;在垂直布局中,您处理高度,在水平宽度处理它。
因此,在您的情况下,您的根布局是垂直LinearLayout。在其中,您的相对布局权重为&#39; 2&#39;对于权重为&#39; 8&#39;的LinearLayout,应该有android:layout_height="0dp"
和相同的内容。
答案 2 :(得分:0)
我相信你正在寻找这样的东西:(顶部占20%,底部占80%)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="5"
android:orientation="vertical">
<!-- Specify height as 0dp-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#000000"
android:layout_weight="1">
<TextView
android:id="@+id/centerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
android:textSize="28sp"
android:text="HALLO ALLE"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/centerText"
android:text="Button top"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<!-- Specify height as 0dp -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Btn 1"/>
</LinearLayout>
</LinearLayout>
处理布局权重时,当父项方向为水平或垂直时,您可以为加权项目的布局宽度或高度提供0dp。
答案 3 :(得分:0)
当使用权重时,最好使用相应的一个到0dp,然后使用layout_weight ....如
如果是android:orientation="horizontal"
使用以下代码
android:layout_width="match_parent"
android:layout_height="0dp"
如果是android:orientation="vertical"
使用以下代码
android:layout_width="0dp"
android:layout_height="match_parent"