Lea*_*ros 6 android styles actionbarsherlock android-actionbar
我想使用不同的ActionBar图标取决于我使用的样式(暗或亮).我无法弄明白是怎么回事,继承了我的尝试:
<style name="AppThemeDark" parent="Theme.Sherlock.ForceOverflow">
<item name="android:actionButtonStyle">@style/customActionButtonStyleDark</item>
<item name="actionButtonStyle">@style/customActionButtonStyleDark</item>
</style>
<style name="AppThemeLight" parent="Theme.Sherlock.Light.ForceOverflow">
<item name="android:actionButtonStyle">@style/customActionButtonStyleLight</item>
<item name="actionButtonStyle">@style/customActionButtonStyleLight</item>
</style>
<style name="customActionButtonStyleDark" >
<item name="@drawable/action_search">@drawable/action_search</item>
</style>
<style name="customActionButtonStyleLight" >
<item name="@drawable/action_search">@drawable/action_search_light</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我也尝试<item name="@drawable/action_search">@drawable/action_search</item>直接插入主题样式,但没有任何效果.
有什么想法?
Ste*_*yle 19
即使您找到了解决方法,也许这会帮助其他人.我在xml中找到了一种简单的方法(logcat的答案指的是什么).我使用的技巧是为菜单/操作栏图标创建自定义属性.每个菜单项图标必须有一个属性.
您需要attrs.xml在values文件夹中创建,并添加自定义属性.将每个属性视为主题/样式设置的常量,然后您的样式/视图可以使用这些属性来设置属性.
<declare-styleable name="customAttrs">
<attr name="customSearchIcon" format="reference" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
在您styles.xml的values文件夹中,让您的主题/样式将自定义图标属性设置为drawable引用.
<style name="AppThemeDark" parent="Theme.Sherlock.ForceOverflow">
<item name="customSearchIcon">@drawable/action_search</item>
</style>
<style name="AppThemeLight" parent="Theme.Sherlock.Light.ForceOverflow">
<item name="customSearchIcon">@drawable/action_search_light</item>
</style>
Run Code Online (Sandbox Code Playgroud)
最后,在您[menu_name].xml的menu文件夹中,让您的菜单项将其图标设置为匹配的自定义图标属性.
<item
android:id="@+id/menuitem_search"
android:icon="?attr/customSearchIcon"/>
Run Code Online (Sandbox Code Playgroud)
现在,根据设置的主题,菜单项的图标将会更改.此外,这允许您使用资源标识符与您的drawable文件夹仍然具有您的drawables(浅色和深色)的API特定版本,因此您可以为3.0之前的3.0和3.0的操作栏样式图标设置不同的菜单样式图标.
此外,请记住在运行时(VS设置一个主题的时候AndroidManifest.xml),你必须在调用之前设置它setContentView()在你的Activity.建议在更改Activity已创建的主题后重新启动活动.
解决了它,但放弃了尝试使用 XML,我现在以编程方式完成了它:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SharedPreferences prefs = getSharedPreferences("app", 0);
boolean isDark = "Dark".equals(prefs.getString("theme", "Dark"));
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
// set Icons
menu.findItem(R.id.menuitem_search).setIcon(isDark ? R.drawable.action_search : R.drawable.action_search_light);
menu.findItem(R.id.menuitem_add).setIcon(isDark ? R.drawable.content_new : R.drawable.content_new_light);
menu.findItem(R.id.menuitem_share).setIcon(isDark ? R.drawable.social_share : R.drawable.social_share_light);
return true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3344 次 |
| 最近记录: |