【问题标题】:ListView displaying problemsListView 显示问题
【发布时间】:2013-09-30 07:14:36
【问题描述】:

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

我希望列表像这样对齐:

  1. 图标和日期应始终完整显示!!
  2. 中间的文本应显示尽可能多的字符
  3. 日期应始终在右侧

下面的 XML 在我的 Nexus 4 上看起来不错。但在华硕平板电脑上,日期应该更多在右侧,中间的列应该显示更多文本 (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>

【问题讨论】:

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


【解决方案1】:

在我的脑海中,你的布局看起来应该是这样的。-

<?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,以便填充行的其余部分。


一些有用的提示。-

  • fill_parent 已弃用,请改用 match_parent
  • 当视图没有子视图时,您可以使用/&gt; 将其关闭
  • singleLine="true" 足以拥有一个单线 TextView
  • 您通常希望使用dp 单位而不是px

【讨论】:

  • 很高兴它有帮助 :) 顺便说一句,不要忘记考虑支持/接受对您有更好帮助的答案,因为它可能对其他用户有用。
【解决方案2】:

这应该可以完成您的工作。我已将图像视图放在父母左侧,父母右侧的日期以及图像右侧和日期左侧的标签。它应该适合你。 margins , padding 请自己放

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

【讨论】:

  • 那么请接受答案..(您必须单击我的答案的对勾才能接受)
猜你喜欢
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
相关资源
最近更新 更多