2017-04-15 75 views
0

我使用的是自定义的操作栏,所以我必须创建一个XML文件namedthemes改成@绘制/菜单/的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> 

此外,我创建的.xml当我使用一个ListView,我选择的项目要改变颜色

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

所以我想:文件名动作条放入@绘制/菜单/ actionbar.xml我的代码。 我在我的themes.xml中尝试了这一行,但它没有奏效。

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

不能安静地理解你的问题,你有一个列表视图,当你选择一个项目,你的操作栏应该改变它的颜色?哪种颜色?背景? –

+0

完全正确。现在,当我选择项目不会改变背景颜色。 – Dim

+0

您是否在应用中添加了“工具栏”小部件?你可以通过id找到它,并在后面的代码中设置它的背景颜色。 –

回答

0

我假设你正在使用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" /> 

然后在您页,其中包括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> 

然后在后面的代码发现这Toolbar通过ID和认购ItemSelected事件的ListView:

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

最后,设置例如这样的背景色:

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

    toolbar.SetBackgroundColor(Color.BlueViolet); 
} 

当然你也可以找到该项目的信息在AdapterView.ItemSelectedEventArgs中,并根据参数设置颜色。

+0

我没有使用android.support.v7.widget.Toolbar。不幸的是,它不工作。但我也在MyListViewAdapter中试过这个工具。 row.click + =代表{.SetBackgroundColor(Color.BlueViolet);};但是,然后它不工作我的主要活动代码在项目单击事件 – Dim

+0

@Dim,'工具栏'和'android.support.v7.widget.Toolbar'都可以工作,我的示例使用'工具栏'。我不知道为什么你的代码不工作,它在我身边很好,我使用的事件是'ItemSelected',也许你可以共享你的示例,或者你的主要活动的更多代码,包括xml代码和后端码。 –