0

各行にパディングがある単純なリスト ビューを作成しようとしていました。

私は本当に簡単な方法でそれを行いましたが、それが正しい方法であるかどうかはわかりません.

これが私が得たものです(コードは以下にあります)。行をタップするとわかるように、それは完全に強調表示されます。(可能であれば) その中のビュー (灰色の背景を持つもの、つまり TextView) のみを強調表示したいです。

例

これはアクティビティ レイアウトのコードです。

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

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="@null"
        android:dividerHeight="0dp" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_events" />

</LinearLayout>

これはリスト項目レイアウトのコードです:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:background="#ccc"
        android:text="placeholder" />

</FrameLayout>

返信ありがとうございます。

PS。アクティビティはアップロードしませんでした。これは単純な ListActivity です。

アップデート

解決しました (Jorge Aguilar のおかげです)。新しいコードは次のとおりです。

リスト項目:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:background="@drawable/event_focused"
        android:text="placeholder" />

</FrameLayout>

イベントに焦点を当てたファイル (drawable ディレクトリにあります):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@android:color/black" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@android:color/darker_gray" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@android:color/darker_gray" /> <!-- hovered -->
    <item android:drawable="@android:color/darker_gray" /> <!-- default -->
</selector>

これも追加しました:

android:listSelector="@android:color/transparent"

私のListViewに:)

4

1 に答える 1

3

onitemselected メソッドにコードを追加して、id などでテキストビューを検索し、背景色を変更することができます。それともあなたが望むものとは違うのですか?

アップデート:

xml ファイル内から変更する必要がある場合は、この投稿がそれを達成するのに役立つ可能性があります。最後の投稿にスキップしてください。うまくいけば、そこに答えが見つかります。

于 2012-08-01T23:01:18.757 に答える