定义一个百分比宽度为的LinearLayout? [英] Defining a percentage width for a LinearLayout?

查看:122
本文介绍了定义一个百分比宽度为的LinearLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为包含一些按钮,这样我可以集中它,这样孩子的按钮可以FILL_PARENT一个LinearLayout中定义一个百分比宽度(70%)。下面这张图片展示了我的意思:

I want to define a percentage width (70%) for a LinearLayout that contains some buttons, so that I can center it and so that the child buttons can fill_parent. Here's a picture showing what I mean:

我目前的布局是这样的:

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>

本的LinearLayout IM指具有ID:linearLayout1。如何做到这一点?

The LinearLayout im referring to has the id: linearLayout1. How do I do this?

推荐答案

您必须设置元素的重量财产。创建三个RelativeLayouts为孩子你的LinearLayout,并设置权重0.15,0.70,0.15。然后,您的按钮添加到第二RelativeLayout的(一个体重0.70)。

You have to set the weight property of your elements. Create three RelativeLayouts as children to your LinearLayout and set weights 0.15, 0.70, 0.15. Then add your buttons to the second RelativeLayout(the one with weight 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%。

Why are the weights 0.15, 0.7 and 0.15? Because the total weight is 1 and 0.7 is 70% of the total.

结果:

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

Thanks to @SimonVeloper for pointing out that the orientation should be horizontal and not vertical and to @Andrew for pointing out that weights can be decimals instead of integers.

这篇关于定义一个百分比宽度为的LinearLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