Android的layout_weight行为举止 [英] Android layout_weight comportment

查看:141
本文介绍了Android的layout_weight行为举止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 layout_weight ,以适应设备的所有尺寸,以建立一个Android的布局,我有一些麻烦了解其行为举止。

I am trying to build an android layout using layout_weight to fit all sizes of devices and I have some trouble understanding its comportment.

我注意到,改变了 layout_width / layout_height 影响了 layout_weight 的行为举止,但我不知道怎么样。

I noticed that changing the layout_width/layout_height influenced the layout_weight comportment, but I don't understand how.

比方说,比如我想有一个垂直的LinearLayout 三胆分为的LinearLayout 使得一个在顶部和一个在底部被填充25%的画面,而在中间的50%,而且不应该依赖的内布局的内容。 (如果内容的内部的LinearLayout 太大了,它不应该转嫁他人的布局)

Let's say for example I want to have a vertical LinearLayout divided in three inners LinearLayout such that the one at the top and the one at the bottom are filling 25% of the screen, and the on in the middle 50%, and that should not depend of the content of the inner layouts. (If the content of a inner LinearLayout is too big, it should not shift the others layouts)

在为了做到这一点,我应该设置的内的LinearLayout layout_height 属性 FILL_PARENT WRAP_CONTENT

In order to do this, should I set the layout_height attribute of the inner LinearLayout to fill_parent or to wrap_content?

谢谢!

编辑:它看起来像layout_weight成反比尺寸布局将occupate

It looks like the layout_weight is inversely proportional to the size the layout will occupate.

3的例子:

重量1/1/1(工作如我所料)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#FF0000"/> //RED
        <LinearLayout
            android:id="@+id/layout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#00FF00"/> //GREEN
        <LinearLayout
            android:id="@+id/layout3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#0000FF"/> //BLUE
</LinearLayout>

结果:

Results:

重量1/2/1(为什么,为什么?)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#FF0000"/> //RED
        <LinearLayout
            android:id="@+id/layout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"
            android:background="#00FF00"/> //GREEN
        <LinearLayout
            android:id="@+id/layout3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#0000FF"/> //BLUE
</LinearLayout>

结果:

Results:

**重量3/2/3(我entended与1/2/1做):

**Weight 3/2/3 (What I entended to do with 1/2/1):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:background="#FF0000"/> //RED
        <LinearLayout
            android:id="@+id/layout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"
            android:background="#00FF00"/> //GREEN
        <LinearLayout
            android:id="@+id/layout3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:background="#0000FF"/> //BLUE
</LinearLayout>

结果:

Results:

推荐答案

您应该设置三个内部布局的layout_height为FILL_PARENT,然后改变自己的权重,使它们看起来像你想要的。

You should set the layout_height of all three inner layouts to "fill_parent" and then change their weights to make them look like you want.

这篇关于Android的layout_weight行为举止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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