0

ListView のセルをタッチで強調表示したい (変更されていない ListView で通常行うのと同じように)。変更したセルでそれを行うにはどうすればよいですか?

私のセルは次のようになります。

ここに画像の説明を入力

そして、ここに私のセルのXMLがあります:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">

  <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="34dip"
        android:stretchColumns="0"
        android:divider="@null"       
         >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/plaincell"
            android:divider="@null"  >

            <TextView
                android:id="@+id/txtKey"
                android:text="Some text"
                android:textSize="16dip"
                android:textStyle="bold"
                android:layout_marginLeft="8dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="left|center_vertical"
                android:layout_weight="1.0"
                android:layout_marginTop="6dp"
                android:layout_marginBottom="6dp"
                android:textColor="@color/black"
            />

            <TextView
                android:id="@+id/txtValue"
                android:text="Some text"
                android:textSize="16dip"
                android:layout_marginRight="8dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right|center_vertical"
                android:layout_weight="1.0"
                android:layout_marginTop="6dp"
                android:layout_marginBottom="6dp"
                android:textColor="@color/darkblue"
            />

        </TableRow>

  </TableLayout>

</LinearLayout>
4

3 に答える 3

2

LinearLayout の背景をセレクター ドローアブルにします

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/myClickedStateDrawable" android:state_pressed="true" />
   <item android:drawable="@drawable/myNonClickedStateDrawable" />
</selector>

myClickedStateDrawable、myNonClickedStateDrawable は、レイアウトの背景を定義するドローアブルです。

お役に立てれば

于 2012-09-24T22:18:21.603 に答える
2

バックグラウンド ドローアブルは、さまざまな状態 (たとえば、押されている、フォーカスされている、どちらでもない) ごとに異なるグラフィックを持つ状態リストである必要があります。

編集:単色を使用するには、次のようにします。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@color/pressedcolor" android:state_pressed="true" />
   <item android:drawable="@color/unpressedcolor" />
</selector>

次に、リソース ファイルで独自の色を定義できます。

<resources>
    <color name="pressedcolor">#ff0000</color>
    <color name="unpressedcolor">#0000ff</color>
</resources>
于 2012-09-24T22:21:05.523 に答える
1

何が起こっているかわかりましたが、最初に:

  • 子が 1 つしかないほとんどのレイアウトは削除できます。
  • また、TableLayout と TableRow はどちらも LinearLayout クラスに基づいています。

したがって、実際には3 つのLinearLayouts があり、必要なのは1 つだけです。

問題:
この行は、レイアウト全体をカバーする新しい背景色を設定すると仮定します。

android:background="@drawable/plaincell"

これは、デフォルトの ListView カラー セレクターもカバーまたはオーバーライドします。画面上のすべての UI 要素の背景とテキストの色の属性を変更する場合は、スタイルとテーマ
を調べることができます。テーマでこれらの属性を設定する場合、古いカラー セレクターが既に行っていたことを行うために、新しいカラー セレクターを作成する必要はありません。

于 2012-09-24T22:34:31.903 に答える