12

ActionBarSherlockを使用しており、行の背景のactivateBackgroundIndicator属性をカスタマイズしようとしています。

ActionBarSherlockを使用せずに最新のAndroidSDKを使用すると、背景をカスタマイズしてres / values / style.xmlに次のスタイルを作成し、 AndroidManifest.xmlandroid:theme = "@ style/Themeとして定義できます。カスタム」

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Custom" parent="android:Theme.Holo.Light.DarkActionBar">
     <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
  </style>
</resources>

次に、res / drawable / activate_background.xmlに次のコードが含まれています:

<selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:state_activated="true" android:drawable="@color/row_activated" />
   <item android:drawable="@android:color/transparent" />  
</selector>

最後に、ListViewの各行を定義するために使用されるコードは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="?android:attr/activatedBackgroundIndicator">
    <TextView
            android:id="@+id/test_row"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/sample_string"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:padding="15dp"/>
</LinearLayout>

結果はスクリーンショットに表示されます。リストアイテムがクリックされたときにListView.CHOICE_MODE_SINGLEgetListView()。setItemChecked(position、true)を持つListViewのみを持つ単純なアプリケーションです。

選択した標準行の青色は黄色になり、完全に機能します。

ABSなしのリスト

ActionBarSherlockを使用して同じカスタマイズを適用したい場合、問題が発生します。これで、スタイルファイルが次のファイルになり、背景はカスタムの黄色ではなく青色で表示されます。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.Custom" parent="Theme.Sherlock.Light.DarkActionBar">
        <item name="activatedBackgroundIndicator">@drawable/activated_background</item>
        <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
    </style>

    <style name="activatedBackgroundIndicator">
        <item name="android:background">?android:attr/activatedBackgroundIndicator</item>
    </style>
</resources>

ABS付きリスト

ActionBarSherlockがandroid:activateBackgroundIndicator機能をサポートしているかどうか、またはデフォルトの色を変更できるようにするために必要な何かを実装するのを忘れたかどうかはわかりません。

何か案は?

4

1 に答える 1

10

ついに私は問題の解決策を見つけました。
これは、行アダプターで使用されるコンテキストでした。使用されるコンストラクターは、次のコードに示されています。

public RowAdapter(Activity activity, ArrayList<String> list) {
    this.mContext = activity.getApplicationContext();
    this.elements = list;
    this.theActivity = activity;

    this.inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

コンテキスト
this.mContext = activity.getApplicationContext()(Application-Context)
から
this.mContext = activity.getBaseContext()(Activity-Context)に変更するだけ
で問題が解決し、背景がカスタムになりました。

ビューを操作する必要があるときはいつでも、Activity-Contextに移動します。そうでない場合は、 Application-Contextで十分です。

この回答は、私の場合、 getApplicationContext( )の代わりにgetBaseContext()を使用する理由を理解するのに役立ちました。

カスタム行の背景

于 2013-03-12T15:04:40.467 に答える