自定义 Android 操作栏
Customizing the Android action bar
我正在尝试自定义我的应用程序的操作栏,但我无法正确执行,我也不知道为什么。
我这样定义我的 themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/ActionBar.Solid.Example</item>
</style>
<style name = "ActionBar.Solid.Example" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/ab_solid_example</item>
</style>
</resources>
我们称之为样式的AndroidManifest.xml是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="coursera.momaproject" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
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>
按理说,我们必须看到紫色的操作栏背景,但我们看到的是她的黑色背景的操作栏
发生了什么事?我做错了什么?
如果我从 AndroidManifest.xml 中删除 android:theme="@style/AppTheme" 并添加 getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.background_alert)));
在 onCreate() 方法上我遇到了下一个错误:
02-10 12:50:56.458 2512-2512/coursera.momaproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{coursera.momaproject/coursera.momaproject.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at coursera.momaproject.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
如果我恢复我的 AndroidManifest.xml 并删除 actionBar 自定义,我的应用程序会在我 运行 时给我一个错误。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="coursera.momaproject" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
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>
Themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
</resources>
我遇到了这个错误:
02-10 13:42:33.955 2574-2574/coursera.momaproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{coursera.momaproject/coursera.momaproject.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at coursera.momaproject.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
onCreate() 方法,其中 ** 行产生错误 ...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
**getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.background_alert)));**
//We add the footer view
LinearLayout container = (LinearLayout)findViewById(R.id.container);
View seekBarView = getLayoutInflater().inflate(R.layout.footer_view, null);
container.addView(seekBarView);
topLeft = (TextView)findViewById(R.id.topLeft);
bottomLeft = (TextView)findViewById(R.id.bottomLeft);
topRight = (TextView)findViewById(R.id.topRight);
bottomRight = (TextView)findViewById(R.id.bottomRight);
SeekBar seekBar = (SeekBar)findViewById(R.id.slider);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Integer newColor = progressColor(progress, ORIGIN_TOP_LEFT_COLOR, TARGET_TOP_LEFT_COLOR);
topLeft.setBackgroundColor(newColor.intValue());
newColor = progressColor(progress, ORIGIN_BOTTOM_LEFT_COLOR, TARGET_BOTTOM_LEFT_COLOR);
bottomLeft.setBackgroundColor(newColor.intValue());
newColor = progressColor(progress, ORIGIN_TOP_RIGHT_COLOR, TARGET_TOP_RIGHT_COLOR);
topRight.setBackgroundColor(newColor.intValue());
newColor = progressColor(progress, ORIGIN_BOTTOM_RIGHT_COLOR, TARGET_BOTTOM_RIGHT_COLOR);
bottomRight.setBackgroundColor(newColor.intValue());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//We don't do anything
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//We don't do anything
}
});
}
我的文件结构是...
编辑:查看评论
如果您使用 Android Studio 的默认项目模板,material 主题也有 values-v21/styles.xml
。您还需要更改它才能在 Lollipop 设备/模拟器上显示。
如果您正在查看 Android Studio 的设计选项卡,您可能希望将 "Android version to use when rendering layouts" 设置为低于 API 21 的版本。
如果你的 minSdkVersion
是 11 或更高 那么写
getActionBar().setBackgroundDrawable(new ColorDrawable("COLOR"));
在您的 .class
文件的 onCreate()
方法中。
现在你的 class 应该看起来像,
Class MyActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity);
getActionBar().setBackgroundDrawable(new ColorDrawable("YourColor"));
}
}
(代表OP发表).
最后,感谢@Maurice Lam,我找到了解决问题的正确方法。
解决方法是:
- 将
MainActivity
的继承从 ActionBarActivity
更改为 Activity
将AppTheme的样式由Theme.AppCompat.Light.DarkActionBar
更改为@android:style/Theme.Holo.Light.DarkActionBar
:
themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppThem" parent="@android:style/Theme.Holo.Light.DarkActionBar"></style>
</resources>
在onCreate()方法中,现在你可以这样定义ActionBar的颜色:
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.background_alert)));
我正在尝试自定义我的应用程序的操作栏,但我无法正确执行,我也不知道为什么。
我这样定义我的 themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/ActionBar.Solid.Example</item>
</style>
<style name = "ActionBar.Solid.Example" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/ab_solid_example</item>
</style>
</resources>
我们称之为样式的AndroidManifest.xml是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="coursera.momaproject" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
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>
按理说,我们必须看到紫色的操作栏背景,但我们看到的是她的黑色背景的操作栏
发生了什么事?我做错了什么?
如果我从 AndroidManifest.xml 中删除 android:theme="@style/AppTheme" 并添加 getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.background_alert)));
在 onCreate() 方法上我遇到了下一个错误:
02-10 12:50:56.458 2512-2512/coursera.momaproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{coursera.momaproject/coursera.momaproject.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at coursera.momaproject.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
如果我恢复我的 AndroidManifest.xml 并删除 actionBar 自定义,我的应用程序会在我 运行 时给我一个错误。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="coursera.momaproject" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
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>
Themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
</resources>
我遇到了这个错误:
02-10 13:42:33.955 2574-2574/coursera.momaproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{coursera.momaproject/coursera.momaproject.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at coursera.momaproject.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
onCreate() 方法,其中 ** 行产生错误 ...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
**getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.background_alert)));**
//We add the footer view
LinearLayout container = (LinearLayout)findViewById(R.id.container);
View seekBarView = getLayoutInflater().inflate(R.layout.footer_view, null);
container.addView(seekBarView);
topLeft = (TextView)findViewById(R.id.topLeft);
bottomLeft = (TextView)findViewById(R.id.bottomLeft);
topRight = (TextView)findViewById(R.id.topRight);
bottomRight = (TextView)findViewById(R.id.bottomRight);
SeekBar seekBar = (SeekBar)findViewById(R.id.slider);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Integer newColor = progressColor(progress, ORIGIN_TOP_LEFT_COLOR, TARGET_TOP_LEFT_COLOR);
topLeft.setBackgroundColor(newColor.intValue());
newColor = progressColor(progress, ORIGIN_BOTTOM_LEFT_COLOR, TARGET_BOTTOM_LEFT_COLOR);
bottomLeft.setBackgroundColor(newColor.intValue());
newColor = progressColor(progress, ORIGIN_TOP_RIGHT_COLOR, TARGET_TOP_RIGHT_COLOR);
topRight.setBackgroundColor(newColor.intValue());
newColor = progressColor(progress, ORIGIN_BOTTOM_RIGHT_COLOR, TARGET_BOTTOM_RIGHT_COLOR);
bottomRight.setBackgroundColor(newColor.intValue());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//We don't do anything
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//We don't do anything
}
});
}
我的文件结构是...
编辑:查看评论
如果您使用 Android Studio 的默认项目模板,material 主题也有 values-v21/styles.xml
。您还需要更改它才能在 Lollipop 设备/模拟器上显示。
如果您正在查看 Android Studio 的设计选项卡,您可能希望将 "Android version to use when rendering layouts" 设置为低于 API 21 的版本。
如果你的 minSdkVersion
是 11 或更高 那么写
getActionBar().setBackgroundDrawable(new ColorDrawable("COLOR"));
在您的 .class
文件的 onCreate()
方法中。
现在你的 class 应该看起来像,
Class MyActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity);
getActionBar().setBackgroundDrawable(new ColorDrawable("YourColor"));
}
}
(代表OP发表).
最后,感谢@Maurice Lam,我找到了解决问题的正确方法。
解决方法是:
- 将
MainActivity
的继承从ActionBarActivity
更改为Activity
将AppTheme的样式由
Theme.AppCompat.Light.DarkActionBar
更改为@android:style/Theme.Holo.Light.DarkActionBar
:themes.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppThem" parent="@android:style/Theme.Holo.Light.DarkActionBar"></style> </resources>
在onCreate()方法中,现在你可以这样定义ActionBar的颜色:
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.background_alert)));