亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號