参见英文答案 Set ImageView’s max width as a percent of its parent’s width1个 我想为包含一些按钮的LinearLayout定义百分比宽度(70%),以便我可以将其居中,以便子按钮可以fill_parent.这是一张显示我的
我想为包含一些按钮的LinearLayout定义百分比宽度(70%),以便我可以将其居中,以便子按钮可以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.我该怎么做呢?
您必须设置元素的权重属性.为LinearLayout创建三个RelativeLayouts作为子项,并设置权重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%.
结果:
编辑:感谢@SimonVeloper指出方向应该是水平的而不是垂直的,并且@Andrew指出权重可以是小数而不是整数.
