为LinearLayout定义百分比宽度?

125
我想定义一个包含一些按钮的LinearLayout百分比宽度(70%),以便我可以将其居中,并且子按钮可以fill_parent。这是一个显示我的意思的图片:

example

我的当前布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id="@+id/layoutContainer" android:orientation="vertical">
    <LinearLayout android:layout_width="fill_parent"
        android:id="@+id/barContainer" android:orientation="horizontal"
        android:layout_height="40dp" android:background="@drawable/titlebackground">
        <ImageView android:id="@+id/barLogo" android:src="@drawable/titlelogo"
            android:layout_gravity="center_vertical" android:adjustViewBounds="true"
            android:layout_height="25dp" android:layout_width="wrap_content"
            android:scaleType="fitXY" android:paddingLeft="5dp"></ImageView>
    </LinearLayout>
    <TextView android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:gravity="center_horizontal"
        android:id="@+id/searchTip" android:text="@string/searchTip"
        android:paddingTop="10dp" android:paddingBottom="10dp"></TextView>
    <LinearLayout android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="Button" android:id="@+id/button1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:layout_width="wrap_content" android:id="@+id/button2" android:layout_height="wrap_content" android:text="Button"></Button>
        <Button android:layout_width="wrap_content" android:id="@+id/button3" android:layout_height="wrap_content" android:text="Button"></Button>
    </LinearLayout>
</LinearLayout>

我要引用的LinearLayout的id是:linearLayout1。我该怎么做?


赞赏这张出色的图表,高排名问题所缺乏的。 - Roboprog
任何人想要了解如何进行此操作的演示,请访问 http://goo.gl/s9mp2W。 - Nitesh Tiwari
10个回答

155

你需要设置元素的权重(weight)属性。在你的LinearLayout中创建三个RelativeLayout作为子元素,并设置0.15、0.70、0.15的权重值。然后将你的按钮添加到第二个RelativeLayout(即权重为0.70的那个)。

像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id="@+id/layoutContainer" android:orientation="horizontal">
    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.15">
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.7">
        
        <!-- This is the part that's 70% of the total width. I'm inserting a LinearLayout and buttons.-->   
            <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">
                
                <Button 
                    android:text="Button1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
                <Button
                    android:text="Button2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
                <Button
                    android:text="Button3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
                
            </LinearLayout>
        <!-- 70% Width End-->
        
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.15">
    </RelativeLayout>
</LinearLayout>
为什么权重是0.15、0.7和0.15呢?因为总权重是1,0.7是总权重的70%。
结果:

enter image description here

编辑:感谢@SimonVeloper指出方向应该是水平而不是垂直,并且感谢@Andrew指出权重可以是小数而不是整数。


3
那很"hackish"但也相当聪明,谢谢! - soren.qvist
1
在第一个按钮上,使用android:layout_alignParentTop="true",然后android:layout_marginTop="50dp"。然后在第二个按钮上,您可以使用android:layout_below="@+id/firstbutton"。以此类推。 - Emir Kuljanin
13
您可以使用小数来设置权重:0.15、0.7、0.15。 - Andrew Mackenzie
3
在垂直方向上,重量不应该与高度有关吗?在水平方向上,重量不应该与宽度有关吗? - Apperside
3
您可以将另外两个不可见的Relative Layout替换为View,并将layout_width/height设置为0dp,将visibility设置为invisible。这样可能会更节省成本。 - Helin Wang
显示剩余2条评论

42

我知道另一种解决方案,它可以根据权重进行操作:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent" 
              android:weightSum="10" 
              android:gravity="center_horizontal">

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" 
            android:layout_weight="7">
    </LinearLayout>
</LinearLayout>

