我使用了布局权重的线性布局。我希望第一个视图占据屏幕的10%,第二个视图占据80%,第三个视图占屏幕的10%。像这样:
----------
view_1: 10% height
----------
view_2: 80% height
----------
view_3: 10% height
----------
我试图通过这种布局来实现结果:
<?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:orientation="vertical"
android:weightSum="100" >
<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10"
android:background="@color/gold" />
<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="80"
android:background="@color/orange" />
<View
android:id="@+id/view3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10"
android:background="@color/blue" />
</LinearLayout>
但我得到了这个结果:
只有当我在所有视图上将权重设置为33.3333时,它才会按预期工作。即所有观点都有相同的高度。任何想法如何解决这个问题?
答案 0 :(得分:1)
替换:
android:layout_height="match_parent"
使用:
android:layout_height="0dp"
在所有三个地点。
答案 1 :(得分:1)
<?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:orientation="vertical">
<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/gold" />
<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="@color/orange" />
<View
android:id="@+id/view3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/blue" />
第二个视图占据空间的一半,其余视图共享剩余空间