我正在使用此示例使用导航抽屉。我只想将蓝色更改为橙色,我该怎么做?我的意思是改变列表视图选择器颜色,操作栏选择器颜色一切。
7 回答
您可以使用选择器
在drawable文件夹中有selector.xml如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/press" />
<item android:state_focused="false"
android:drawable="@drawable/normal" />
</selector>
在可绘制文件夹中 press.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FF9D21"/>
</shape>
在可绘制文件夹normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#8BC7EB"/>
</shape>
在drawer_list_item.xml
android:background="@drawable/selector"
用于设置栏的样式
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<item name="android:background">@style/MyActionBar</item>
<!-- API 11 theme customizations can go here. -->
</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:background">@drawable/press</item>
</style>
</resources>
在您的清单中
<activity
android:name=".MainActivity"
android:theme="@style/MyActionBar"
android:label="@string/app_name">
您可以为特定活动而不是整个应用程序指定主题。
更多信息
http://android-developers.blogspot.in/2011/04/customizing-action-bar.html
https://developer.android.com/training/basics/actionbar/styling.html
快照
为此,您必须做两件事。
- 对于 listview 创建您的自定义 listview 选择器(请参阅答案 @raghunandan )
- 对于 actionbar 转到此处创建您的样式并从清单设置您的样式,将您的应用程序主题设置为您创建的样式
试试这个代码,
mDrawerLayout.setBackgroundResource(int);
您需要使用选择器。
查看本教程了解更多信息。自定义列表视图
对于 ActionBar 背景:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:actionBarItemBackground">@drawable/bg_actionbar</item>
</style>
</resources>
我想为@Raghunandan 给出的答案做出贡献。在列表中,您可以区分单击的项目 (android:state_pressed) 和激活的项目 (android:state_activated),因此,如果您单击列表中已激活的项目,则单击是可视化的。
在drawable文件夹下创建一个activated.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#6A6A6A"/>
</shape>
并修改 selector.xml 添加 state_activated 修饰符:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/press" />
<item android:state_activated="true"
android:drawable="@drawable/activated" />
<item android:state_focused="false"
android:drawable="@drawable/normal" />
</selector>
使用选择器来实现这一点。您可以使用@drawable
或@color
。
item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
item android:drawable="@drawable/list_item_bg_selected" android:state_activated="true"/