6

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>

私のコードは現在これを生成します: grrr

4

2 に答える 2

16

ステップ1:RelativeLayoutベースを使用します。

ステップ2:あなたImageButtonが持っているものとして置くandroid:layout_alignParentRight="true"

ステップ3:あなたのTextView持っているものandroid:layout_alignParentLeft="true"(あなたのIDはandroid:layout_toLeftOf="..."どこですか)、そしておそらく垂直方向の配置のための他の値を入れてください...ImageButtonRelativeLayout.LayoutParams

于 2010-02-21T23:46:34.410 に答える
2

次のコードスニペットは、テキスト(水平方向に左揃え、via android:layout_alignParentLeft="true")とアイコン(水平方向に右揃え、via android:layout_alignParentRight="true")、およびすべて垂直方向に中央揃え()を持つListView行を作成する方法の例を示しています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>
于 2015-02-09T20:24:00.393 に答える