【问题标题】:RelativeLayout behaves different as list view itemRelativeLayout 的行为与列表视图项不同
【发布时间】:2011-12-10 05:50:19
【问题描述】:

机器人们下午好!

我面临一个烦人的布局问题,经过数小时的调查后我无法解释......:/。 我创建了一个简化的测试用例,在这里展示。

我有一个简单的列表项布局。

list item http://www.freeimagehosting.net/4075a.jpg

lis item image link

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:minHeight="96dp">

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="TextView"
    android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_centerVertical="true" android:layout_centerHorizontal="true" />

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="TextView"
    android:id="@+id/textView2" android:layout_alignParentTop="true" />

<ImageView 
    android:layout_width="10dp" 
    android:layout_height="match_parent"
    android:id="@+id/imageView1" 
    android:src="@drawable/errorindicator"
    android:layout_below="@id/textView2" />

</RelativeLayout>

错误指示器是一个红色矩形,您可以在左侧的图像中看到它。 xml 看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>
    <shape shape="rectangle"  xmlns:android="http://schemas.android.com/apk/res/android">
     <solid android:color="@color/ControllingConflictColor" />
     <padding android:left="0dp" android:top="0dp" 
         android:right="0dp" android:bottom="0dp" />
    </shape>

所描述的列表项布局按预期工作。

现在我尝试用列表项填充列表视图,如上所述。结果如下所示:

list view with list items http://www.freeimagehosting.net/4cc4b.jpg

list view image link

对应的xml:

    <?xml version="1.0" encoding="utf-8"?>
    <ListView android:id="@+id/listView1" android:layout_width="fill_parent"
        android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Preview: listitem=@layout/test -->
    </ListView>

正如你在图片上看到的,红色矩形不再像它应该的那样匹配它的父级。图片是由设计师制作的,但同样的效果也在模拟器和设备。我正在为 API 级别 8 开发。

**如果有人能解释一下,为什么所描述的布局在列表视图中不能按预期工作,我会很高兴。为什么形状可绘制的行为不同? **

感谢您的宝贵时间:)

编辑:我在嵌入图像时遇到问题,我使用了链接,抱歉 ;( 编辑:添加 xml-drawable 标记。

编辑:我的示例甚至可以变得更容易。我包含了两个文本视图,因为它与我的真实布局有些相似。您可以从示例中删除两个文本视图,但仍然存在问题,即如果在列表视图中使用布局,图像视图中可绘制的形状与定义的父级高度不匹配。

【问题讨论】:

  • 你想要的布局设计是什么?
  • 您需要在所有 textview 的下方添加一行,对吗?
  • Venky 您可以在第一个链接图像中看到所需的设计。问题是,如果我在列表视图中使用描述的相对布局,红色形状可绘制的行为会有所不同。

标签: android listview layout android-relativelayout xml-drawable


【解决方案1】:

我还发现使用RelativeLayout 很难完成它。那么试试这个布局为你的行 xml。它使用线性布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical">
    <TextView android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="TextView" 
        android:id="@+id/textView2" 
        android:layout_alignParentTop="true" />
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <LinearLayout android:layout_width="10dip" 
            android:layout_height="fill_parent"
            android:background="@drawable/errorindicator"/>       
        <TextView android:layout_width="0dip" 
            android:layout_height="wrap_content" 
            android:text="TextView" android:layout_weight="1.0" 
            android:id="@+id/textView1"
            android:textAppearance="?android:attr/textAppearanceMedium" 
            android:gravity="center"/>
   </LinearLayout>
</LinearLayout>

【讨论】:

  • 感谢您的回答 blessenm。如果我在列表视图中使用描述的相对布局,问题描述是一个简化的示例,以显示可绘制形状与父级高度不匹配。因此,无论是否在列表视图中使用,相同布局的行为都是不同的。我知道有不同的方法可以在视觉上实现与我的示例相同的效果,但布局比相对布局更简单、更高效。问题更多,为什么形状drawable表现不同。我想列表视图的布局过程肯定有什么不同...
  • 我确实注意到,当为可绘制形状指定大小时,它出现了。以后可能会尝试一下,看看有什么问题,因为这对我来说也很奇怪。
  • 非常感谢!请注意,我编辑了我的问题并指出,文本视图不相关。
  • 我现在发现,如果相对布局有一个固定的 android:layout_height,那么图像视图会填充父母的高度。但在我的情况下,我更喜欢将固定值(首选高度)设置为 minHeight 但该行应该能够比高度增长更多......嗯:/
  • 我想我想要的行为是不可能的。 RelativeLayout 文档说:“请注意,RelativeLayout 的大小与其子级的位置之间不能有循环依赖关系。例如,不能有一个高度设置为 WRAP_CONTENT 和一个子级设置为 ALIGN_PARENT_BOTTOM 的 RelativeLayout。”所以我的高度设置为包装内容的相对布局,其高度设置为 match_parent 的子级是这样的循环依赖!?但是如果RelativeLayout首先通过具有特定高度的孩子计算它的高度,它可以工作......
【解决方案2】:

因为我猜我想要的行为是不可能使用给布局管理器的,所以我采用了另一种适合我个人情况的方式。

我已经重写了RelativeLayout的onDraw方法并自己绘制了这个红色矩形(在布局过程完成并设置了高度之后)......

【讨论】:

    【解决方案3】:

    如果您将资源设置为 png,则可以对其进行 9-patch 并将其添加为 RelativeLayout 的背景

    【讨论】:

    • 感谢您的建议。我没有尝试过,但我可以想象这也可以作为解决方法(即使没有 9-patch 和现有的可绘制形状)。但它仍然没有回答我的问题;(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多