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

我想为一个包含一些按钮的 LinearLayout 定义一个百分比宽度(70%) ,这样我就可以将它居中,这样子按钮就可以填充 _ father。下面这张照片显示了我的意思:

example

My current layout looks like this:

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

我引用的 LinearLayoutim 具有 id: lineLayout1。我如何做到这一点?

196483 次浏览

不能在 XML 中使用百分比来定义宽度/高度/边距/... 。但是您想要使用的是“ weight”属性,这是,IMO,最相似的东西。

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

必须设置元素的权重属性。创建三个 RelativeLayout 作为 LinearLayout 的子元素,并设置权重为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 指出权重可以是小数而不是整数。

希望这个能帮上忙

<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)设置布局宽度为“0” (2)将 lay_ height 设置为. xx (%)

* < em > 可以使用 ABC0属性。如果您想取父宽度的70% ,您必须设置子 ABC1属性值0dp,如 android:layout_width="0dp" *

我认为艾米亚姆的方法是正确的。但也同意 Simon Veloper (Emiam 回答的评论员之一) : 当我们需要70% 宽度的按钮时,我们应该在线性布局中使用水平方向,并将每个相对布局的宽度设置为0,按指定的重量设置 所以我建议这么做:

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

我解决了一个类似的问题,像这样在 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>

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

我知道另一个办法,对付重量的办法:

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

作为2015年安卓系统的最新更新,谷歌加入了百分比支持库

Support: cent: 23.1.0支持率: 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>

Google introduced new API called 百分比相对布局

添加编译依赖项,如

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

在那个百分比相对布局是什么,我们可以做的百分比布局

使用新的百分比支持库

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>

了解更多信息 Tutorial1 教程2 Tutorial3