【问题标题】:ListView row styling - left aligned text, and right-aligned iconListView 行样式 - 左对齐文本和右对齐图标
【发布时间】:2011-01-19 10:37:28
【问题描述】:

我正在尝试让 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>

我的代码目前产生这个:

【问题讨论】:

    标签: android android-layout android-listview android-xml


    【解决方案1】:

    第 1 步:使用 RelativeLayout 基数。

    第 2 步:将您的 ImageButton 设置为具有 android:layout_alignParentRight="true"

    第 3 步:将您的 TextView 具有 android:layout_alignParentLeft="true"android:layout_toLeftOf="..."(其中 ... 是您的 ImageButton 的 ID),也许还有其他一些 RelativeLayout.LayoutParams 值用于垂直对齐

    【讨论】:

    • 嘿!有效!谢谢一堆。我之前从来没有真正使用过RelativeLayout,所以我没想过使用它们。
    【解决方案2】:

    以下代码 sn-p 提供了一个示例,说明如何创建一个 ListView 行,其中包含一些文本(水平左对齐,通过android:layout_alignParentLeft="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>
    

    【讨论】:

      猜你喜欢
      • 2012-10-13
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 2019-01-21
      • 2015-01-09
      相关资源
      最近更新 更多