导航到下一个时更改操作栏颜色及其文本对齐方式 activity

Change Actionbar color and its text alignment When navigating to next activity

我想在我的 Android 应用程序中更改 ActionBar 的颜色。

如果我点击按钮转到下一个 activity,我也想更改 Actionbar 颜色,但它显示 "Unfortunately, app has stopped"。

下面是我的代码,如果有什么问题请告诉我..?

我的代码:

舱单代码:

<application
    android:allowBackup="true"
    android:icon="@drawable/app_icon_152"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.Light">
    <activity
        android:name=".WelcomeScreen"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SignIn"
        android:label="Sign In"
        android:theme="@style/MyTheme"
        android:parentActivityName=".WelcomeScreen"></activity>
</application>

values/theme.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<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:titleTextStyle">@style/TitleBarTextColor</item>
    <item name="android:background">#3366CC</item>
    <item name="android:icon">@drawable/backarrow_white</item>
</style>
<style name="TitleBarTextColor"     parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">#ffffff</item>
</style>
</resources>

values/styles.xml代码:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowActionBar">true</item>
</style>
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@drawable/splashscreen</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">false</item>
</style>
</resources>

如有任何帮助,我们将不胜感激。提前致谢。

最佳做法是使用工具栏:See this android developers page

使用工具栏,您可以设置要使用的布局并更改背景颜色和文本对齐方式。

试试这个

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

你没有显示你的 java 代码,但试试这个

 ActionBar actionBar = getSupportActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#95CDBA")));
        actionBar.setTitle(Html.fromHtml("<font color='#000099'>Hello World</font>"));

并导入

android.support.v7.app.ActionBar actionBar = getSupportActionBar();