簡體   English   中英

ListView行樣式 - 左對齊文本和右對齊圖標

[英]ListView row styling - left aligned text, and right-aligned icon

我正在嘗試將ListView行看起來如下所示:

| Text-Text-Text                        <ImageButton> |

圖像按鈕捕捉到右邊緣。 我怎樣才能做到這一點? 這是我正在使用的當前布局代碼。 我究竟做錯了什么?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layercontainer"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="#699">
 <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:layout_gravity="left">
   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="YO HOW SI IT GOESSDA" />
 </LinearLayout>

 <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:layout_gravity="right">
   <ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/trash" />
 </LinearLayout>
</LinearLayout>

我的代碼目前產生這個: 哎呀

步驟#1:使用RelativeLayout基礎。

第2步:將你的ImageButton設為android:layout_alignParentRight="true"

第3步:把TextView放到android:layout_alignParentLeft="true"android:layout_toLeftOf="..." (其中...是你的ImageButton的ID),也許還有其他一些RelativeLayout.LayoutParams值用於垂直對齊

以下代碼片段提供了一個示例,說明如何創建具有一些文本的ListView行(水平左對齊,通過android:layout_alignParentLeft="true" )和一個圖標(通過android:layout_alignParentRight="true"水平右對齊android:layout_alignParentRight="true" ),並且所有垂直居中( android:layout_centerVertical="true" )。

它呈現如下(YMMV,取決於全局樣式):

渲染代碼示例

還有一個注釋掉的附加圖標,可以放在最右邊的圖標的左側; 刪除XML注釋標記以啟用。

這是布局XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:descendantFocusability="blocksDescendants"
    android:padding="6dp">
    <!-- Note: android:descendantFocusability="blocksDescendants" set to ensure that
     OnItemClickListener works by ensuring constituent controls do not take focus -->


    <TextView
        android:id="@+id/lbl_list_item_row_text"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:lines="1"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="My List Item Text"
        />

    <!-- This can be uncommented to add another button 

    <ImageButton
        android:id="@+id/btn_additional_icon"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/icon_additional_icon"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:padding="3dp"
        android:background="@null" 
        android:src="@drawable/icon_additional_icon" />

    -->

    <ImageButton
        android:id="@+id/icon_additional_icon"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@null"
        android:padding="3dp"
        android:src="@drawable/icon_right_aligned_icon" />
</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM