猿问

Android/Java:布局自动换行

我的 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>


HUWWW
浏览 161回答 2
2回答

摇曳的蔷薇

自动包装视图是 Google 的FlexboxLayout的一个典型用例。在您的情况下,它可能类似于:<com.google.android.flexbox.FlexboxLayout&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; app:alignContent="flex_start"&nbsp; &nbsp; app:flexWrap="wrap">&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/textview1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content" />&nbsp; &nbsp; <!-- right justified (using the remaining space) -->&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/textview2"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="end"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_flexGrow="1" /></com.google.android.flexbox.FlexboxLayout>

慕尼黑5688855

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingBottom="10dp"android:paddingTop="10dp"><TextView&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:gravity="right"&nbsp; &nbsp; android:layout_alignParentRight="true"&nbsp; &nbsp; android:layout_alignParentEnd="true"&nbsp; &nbsp; android:orientation="vertical"&nbsp; &nbsp; android:id="@+id/text1"/><TextView&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:gravity="left"&nbsp; &nbsp; android:id="@+id/notification_message"&nbsp; &nbsp; android:layout_alignParentLeft="true"&nbsp; &nbsp; android:layout_alignParentStart="true"&nbsp; &nbsp; android:layout_alignParentTop="true"&nbsp; &nbsp; android:layout_toLeftOf="@id/text1"&nbsp; &nbsp; android:layout_toStartOf="@id/text1"/></RelativeLayout>
随时随地看视频慕课网APP

相关分类

Java
我要回答