2017-04-15 4 views
0

を選択:カスタムアクションバーリストビューには、私は私のコードでの@描画可能/メニュー/ themes.xmlにxmlファイルのnamedthemesを作成している私は、カスタムアクションバーを使用しているカラー

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<!-- the theme applied to the application or activity --> 
<style name="CustomActionBarTheme" 
    parent="@android:style/Theme.Holo.Light.DarkActionBar"> 
<item name="android:actionBarStyle">@style/MyActionBar</item> 
<item name="android:windowNoTitle">false</item> 
<item name="android:windowFullscreen">true</item> 
</style> 

<!-- ActionBar styles --> 
<style name="MyActionBar" 
    parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse"> 
<item name="android:background">#2d73c4</item> 
</style> 

</resources> 

をも、私は.xmlファイルを作成します私のコードでの@描画可能/メニュー/ actionbar.xmlに置かれたファイル名のアクションバー:私はリストビューを使用するときに

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:id="@+id/search" 
    android:showAsAction="always" 
    android:title="Αναζήτηση" 
    android:icon="@drawable/ic_action_search"/> 

</menu> 

ので、私はしたい、色を変更するための私の選択した項目に。 私はthemes.xmlでこの行を試しましたが、動作しませんでした。

私はその後、私はここに Toolbarウィジェットを使用するための例を取る、あなたのアクションバーのため Toolbarまたは android.support.v7.widget.Toolbarウィジェットを使用していることを前提としてい
<item name="android:colorActivatedHighlight">@android:color/transparent</item> 
+0

あなたの質問を理解できない、リストビューを持っている、1つのアイテムを選択すると、アクションバーの色が変わるはずですか?どの色ですか?バックグラウンド? –

+0

正確には、アイテムを選択しても背景色が変わっていません。 – Dim

+0

あなたのアプリに 'ツールバー'ウィジェットを追加しましたか?あなたはidでそれを見つけることができ、コードの背景色を設定することができます。 –

答えて

0

<?xml version="1.0" encoding="utf-8" ?> 
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?android:attr/actionBarSize" 
    android:background="?android:attr/colorPrimary" 
    android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar" /> 

その後ListViewが含まれますページにアクションバーの背景色を変更するには、これを含むことができます。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar" /> 

    <ListView 
    android:id="@+id/lv" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" /> 

</LinearLayout> 

そして、コードで背後そのIDによるこのToolbarを見つけて、リストビューのItemSelectedイベントをサブスクライブ:

private void Lv_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e) 
{ 

    toolbar.SetBackgroundColor(Color.BlueViolet); 
} 

アイテムの情報を見つけることができますもちろん:最後に、このような例の背景色を設定し

public Toolbar toolbar; 

protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 
    SetContentView(Resource.Layout.Main); 

    toolbar = FindViewById<Toolbar>(Resource.Id.toolbar); 
    SetActionBar(toolbar); 

    ListView lv = (ListView)FindViewById(Resource.Id.lv); 

    lv.ItemSelected += Lv_ItemSelected; 
} 

AdapterView.ItemSelectedEventArgsで、色を設定するargsに従って。

+0

私はandroid.support.v7.widget.Toolbarを使用していません。残念なことに、それdoes not work.But MyListViewAdapterでこれを試して、それは動作します。 row.click + = delegate {.SetBackgroundColor(Color.BlueViolet);};しかし、それは、アイテムクリックイベント – Dim

+0

@Dimの私の主な活動コードを働かせません。 'Toolbar'と' android.support.v7.widget.Toolbar'の両方が動作し、私のサンプルは 'Toolbar'を使用します。なぜあなたのコードがうまくいかないのか分かりませんし、私の側でうまく動作し、私が使ったイベントは 'ItemSelected'です。おそらく、あなたのサンプルや、主な活動のコード、XMLコードとバックエンドコード。 –

関連する問題