如何在android中将布局的宽度和高度设置为百分比?

时间:2014-03-11 11:45:51

标签: android xml layout

我需要创建horizontal layout,如下所示:Layout Pic

我可以使用android:layout_width="match_parent"设置C布局的总宽度,但我完全不知道设置其余的布局。任何帮助都非常感谢。 整个应用程序设置为仅在水平布局上运行,如果这有助于确定设置布局的方法。

2 个答案:

答案 0 :(得分:5)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 android:weightSum="100" >

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="90"
    android:orientation="horizontal"
    android:weightSum="100" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="50"
        android:background="#FF0000" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="50"
        android:background="#00FF00" >
    </LinearLayout>
 </LinearLayout>

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="10"
    android:background="#FFFF00" >
 </LinearLayout>

</LinearLayout>

答案 1 :(得分:0)

诀窍是使用具有一定重量的LinearLayout:

<?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="10">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">


        <View
            android:id="@+id/view_A"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <View
            android:id="@+id/view_B"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

    </LinearLayout>

    <View
        android:id="@+id/view_C"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="9" />

</LinearLayout>