1
我不知道为什么它的评分这么低,我猜这比被接受的解决方案好多了! - deathangel908
2
@deathangel908 同意!而且更加简洁! - KMC
1
它看起来很干净,但对我来说不起作用:内部LinearLayout占据了整个屏幕的宽度(至少在布局编辑器预览中是这样)。你是不是想让内部LinearLayout有android:layout_width="0dp"?文档指出这样做(https://developer.android.com/guide/topics/ui/layout/linear.html#Weight),并且在布局编辑器预览中对我有效。 - LarsH
3
这是一个更加清晰简洁的解决方案。然而,在嵌套布局中,应该使用layout_width="0dp"代替layout_width="wrap_content" - Ramashish Baranwal
简单而好的解决方案 - Gary Chen

29
希望这可以帮助。
<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="horizontal">

    <LinearLayout android:layout_width="0dip"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:id="@+id/linearLayout_dummy1" android:layout_weight=".15">
    </LinearLayout>


    <LinearLayout android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:orientation="vertical"
        android:layout_width="0dip" android:layout_weight=".7">
        <Button android:text="Button" android:id="@+id/button1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_gravity="center">
        </Button>
        <Button android:layout_width="wrap_content" android:id="@+id/button2"
            android:layout_height="wrap_content" android:text="Button"
            android:layout_gravity="center"></Button>
        <Button android:layout_width="wrap_content" android:id="@+id/button3"
            android:layout_height="wrap_content" android:text="Button"
            android:layout_gravity="center"></Button>
    </LinearLayout>

    <LinearLayout android:layout_width="0dip"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:id="@+id/linearLayout_dummy2" android:layout_weight=".15">
    </LinearLayout>

</LinearLayout>

(1)将layout_width设置为“0dip”

(2)将layout_height设置为.xx%(您想要的百分比)


4
阅读并尝试了许多不同的选项后,我相当确信这是最佳答案。顺便说一句,安卓布局非常糟糕!无法相信他们不接受相对(%)值!感谢这个答案。 - Clint Eastwood
你确定要设置 layout_height (2) 吗? - skywall
救命稻草。适用于C# Xamarin axml布局。 - nishantvodoo
太棒了!这对我来说像个冠军一样运作,让我能够继续使用LinearLayouts。 - Alyoshak

7

我认为Emiam的方法是正确的。但也同意Emiam回答评论者Simon Veloper的观点:当我们需要按钮占据70%的宽度时,应该使用水平方向的LinearLayout,并将每个RelativeLayout的宽度设置为0dip,权重设为指定值。

因此,我建议使用以下版本:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:id="@+id/layoutContainer" android:orientation="horizontal">
    <RelativeLayout
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="3">
    </RelativeLayout>
    <RelativeLayout
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="14">

            //This is where you add buttons. You can make them "fill_parent".
    </RelativeLayout>
    <RelativeLayout
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="3">
    </RelativeLayout>
</LinearLayout>

5
作为2015年安卓的最新更新,谷歌已经包含了百分比支持库。

com.android.support:percent:23.1.0

你可以参考这个网站,了解如何使用它的示例。

https://github.com/JulienGenoud/android-percent-support-lib-sample

Gradle:

dependencies {
    compile 'com.android.support:percent:22.2.0'
}

在布局中:
<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/top_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:background="#ff44aacc"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="70%" />

    <View
        android:id="@+id/top_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/top_left"
        android:background="#ffe40000"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="30%" />


    <View
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/top_left"
        android:background="#ff00ff22"
        app:layout_heightPercent="80%" />
</android.support.percent.PercentRelativeLayout>

2
请将以下与编程有关的内容从英语翻译成中文。 仅返回已翻译的文本:此处高度不鼓励仅链接答案。只需将链接作为参考添加。 - Anirudh Sharma

4

使用新的百分比支持库

compile 'com.android.support:percent:24.0.0'

请看下面的例子。
<android.support.percent.PercentRelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentRelativeLayout>

更多信息请查看 教程1 教程2 教程3


3

在XML中,无法使用百分比定义宽度/高度/边距等。但是你可以使用“weight”属性,这是我认为最相似的东西。

另一种方法是在代码中充气布局后以编程方式设置大小,通过获取屏幕大小并计算所需的边距。


我能用“height”来做这件事吗? - Francisco Corrales Morales

2

*您可以使用layout_weight属性。如果您想占用父宽度的70%,则必须将子layout_width属性值设置为0dp,例如android:layout_width="0dp"*


1

我解决了一个类似的问题,只需对LinearLayout应用一些内边距,就像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_background"
    android:padding="35dip">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/black">

</RelativeLayout>
</LinearLayout>

这可能无法给你一个确切的百分比,但可以轻松地分级并避免额外不必要的布局元素。

1
谷歌推出了一个新的API,名为PercentRelativeLayout
添加编译依赖项:
compile 'com.android.support:percent:22.2.0'
其中,PercentRelativeLayout是我们可以进行百分比布局的内容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接