如何在 Android 中更改 ActionMode 背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20769315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change ActionMode background color in Android
提问by Love Garg
i am creating an android app in which support level api is 7 so i am using sherlock actionbar. I am using action mode in it. Issue is i want to change the background of action mode. So i have tried
我正在创建一个支持级别 api 为 7 的 android 应用程序,所以我正在使用 Sherlock 操作栏。我在其中使用动作模式。问题是我想改变动作模式的背景。所以我试过了
<item name="android:background">@color/something</item>
<item name="android:backgroundStacked">@color/something</item>
<item name="android:backgroundSplit">@color/something</item>
回答by Kokusho
In my case i resolved with this. First i created a style to define the actionModeBackground:
就我而言,我解决了这个问题。首先,我创建了一个样式来定义 actionModeBackground:
<style name="MyTheme.ActionMode" parent="@android:style/Theme.Holo.Light">
<item name="android:actionModeBackground">#FFFFFF</item>
</style>
Then i add the style into my base application theme:
然后我将样式添加到我的基本应用程序主题中:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionModeStyle">@style/MyTheme.ActionMode</item>
</style>
Hope it helps!!!
希望能帮助到你!!!
回答by Shayan Pourvatan
if you want change the color of ActionBar just do this:
如果您想更改 ActionBar 的颜色,请执行以下操作:
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));
see the following linkfor more info
请参阅以下链接了解更多信息
and if you using ActionMode This is the style used for any ActionMode. You'll need to create your own style to customize it
如果您使用 ActionMode 这是用于任何 ActionMode 的样式。您需要创建自己的样式来自定义它
<style name="Widget.ActionMode">
<item name="android:background">?android:attr/actionModeBackground</item>
<item name="android:backgroundSplit">?android:attr/actionModeSplitBackground</item>
<item name="android:height">?android:attr/actionBarSize</item>
<item name="android:titleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Title</item>
<item name="android:subtitleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Subtitle</item>
</style>
more info in this site
在这个网站上的更多信息
see thistoo
看到这个太
Edit
编辑
for pre-Honeycomb see thisplease
预蜂窝看到这个请
回答by Chintan Khetiya
If you are using simple Action Barthen try this,
如果您使用的是简单的操作栏,请尝试此操作,
ActionBar abar = getActionBar();
abar.setBackgroundDrawable(new ColorDrawable(0xff123456));
But if you are using Actionbar share lock Libthen try this
但是如果您使用的是Actionbar 共享锁 Lib然后试试这个
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xff123456));
ColorDrawable(0xff123456)
- should be your color code.
ColorDrawable(0xff123456)
- 应该是你的颜色代码。
Drawable/styles.xml
Drawable/styles.xml
<item name="android:actionModeBackground">@drawable/actionbar_background</item>
<item name="actionModeBackground">@drawable/actionbar_background</item>
Change Theme as per API level
根据 API 级别更改主题
If you want to change as per the API level then you should go for this
如果您想根据 API 级别进行更改,那么您应该这样做
回答by Colateral
If you want to do it programmatically (supporting AppCompat) you need to look for icon parent view. This code is tested from 2.3.7 to 6.0.1. You need to check on lower API... .
如果您想以编程方式(支持 AppCompat)执行此操作,则需要查找图标父视图。此代码从 2.3.7 到 6.0.1 进行了测试。您需要检查较低的 API... 。
first get your activity decor view
首先获取您的活动装饰视图
final ViewGroup decorView = (ViewGroup) getActivity().getWindow().getDecorView();
second set the color in onPrepareActionMode. You can also set the back icon or other elements here
其次在 onPrepareActionMode 中设置颜色。您还可以在此处设置后退图标或其他元素
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
{
decorView.postDelayed(new Runnable() {
@Override
public void run()
{
int buttonId = getResources().getIdentifier("action_mode_close_button", "id", "android");
View v = decorView.findViewById(buttonId);
if (v == null)
{
buttonId = R.id.action_mode_close_button;
v = decorView.findViewById(buttonId);
}
if (v != null)
{
((View)v.getParent()).setBackgroundColor(Color.red /*your color here*/);
}
}
}, 500);}
回答by Arkadiusz Cie?liński
Override this style in your custom theme:
在您的自定义主题中覆盖此样式:
<style name="Base.Widget.AppCompat.ActionMode" parent="">
<item name="background">?attr/actionModeBackground</item>
<item name="backgroundSplit">?attr/actionModeSplitBackground</item>
<item name="height">?attr/actionBarSize</item>
<item name="titleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Title</item>
<item name="subtitleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Subtitle</item>
<item name="closeItemLayout">@layout/abc_action_mode_close_item_material</item>
</style>
For example:
例如:
<style name="LywActionMode" parent="Base.Widget.AppCompat.ActionMode">
<item name="background">@color/lyw_primary_color</item>
<item name="backgroundSplit">@color/lyw_primary_color</item>
</style>
<style name="LywCompatTheme" parent="@style/Theme.AppCompat.Light">
...
<item name="actionModeStyle">@style/LywActionMode</item>
</style>
回答by Roger Alien
For those who still struggle, and none of the existing solutions helps, please make sure that in your layout with toolbar you have this:
对于那些仍然在挣扎并且现有解决方案都没有帮助的人,请确保在带有工具栏的布局中具有以下内容:
app:popupTheme="@style/Theme.ToolBar"
android:popupTheme="@style/Theme.ToolBar"
and you don't have
而你没有
app:theme="@style/Theme.ToolBar"
Since they will rise a conflict and your customization will not work.
因为它们会引起冲突,您的自定义将不起作用。
回答by Eugene Sianko
The simplest way to do it
最简单的方法
ActionBarContextView actionBar = getWindow().getDecorView().findViewById(R.id.action_mode_bar);
actionBar.setBackgroundColor(WhateverColorYouWant);
回答by M D
This is the style used for any ActionMode, I pulled it from the SDK. You'll need to create your own style to customize it. It's really easy to do. If you've never done anything like this before, you should read through this poston customizing the ActionBar. It explains everything you'll need to know.
这是用于任何 ActionMode 的样式,我从 SDK 中提取它。您需要创建自己的样式来自定义它。这真的很容易做到。如果您以前从未做过这样的事情,您应该通读这篇关于自定义 ActionBar 的文章。它解释了你需要知道的一切。
<style name="Widget.ActionMode">
<item name="android:background">?android:attr/actionModeBackground</item>
<item name="android:backgroundSplit">?android:attr/actionModeSplitBackground</item>
<item name="android:height">?android:attr/actionBarSize</item>
<item name="android:titleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Title</item>
<item name="android:subtitleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Subtitle</item>
</style>
回答by nAkhmedov
I wanna make more clearer this answer. First create custom background "storage_list_header_shape.xml" in your drawable folder.
我想让这个答案更清楚。首先在您的可绘制文件夹中创建自定义背景“storage_list_header_shape.xml”。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="270"
android:startColor="@android:color/holo_orange_light"
android:centerColor="@android:color/holo_orange_dark"
android:endColor="@android:color/holo_orange_light" />
<size android:height="3dp" />
</shape>
Second, create custom style in your style.xml
其次,在 style.xml 中创建自定义样式
<style name="customActionModeTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionModeBackground">@drawable/storage_list_header_shape</item>
</style>
Third, call the style in your manifest.
第三,调用清单中的样式。
<activity
android:name="com.javacafe.activities.Main"
android:launchMode="singleTop"
android:screenOrientation="sensorLandscape"
android:theme="@style/customActionModeTheme" >
</activity>