为了账号安全,请及时绑定邮箱和手机立即绑定

Android/Java:布局自动换行

Android/Java:布局自动换行

HUWWW 2022-05-25 16:52:52
我的 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>
查看完整描述

2 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

自动包装视图是 Google 的FlexboxLayout的一个典型用例。在您的情况下,它可能类似于:


<com.google.android.flexbox.FlexboxLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    app:alignContent="flex_start"

    app:flexWrap="wrap">


    <TextView

        android:id="@+id/textview1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />


    <!-- right justified (using the remaining space) -->

    <TextView

        android:id="@+id/textview2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:gravity="end"

        app:layout_flexGrow="1" />


</com.google.android.flexbox.FlexboxLayout>


查看完整回答
反对 回复 2022-05-25
?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

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

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:gravity="right"

    android:layout_alignParentRight="true"

    android:layout_alignParentEnd="true"

    android:orientation="vertical"

    android:id="@+id/text1"/>

<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:gravity="left"

    android:id="@+id/notification_message"

    android:layout_alignParentLeft="true"

    android:layout_alignParentStart="true"

    android:layout_alignParentTop="true"

    android:layout_toLeftOf="@id/text1"

    android:layout_toStartOf="@id/text1"/>



</RelativeLayout>


查看完整回答
反对 回复 2022-05-25
  • 2 回答
  • 0 关注
  • 160 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号