LinearLayout 权重工作错误
LinearLayout Weight works wrong
嘿,我创建了一个带有 2 个 RelativeLayout 的 LinearLayout。一个 RelativeLayout 的权重为 8,另一个权重为 2。当我启动我的应用程序时,应使用 80% 屏幕的布局占用 20% 的屏幕,而另一个应使用 20% 屏幕的 RelativeLayout 占用 80屏幕百分比。
这是我的代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimary"
android:layout_weight="8">
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:layout_weight="2">
</RelativeLayout>
</LinearLayout>
在 LinearLayout 上使用权重时,必须注意 weightSum
值。默认情况下它是 1,如果需要,在 layout_weight
中你可以有浮点值。子项应指定 layout_weight
值,并且 layout_width
或 layout_height
必须是 0dp
,具体取决于父项的方向。
您没有自己修复它有点奇怪,因为 Android Studio 会自动警告您。
无论如何,您可以在下面找到固定布局。让我知道它是否适合你。
<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="0dp"
android:background="@color/colorPrimary"
android:layout_weight="8">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#FFFFFF"
android:layout_weight="2">
</RelativeLayout>
</LinearLayout>
另外,我把fill_parent
换成了match_parent
,因为如果你用了,就说明你是在开发API Level 8+,所以你应该用"match_parent"(它已被替换,但值保持不变“-1”)。
嘿,我创建了一个带有 2 个 RelativeLayout 的 LinearLayout。一个 RelativeLayout 的权重为 8,另一个权重为 2。当我启动我的应用程序时,应使用 80% 屏幕的布局占用 20% 的屏幕,而另一个应使用 20% 屏幕的 RelativeLayout 占用 80屏幕百分比。
这是我的代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimary"
android:layout_weight="8">
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:layout_weight="2">
</RelativeLayout>
</LinearLayout>
在 LinearLayout 上使用权重时,必须注意 weightSum
值。默认情况下它是 1,如果需要,在 layout_weight
中你可以有浮点值。子项应指定 layout_weight
值,并且 layout_width
或 layout_height
必须是 0dp
,具体取决于父项的方向。
您没有自己修复它有点奇怪,因为 Android Studio 会自动警告您。
无论如何,您可以在下面找到固定布局。让我知道它是否适合你。
<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="0dp"
android:background="@color/colorPrimary"
android:layout_weight="8">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#FFFFFF"
android:layout_weight="2">
</RelativeLayout>
</LinearLayout>
另外,我把fill_parent
换成了match_parent
,因为如果你用了,就说明你是在开发API Level 8+,所以你应该用"match_parent"(它已被替换,但值保持不变“-1”)。