详情:
我正在扩展 ActionBarActivity。
截至 2011 年 11 月 6 日,Eclipse 和 SDK 已完全修补。
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14" />
部署到 Android 2.3.3 三星设备
申请有
android:theme="@android:style/Theme.Light"
问题:应用程序很轻,但是ActionBar是蓝色的,灰色的图标,在蓝色背景色的映衬下几乎看不出来。我还希望 ActionBar 变亮,这样它们的灰色图标就更明显了。
我尝试修改样式但无济于事
我可能遗漏了一些微不足道的东西。
如何使用 XML 更改 ActionBarActivity 的 ActionBar 的背景颜色?
根据文档-“您可以使用在 Android 3.0(API 级别 11)中添加的 ActionBar API 来控制操作栏的行为和可见性。”
因此,ActionBar 不适用于 API 级别 10 (Android 2.3.3) 的目标环境。
以防万一,如果您的目标是最低 API 级别 11,您可以通过定义自定义样式来更改 ActionBar 的背景颜色,如:
<resources>
<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">ANY_HEX_COLOR_CODE</item>
</style>
</resources>
然后,将“MyTheme”设置为应用程序/活动的主题。
试试这个
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));
试试这个:
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
使用这个 - http://jgilfelt.github.io/android-actionbarstylegenerator/
这是一个了不起的工具,可让您通过实时预览自定义操作栏。
我尝试了较早的答案,但在更改选项卡和字母等其他颜色时总是遇到问题,并且花了数小时来调整内容。这个工具帮助我在几分钟内完成了设计。
一切顺利! :)
我遇到了同样的问题,当我输入该代码时,我的操作栏会变成灰色。您的原始样式表很可能是这样的:
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
“DarkActionBar”使您的 Action Bar 保持灰色。我把它改成了这个,它起作用了:
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 14 theme customizations can go here. -->
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#2aa4cd</item>
<item name="android:titleTextStyle">@style/Theme.MyAppTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="Theme.MyAppTheme.ActionBar.TitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#FFFFFF</item>
</style>
我还投入了如何编辑文本颜色。此外,无需更改资源周围的任何内容。
-沃伦
我正在使用
minSdkVersion = "9"
和 targetSdkVersion = "21"
构建一个应用程序我更改了操作栏的颜色并且它 适用于 API 级别 9
这是一个xml
res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">@color/actionbar_background</item>
</style>
</resources>
并设置你想要的操作栏的颜色
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="actionbar_background">#fff</color> //write the color you want here
</resources>
Actionbar颜色也可以在
.class
文件中定义,代码片段是
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
但这 不适用于API ,因此在 < 11xml
中设置操作栏样式是 API的唯一方式 < 11
getSupportActionBar()?.setBackgroundDrawable(
ColorDrawable(Color.parseColor("#003459"))
)
像这样在onCreate中添加这个-
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getSupportActionBar()?.setBackgroundDrawable(
ColorDrawable(Color.parseColor("#003459"))
)
ActionBar bar = getActionBar(); // or MainActivity.getInstance().getActionBar()
bar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
bar.setDisplayShowTitleEnabled(false); // required to force redraw, without, gray color
bar.setDisplayShowTitleEnabled(true);
(全部归功于这篇文章,但它被埋在评论中,所以我想在这里浮出水面)
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MainColor)));
supportActionBar?.setBackgroundDrawable(ColorDrawable(ContextCompat.getColor(this,R.color.red)))
只需将其放入onCreate并根据您的需要更改颜色
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
public class ActivityUtils {
public static void setActionBarColor(AppCompatActivity appCompatActivity, int colorId){
ActionBar actionBar = appCompatActivity.getSupportActionBar();
ColorDrawable colorDrawable = new ColorDrawable(getColor(appCompatActivity, colorId));
actionBar.setBackgroundDrawable(colorDrawable);
}
public static final int getColor(Context context, int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 23) {
return ContextCompat.getColor(context, id);
} else {
return context.getResources().getColor(id);
}
}
}
从您的 MainActivity.java 中像这样更改操作栏颜色
ActivityUtils.setActionBarColor(this, R.color.green_00c1c1);
ActionBar actionbar = getActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable("color"));
当您扩展 AppCompatActivity 使用以下代码
ActionBar actionbar = getSupportActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable("color"));
ActionBar actionbar = getActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable("color"));
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
快乐编码
<item name="actionBarStyle">@style/BlackActionBar</item>
</style>
<style name="BlackActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">
<item name="background">@android:color/black</item>
<item name="titleTextStyle">@style/BlackActionBarTitleTextStyle</item>
</style>
<style name="BlackActionBarTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@android:color/white</item>
<item name="android:fontFamily">@font/cinzel_decorative</item>
</style>
androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable("Color"));
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
</style>
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- customize the color palette -->
<item name="colorPrimary">@color/material_blue_500</item>
<item name="colorPrimaryDark">@color/material_blue_700</item>
<item name="colorAccent">@color/material_blue_500</item>
<item name="colorControlNormal">@color/black</item>
</style>
<style name="CardViewStyle" parent="CardView.Light">
<item name="android:state_pressed">@color/material_blue_700</item>
<item name="android:state_focused">@color/material_blue_700</item>
<!--<item name="android:background">?android:attr/selectableItemBackground</item>-->
</style>
对于深色操作栏
<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:background">@color/colorDarkGrey</item>
</style>
for_LightActionBar
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.ActionBar">
<item name="android:background">@color/colorDarkGrey</item>
</style>
<?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>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/actionbar_background</item>
</style>
注意:此代码仅适用于 android 3.0 及更高版本
ActionBar actionbar = getActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable("color"));
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.TabColor)));
color.xml文件:
<resources> <color name="TabColor">#11FF00</color> </resources>`