0

我有以下布局文件:

<?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" >

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/blue" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/red" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/yellow" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/blue" />

</LinearLayout>

它使用嵌套权重,我知道性能问题。
我想在 appwidget 中显示这个布局,但是当我加载 appwidget 时,会出现“无法加载小部件”的消息。我认为这是因为嵌套的权重(不确定)。

当我使用 aRelativeLayout作为基本布局时,我有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <View
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/blue" />

    <View
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/blue" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom"
        android:layout_below="@id/top" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/red" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/yellow" />
    </LinearLayout>

</RelativeLayout>

当我使用这种布局时,红色和黄色视图不显示,我猜高度保持在0dp.

我怎样才能实现我的目标?
要清楚:我想在顶部和底部有一个视图,固定高度。在它们之间,我想有几个相同高度的视图,填补空白。


编辑
所以我发现了一些东西。当您的布局中有两个级别的 ViewGroups 时,appwidget 不会显示:

<LinearLayout
    android:id="@+id/middle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp"
    android:orientation="vertical" >


</LinearLayout>

<View
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentTop="true"
    android:background="@color/teal" />

<View
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentBottom="true"
    android:background="@color/teal" />

这有效,但这不起作用:

<LinearLayout
    android:id="@+id/middle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp"
    android:orientation="vertical" >

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red" />
</LinearLayout>

<View
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentTop="true"
    android:background="@color/teal" />

<View
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentBottom="true"
    android:background="@color/teal" />

有谁知道为什么会发生这种情况以及如何实施我的想法?克里斯托弗的形象展示了我的想法。

4

2 回答 2

0

如果我正确理解您的要求,这就是您要实现的布局。

布局

我已更改您的 xml 代码来创建此布局。这里是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/red" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/white" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/red" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/white" />
    </LinearLayout>

    <View
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_alignParentTop="true"
        android:background="@color/teal" />

    <View
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_alignParentBottom="true"
        android:background="@color/teal" />

</RelativeLayout>

如果它们的权重都设置为 1,则放置在“中间”的所有视图都将具有统一的高度。

于 2012-07-19T20:36:16.427 回答
0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:id="@+id/theparent"
>
<View 
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentTop="true"
    android:background="@color/red"
    />



<View 
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentBottom="true"
    android:background="@color/red"
    />
</RelativeLayout>

这应该照顾你的页眉和页脚。告诉我更多关于你想在内容区域做什么,我也许可以帮助你

于 2012-07-19T20:44:25.443 回答