为LinearLayout定义一个百分比宽度?

我想为一个包含一些button的LinearLayout定义一个百分比宽度(70%),这样我可以居中并使子button可以fill_parent。 下面是一张照片,显示我的意思:

例

我目前的布局看起来像这样:

<?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。 我该怎么做呢?

你必须设置你的元素的重量属性。 创build三个RelativeLayouts作为您的LinearLayout的孩子,并设置权重0.15,0.70,0.15。 然后将你的button添加到第二个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%。

结果:

在这里输入图像说明

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

希望这可以帮助

 <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(所需的%)

我知道另一个解决scheme,与重量工作:

 <?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> 

我认为Emiam的方法是对的。 但也同意Simon Veloper(Emiam的回答评论员之一):当我们需要在70%的宽度的button,我们应该使用Linearlayout的水平方向,并将每个Relativelayout的宽度设置为0dip和他们的重量所以我build议版本:

 <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> 

作为2015年Android的最新更新,Google包含了百分比支持库

com.android.support:percent:23.1.0

你可以参考这个网站,例如使用它

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

摇篮:

 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> 

您无法使用XML中的百分比定义宽度/高度/边距/ …。 但是,你想要使用的是“重量”属性,即IMO,最相似的东西。

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

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

我解决了类似的问题,像这样应用一些填充到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> 

这可能不会给你一个确切的百分比,但可以很容易地gradle,并避免额外的不必要的布局元素。

Google推出了一个名为PercentRelativeLayout的新API

像编译依赖关系一样

编译“com.android.support:percent:22.2.0”

在PercentRelativeLayout是我们可以做百分比布局

使用新的百分比支持库

 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