为什么同一相对布局中的两个项目的布局不同?

时间:2011-06-03 16:48:01

标签: android relativelayout

我有一个listview,其中包含RelativeLayout

指定的项目

有一个图像(灰色箭头,这是png,带有透明填充)。在第一种情况下,带有蓝色背面的文本与此图像重叠,在第二种情况下 - 它是对齐的。 这些情况之间的唯一区别是左照片在第一种情况下是不可见的。我不明白为什么在第二种情况下蓝色文字不会重叠灰色箭头? (这是理想的行为)

<?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="wrap_content"
  android:padding="@dimen/padding_content">
  <ru.ip_news.ui.views.RationalImage
    android:id="@+id/picture"
    android:layout_width="@dimen/article_short_image_width"
    android:layout_height="fill_parent"/>
  <View
    android:id="@+id/separator"
    android:layout_width="@dimen/padding_content"
    android:layout_height="fill_parent"
    android:layout_toRightOf="@id/picture"/>
  <ImageView
    android:id="@+id/dis_ind"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:src="@drawable/dis_ind"/>   
  <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/separator"
    android:layout_toLeftOf="@id/dis_ind"
    android:layout_gravity="left"
    android:textStyle="bold"
    android:textColor="@color/article_text_main"
    android:maxLines="2"
    android:ellipsize="end"
    android:background="#F0F0"/>
  <TextView
    android:id="@+id/description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/separator"
    android:layout_below="@id/title"
    android:layout_alignParentBottom="true"
    android:textColor="@color/article_text_secondary"
    android:maxLines="4"
    android:background="#F00F"/>
</RelativeLayout>

enter image description here

3 个答案:

答案 0 :(得分:0)

之前我从未使用过Android编码,但只是从查看代码时我会说android:layout_alignParentRight="true"之间存在冲突:

<ImageView
    android:id="@+id/dis_ind"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:src="@drawable/dis_ind"/> 

以及您在此处找到的android:layout_alignParentBottom="true"

  <TextView
    android:id="@+id/description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/separator"
    android:layout_below="@id/title"
    android:layout_alignParentBottom="true"
    android:textColor="@color/article_text_secondary"
    android:maxLines="4"
    android:background="#F00F"/>

我之前从未使用过代码库,也没有为您提供“解决方案”,但也许它会为您提供一个起点。

答案 1 :(得分:0)

由于顶视图中不再存在图片,因此布局变小,因此文本视图更接近。在两个文本视图之间加上一些余地来解决问题。

答案 2 :(得分:0)

通过提取linearlayout并将照片移动到它来解决问题。出于某种原因,同一布局中的这张照片涉及文本之间的空间。

<?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="wrap_content"
    android:padding="@dimen/padding_content">
    <ru.ip_news.ui.views.RationalImage
        android:id="@+id/picture"
        android:layout_width="@dimen/article_short_image_width"
        android:layout_height="fill_parent"/>
    <RelativeLayout   
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingLeft="@dimen/padding_content">
      <ImageView
        android:id="@+id/dis_ind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/dis_ind"/>   
      <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/dis_ind"
        android:layout_gravity="left"
        android:layout_alignParentLeft="true"
        android:textStyle="bold"
        android:textColor="@color/article_text_main"
        android:maxLines="2"
        android:ellipsize="end"/>
      <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_alignParentBottom="true"
        android:textColor="@color/article_text_secondary"
        android:maxLines="4"/>
    </RelativeLayout>
</LinearLayout>
相关问题