TextView 不可见

TextView not visible

我写的布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >

<TextView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"
    android:contentDescription="TODO"
    android:background="@drawable/history_unfiled_circle" />

<TextView
    android:id="@+id/secondLine"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/icon"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="Description"
    android:textSize="12sp" />

<TextView
    android:id="@+id/firstLine"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/secondLine"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_alignWithParentIfMissing="true"
    android:layout_toRightOf="@id/icon"
    android:gravity="center_vertical"
    android:text="Example application"
    android:textSize="16sp" />

</RelativeLayout> 

左边应该是一个图标,右边是两行文字。 但是第一行不可见。我花了很多时间,但无法解决。希望你的帮助。

更新:其实我想得到这个结果

因为你在RelativeLayout中。将其更改为 LinearLayout 并且不要忘记添加方向。阅读此文档以了解何时使用 RelativeLayout 以及何时使用 LinearLayout。

http://www.androidhive.info/2011/07/android-layouts-linear-layout-relative-layout-and-table-layout/

编辑:此外,如果您 fill_parent 在您的高度 (ImageView),您的 ImageView 将占据整个屏幕。

这就是我的 Android Studio 使用虚拟设备 Nexus S 时的样子。

代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="80px"
    android:layout_height="80px"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/black_mana_toggle"
    android:layout_marginRight="10px"
    />

<TextView
    android:id="@+id/secondLine"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/icon"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="Description"
    android:textSize="12sp" />

<TextView
    android:id="@+id/firstLine"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/secondLine"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_alignWithParentIfMissing="true"
    android:layout_toRightOf="@id/icon"
    android:gravity="center_vertical"
    android:text="Example application"
    android:textSize="16sp" />
</RelativeLayout>
  1. 您需要设置 RelativeLayout android:minHeight=?android:attr/listPreferredItemHeightandroid:layout_height=wrap_content,因为其中一个文本的高度是固定的。

  2. 您的secondLineTextView需要指定android:layout_below="@id/firstLine"