我正在努力研究 android 布局,我想我会从一个简单的例子开始,但由于某种原因它不起作用。
我首先将布局分成 4 个 block ,并使用背景颜色来证明它按预期工作。
此代码非常适合:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:background="@android:color/holo_green_light">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:background="@android:color/white">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:background="@android:color/holo_orange_light">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:background="@android:color/holo_red_light">
</RelativeLayout>
</LinearLayout>
所以我想继续分割这些部分,并在第二个布局上添加两个新布局,每个布局占 50%(有效覆盖它)。
我已经尝试了 LinearLayout 和 RelativeLayout,但我无法正常工作。
这是我现在的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:background="@android:color/holo_green_light">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="@android:color/holo_green_dark"
android:layout_alignParentLeft="true" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="@android:color/holo_blue_dark"
android:layout_alignParentRight="true" >
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:background="@android:color/holo_orange_light">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:background="@android:color/holo_red_light">
</RelativeLayout>Layout>
</LinearLayout>
我认为问题出在子 LinearLayouts 中的 match_parent,但问题是如果我使用 wrap_content,则没有内容,因此它们会消失。
最佳答案
请检查更新的 xml。它会很好地工作
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:background="@android:color/holo_green_light">
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:weightSum="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="@android:color/holo_green_dark"
android:layout_alignParentLeft="true" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="@android:color/holo_blue_dark"
android:layout_alignParentRight="true" >
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:background="@android:color/holo_orange_light">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:background="@android:color/holo_red_light">
</RelativeLayout>
</LinearLayout>
我添加了 weightsum 标签并将您的一个相对布局更改为线性布局
关于android - 基于百分比的android布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29763757/