我正在尝试自定义应用的操作栏,但我无法正确操作,我不知道为什么。
我已经定义了我的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)));
on 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.access$600(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.access$600(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.access$600(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.access$600(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
}
});
}
我的文件结构是......
答案 0 :(得分:0)
编辑:见评论
如果您使用的是Android Studio的默认项目模板,则材质主题也会values-v21/styles.xml
。您还需要更改它以便在Lollipop设备/模拟器上显示它。
如果您正在查看Android Studio的设计标签,则可能需要设置在渲染布局时使用的&#34; Android版本&#34;低于API 21的版本。
答案 1 :(得分:0)
如果您的minSdkVersion
11或更高,请写
getActionBar().setBackgroundDrawable(new ColorDrawable("COLOR"));
在.class
文件的onCreate()
方法中。
现在你的班级应该是这样的,
Class MyActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity);
getActionBar().setBackgroundDrawable(new ColorDrawable("YourColor"));
}
}
答案 2 :(得分:0)
(代表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)));