【问题标题】:Nested weights in appwidget layout应用小部件布局中的嵌套权重
【发布时间】:2012-07-19 01:34:52
【问题描述】:

我有以下布局文件:

<?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 时,会出现“无法加载小部件”的消息。我认为这是因为嵌套的权重(不确定)。

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

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

有谁知道为什么会发生这种情况以及如何实施我的想法? Christopher 的图片展示了我的想法。

【问题讨论】:

  • 你能画一个你想要的简单图像吗?我们会更容易为您提供帮助。

标签: android android-layout


【解决方案1】:

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

我已更改您的 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,则放置在“中间”的所有视图将具有统一的高度。

【讨论】:

    【解决方案2】:
    <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>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 2018-03-03
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多