简体   繁体   English

自定义ActionBar Listview选定的颜色

[英]Custom ActionBar Listview selected color

I'm using a custom action bar so i have create an xml file namedthemes into @drawable/menu/themes.xml with my code: 我使用的是自定义操作栏,因此我使用自己的代码在@ drawable / menu / themes.xml中创建了一个名为themes的xml文件:

  <?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>

Also i create a .xml file name actionbar placed into @drawable/menu/actionbar.xml with my code: 我也用我的代码创建一个放置在@ drawable / menu / actionbar.xml中的.xml文件名actionbar:

<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>

So i want when i use a listview, on my selected item to be changed color. 所以我想当我使用列表视图时,在我选择的项目上更改颜色。 I tried this line in my themes.xml but it didnt work. 我在themes.xml中尝试了此行,但没有成功。

 <item name="android:colorActivatedHighlight">@android:color/transparent</item>

I assume that you're using a Toolbar or android.support.v7.widget.Toolbar widget for your action bar, then I take a example for using Toolbar widget here: 我假设您正在使用Toolbarandroid.support.v7.widget.Toolbar小部件作为操作栏,然后在此处举一个使用Toolbar小部件的示例:

<?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" />

Then in you page which includes the ListView for changing the background color of action bar, you can include this Toolbar : 然后,在包含用于更改操作栏背景颜色的ListView页面中,可以包含以下Toolbar

<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>

Then in code behind find this Toolbar by its id and subscribe the ItemSelected event of listview: 然后在后面的代码中按其ID找到此Toolbar ,并订阅listview的ItemSelected事件:

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;
}

Finally set the background color for example like this: 最后像这样设置背景色:

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

   toolbar.SetBackgroundColor(Color.BlueViolet);
}

Of course you can find the item's info in the AdapterView.ItemSelectedEventArgs , and according to the args to set the color. 当然,您可以在AdapterView.ItemSelectedEventArgs找到该项目的信息,并根据args设置颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM