我的 android 应用程序中有一个必须显示键值对列表的活动。key 和 value 都是长度不统一的字符串资源。键应左对齐,值应右对齐。如果 key 和 value 在屏幕上排成一行,则它们应相邻显示,而换行符应将 key 和 value 分开。
我为列表项创建了两种布局,一种带有换行符,一种没有,如下图所示:
如何在我的活动的 ListView 中自动显示正确的列表项?
编辑:根据要求,我还添加了我的源代码(我认为这不是很有帮助,两个布局文件之间的区别必须在我将列表布局片段添加到我的 ListView 的部分中完成。但是,在这个地方我将不得不解决许多其他问题,例如确定字符串资源的长度、确定可用于显示布局的空间以及创建对可用空间变化做出反应的处理程序。因此我更喜欢@bwt 提出的解决方案)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_6">
<TextView
android:id="@+id/listitem_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
tools:text="Key"/>
<TextView
android:id="@+id/listitem_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
tools:text="Value"/>
</RelativeLayout>
并带有换行符
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_6">
<TextView
android:id="@+id/listitem_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
tools:text="Key with a very long text constant"/>
<TextView
android:id="@+id/listitem_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/listitem_key"
android:layout_alignParentEnd="true"
tools:text="Value with a very long text constant"/>
</RelativeLayout>
摇曳的蔷薇
慕尼黑5688855
相关分类