ListView显示问题

时间:2013-09-24 21:24:02

标签: android android-layout listview android-listview alignment

我有一个包含三列的列表视图。第一个是ImageView,第二个是textview(见代码打击)。

我希望列表像这样对齐:

  1. 图标和日期应始终完整显示!!
  2. 中间的文字应该显示尽可能多的字符
  3. 日期应始终位于右侧的右侧
  4. 使用下面的XML在我的Nexus 4上看起来没问题。但是在华硕平板电脑上,日期应该在右边,而中间的coloumn应该显示更多的文本(screenshot 1)。在AVD(screenshot 2)上,中间的颜色应该更小,日期应该完整显示。

    有谁能告诉我如何更改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="fill_parent"
    android:padding="5dp" >
    
    <ImageView
        android:id="@+id/logo"
        android:layout_width="50px"
        android:layout_height="50px"
        android:layout_marginLeft="5px"
        android:layout_marginRight="20px"
        android:layout_marginTop="5px"
        android:src="@drawable/social_chat"
        android:layout_weight="0"
        >
    </ImageView>
    
    <TextView
        android:id="@+id/label"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_marginTop="0px"
        android:text="@+id/label"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_weight="50"
        android:maxLines="1"
        android:singleLine="true"
        >
    </TextView>
    
    <TextView
        android:id="@+id/last_changed"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_marginTop="15px"
        android:layout_marginLeft="5px"
        android:text="@+id/last_changed"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_weight="50"
        android:maxLines="1"
        android:singleLine="true"
        >
    </TextView>
    

2 个答案:

答案 0 :(得分: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"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/social_chat" />

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_weight="1"
        android:singleLine="true" />

    <TextView
        android:id="@+id/last_changed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="5dp"
        android:text="@+id/last_changed"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:singleLine="true" />

</LinearLayout>

这里的关键是将wrap_content用于需要保持其大小的视图,并为中间视图设置weight,您希望填充该行的其余部分。 / p>


一些有用的提示.-

  • fill_parent已弃用,请改用match_parent
  • 当视图没有子视图时,您可以使用/>
  • 关闭它
  • singleLine="true"足以拥有singleLined TextView
  • 您通常希望使用dp单位而不是px

答案 1 :(得分:0)

这应该可以胜任你的工作。我把imageview放在父母左边,父母右边的日期和右边图像的标签以及日期的左边。它应该适合你。边距,填充请自己放置

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



<ImageView
    android:id="@+id/logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:src="@drawable/social_chat" />

    <TextView
    android:id="@+id/last_changed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:singleLine="true" />

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_toLeftOf="@id/last_changed"
    android:layout_toRightOf="@id/logo"
    android:singleLine="true" />
</RelativeLayout>
相关问题