允许布局占据屏幕空间的百分比?

时间:2011-06-09 16:08:55

标签: android android-layout

是否可以允许布局占据屏幕空间的百分比?

我有2个布局,目前一个占用大约130个高度,这对于大多数屏幕都很好,但是当我切换到横向时,其他线性布局在某些屏幕尺寸上几乎看不到。我如何确保每个屏幕都有相同的屏幕空间?

3 个答案:

答案 0 :(得分:5)

为每个布局设置android:layout_weight的相同值。

示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" 
                  android:layout_weight="1" android:background="#FF0000" />
    <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" 
                  android:layout_weight="1" android:background="#0000FF" />
</LinearLayout>

答案 1 :(得分:1)

使用android:layout_weight,这是包含an exmaple的文档:

这是一个精简的例子:

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    >
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="0dip"
        android:layout_weight="0.5"
    />
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="0dip"
        android:layout_weight="0.5"
    />
</LinearLayout>

这将使2个内部LinearLayouts中的每一个占据屏幕的一半。

答案 2 :(得分:0)

在您想要占用相等空间的每个项目中设置android:layout_weight = "1"。这将使其适用于任何大小的屏幕。

android:layout_width设置为相同的值将不适用于所有屏幕尺寸。

...但是,无论屏幕尺寸如何,使用layout_weight都会自动平均分配每个

相关问题