自定义ActionBar主题无法工作

5

我想要自定义操作栏的外观,但似乎不起作用。尽管我明确将其设置为红色,但背景仍显示为灰色。

我的 build.gradle 文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.eliddell.rapsheet"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    wearApp project(':wear')
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.google.android.gms:play-services:7.0.0'
}

styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
    </style>
    <style name="CustomActionBarTheme"
        parent="@style/AppTheme">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>
    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@style/CustomActionBarTheme">
        <item name="android:background">@color/red</item>
    </style>
</resources>

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eliddell.rapsheet" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme" >
        <activity
            android:name=".MainActivity"
            android:theme="@style/CustomActionBarTheme"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

主活动

    import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

如果我更新我的样式为:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light">
        <!-- Customize your theme here. -->
    </style>

    <style name="CustomActionBarTheme"
        parent="@style/AppTheme">
        <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">@color/red</item>
    </style>

</resources>

然后我简要地看到了颜色变化,但应用程序崩溃了:

由于:java.lang.IllegalStateException: 您需要使用Theme.AppCompat主题(或其后代)来启动此活动。

3个回答

14

好的,我搞清楚了,感谢你们的帮助。问题在于我的样式xml文件指向了“android:background”和“android:actionBarStyle”,但是因为使用了支持库,“android:”被删除了。

<resources>

<!-- Base application theme. -->
<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>



<!-- ActionBar styles -->
<style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="android:windowBackground">@color/sapient_heat</item>

    <item name="background">@color/red</item>

</style>


1
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= 11) {
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
    } else {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }
    setContentView(R.layout.activity_main);
    if (Build.VERSION.SDK_INT >= 11) {
        ActionBar actionBar = this.getActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color
                .parseColor("#ff0000")));
        actionBar.show();
    }

}

在SDK版本11之后,使用ActionBar,获取ActionBar实例,然后进行任何操作。


你是在说不要使用主题吗? - erik

0

在 Android Manifest 中使用主题:

android:theme="@style/AppTheme"

在活动中扩展它

public class MainActivity extends ActionBarActivity{

将自定义视图作为操作栏展开:

// Inflate your custom layout
        final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
                R.layout.actionbar_layout,
                null);
        //Set Custom ActionBar
        actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(actionBarLayout);

其中actionbar_layout是用于你的ActionBar的布局XML文件


你在Android清单文件中使用过AppTheme吗?并且在onCreate()方法中加入以下代码:ActionBar actionBar = getSupportActionBar(); - Pankaj
请使用以下样式:<style name="AppTheme1" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowBackground">@android:color/black</item> </style> - Pankaj
actionbaractivity无法解决..我不理解你的解决方案。 - erik
这会改变整个活动的背景,而不是操作栏。 - erik
将自定义视图展开为:'// 展开您的自定义布局 final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate( R.layout.actionbar_layout, null); // 设置自定义 ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowTitleEnabled(false); actionBar.setDisplayShowCustomEnabled(true); actionBar.setCustomView(actionBarLayout);' - Pankaj
这不是开发者.android建议的做法...这太混乱了。 - erik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接