安卓复杂布局线性和相对布局

6

我需要实现如上图所示的布局,但是我不知道最佳的组合来实现所需的设计。

我正在设计7英寸平板电脑,并希望设计能够在10英寸平板电脑上很好地呈现。

我假设像1、2、3、4、5这样的布局是LinearLayouts,对吗?

*活动布局是什么?我尝试使用RelativeLayout,但我无法在布局1和2和3之间分配宽度(使用android:layout_weight)。

*我尝试使用整个活动的水平LinearLayout,但我无法正确地将标头和页脚布局添加到主水平线性布局中。

我阅读了文档和教程,但找不到这种复杂设计的线索,请帮忙。

此外,嵌套布局的性能损失是多少?

谢谢!


1
在我的经验中,嵌套布局的性能影响可以忽略不计。而且你这里并没有太多的视图,所以除非你在很多视图中有一些复杂的动画效果,否则不用担心它。 - invertigo
3个回答

14
你可以尝试像这样做,正如其他人所说,在这个级别上你不会遇到性能问题。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.2"
    android:background="@android:color/holo_orange_light"
    android:orientation="horizontal"
    android:weightSum="1" >

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="0.2"
        android:background="@android:color/black" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="0.8"
        android:background="@android:color/black" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.6"
    android:background="@android:color/holo_blue_light"
    android:orientation="horizontal"
    android:weightSum="1" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:layout_weight="0.2"
        android:background="@android:color/holo_purple"
        android:orientation="vertical"
        android:weightSum="1" >

        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight="0.2"
            android:background="@android:color/black" />

        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight="0.3"
            android:background="@android:color/black" />

        <View
            android:id="@+id/view2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight="0.5"
            android:background="@android:color/black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:layout_weight="0.4"
        android:background="@android:color/holo_red_dark"
        android:orientation="vertical"
        android:weightSum="1" >

        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight="0.33"
            android:background="@android:color/black" />

        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight="0.33"
            android:background="@android:color/black" />

        <View
            android:id="@+id/view2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight="0.33"
            android:background="@android:color/black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:layout_weight="0.4"
        android:background="@android:color/darker_gray"
        android:orientation="vertical"
        android:weightSum="1" >

        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight="0.5"
            android:background="@android:color/black" />

        <View
            android:id="@+id/view2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight="0.5"
            android:background="@android:color/black" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.2"
    android:background="@android:color/holo_green_light"
    android:orientation="horizontal"
    android:weightSum="1" >

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="0.2"
        android:background="@android:color/black" />

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="0.3"
        android:background="@android:color/black" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="0.5"
        android:background="@android:color/black" />
</LinearLayout>

</LinearLayout>

在此输入图片描述


已在我的平板电脑上进行了测试,运行良好。我将使用所需的小部件替换视图并提供反馈。我猜你的方法在更大的屏幕平板电脑上会很好用。非常感谢你所做的努力。 - Montaro
1
@Montaro 什么都没有 Montaro :-))) - Lisa Anne
为什么在ADT设计师中整个布局都被裁剪了,而不像http://img32.imageshack.us/img32/5154/h4gu.jpg那样呢?然而,在物理平板电脑上显示得很好。我使用的是eclipse/ubuntu。 - Montaro

1

你说得对,Paul。博客文章表明这是正确的方法。我会测试并反馈,但现在我很匆忙要进行实验。谢谢! - Montaro

1
你需要使用线性和相对布局的组合。为了在水平和垂直方向上分组块,可以使用LinearLayout;为了将这些组放置在一起,可以使用LinearLayout并相应地调整权重,或者使用RelativeLayout并相互设置它们。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接