Android布局百分比

时间:2014-03-25 13:08:58

标签: android android-layout layout android-xml

你好,我是Android的新手,我的英语很糟糕。我试试看。 我想将我的布局分成3种不同的尺寸。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/txt"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:text="Esto es una mierda de Relative Layout" />
        </RelativeLayout>

        <RelativeLayout 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2">
            <Button
                android:id="@+id/btn1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="bottom"
                android:text="Button" />
        </RelativeLayout>
        <RelativeLayout 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/btn"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="top"
                android:text="Button" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

我的代码应显示三列,中间列是侧列大小的两倍。如果我使用相同的布局权重设置所有布局,它们都会正确显示,但大小相同。

但是,当我使用不同大小的值(在布局权重中)设置中心列时,它会从屏幕上消失。

如何在不同大小的三列中显示布局?

如果有人可以帮助我。谢谢你的阅读。

4 个答案:

答案 0 :(得分:2)

使用这种结构:

    <!--  parent layout-->

    <LinearLayout
        android:weightSum="1">

         <!-- children layouts -->

        <RelativeLayout
             android:weight="0.25"
         ></RelativeLayout>

       <RelativeLayout
             android:weight="0.5"
         ></RelativeLayout>

       <RelativeLayout
             android:weight="0.25"
         ></RelativeLayout>

    </LinearLayout>

答案 1 :(得分:1)

这是您修改过的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/txt"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Esto es una mierda de Relative Layout" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2" >

            <Button
                android:id="@+id/btn1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="bottom"
                android:text="Button" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <Button
                android:id="@+id/btn"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="top"
                android:text="Button" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

使用match_parent而不是fill_parent。你曾为API 8编写程序 - 。

答案 2 :(得分:1)

这样做:

将相应的宽度/高度设置为0dp非常重要。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="4" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="column1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:text="column2" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="column3" />
</LinearLayout>

答案 3 :(得分:1)

enter image description here

    <LinearLayout
        android:id="@+id/stupid_android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <View
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:background="#FF0000"
            android:layout_weight="1" />

        <View
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:background="#0000FF"
            android:layout_weight="2" />

        <View
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:background="#00FF00"
            android:layout_weight="1" />

    </LinearLayout>


</LinearLayout>