您好我会更改我的操作栏背景颜色。我发现了很多教程但是当我启动我的应用程序时它会崩溃给我错误信息。
这就是我所做的。 的AndroidManifest.xml:
<activity
android:name=".UserActivity"
android:label="@string/app_name"
android:parentActivityName="com.example.securepassword.MainActivity"
android:theme="@style/MyTheme" >
<intent-filter>
<action android:name="user" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".AddItemActivity"
android:label="@string/app_name"
android:parentActivityName="com.example.securepassword.UserActivity"
android:theme="@style/MyTheme" >
<intent-filter>
<action android:name="add_item" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
UserActivity和AddItemActivity是操作栏出现的活动。
我在/res/values-v11/styles.xml
中创建了我的个人风格<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">#0086D4</item>
</style>
当我启动UserActivity时,我的应用程序崩溃,给我这个错误:
01-10 12:53:08.337: E/AndroidRuntime(15095): FATAL EXCEPTION: main
01-10 12:53:08.337: E/AndroidRuntime(15095): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.securepassword/com.example.securepassword.UserActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.os.Looper.loop(Looper.java:137)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-10 12:53:08.337: E/AndroidRuntime(15095): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 12:53:08.337: E/AndroidRuntime(15095): at java.lang.reflect.Method.invoke(Method.java:525)
01-10 12:53:08.337: E/AndroidRuntime(15095): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-10 12:53:08.337: E/AndroidRuntime(15095): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-10 12:53:08.337: E/AndroidRuntime(15095): at dalvik.system.NativeStart.main(Native Method)
01-10 12:53:08.337: E/AndroidRuntime(15095): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
01-10 12:53:08.337: E/AndroidRuntime(15095): at com.example.securepassword.UserActivity.onCreate(UserActivity.java:17)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.Activity.performCreate(Activity.java:5133)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-10 12:53:08.337: E/AndroidRuntime(15095): ... 11 more
问题出现在androidmanifest中我使用MyTheme样式进行两种活动......有什么不对?
答案 0 :(得分:1)
您已在异常消息中回答
You need to use a Theme.AppCompat theme (or descendant) with this activity.
使用Theme.AppCompat。
此外,您可以使用工具栏自定义操作栏 您可以在布局xml中添加它,例如标准视图,并通过setSupportActionBar(toolbarObject)方法在活动中设置它,如下所示
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:elevation="5dp"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<!-- your layout -->
</LinearLayout>
YourActivty.class
public class YourActivty extends ActionBarActivity {
Toolbar mToolbar;
public void onCreate(Bundle savedInstanceState) {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
// configure toolbar stuff
setSupportActionBar(mToolbar);
}
}
但是当你想使用工具栏时。您必须使用Theme.AppCompat.NoActionBar主题
答案 1 :(得分:0)
您可以尝试使用Activity
中的代码:
ActionBar ab = getActionBar();
ab.setHomeButtonEnabled(true);
ColorDrawable colorDrawable = new ColorDrawable(
Color.parseColor("#0086D4"));
ab.setBackgroundDrawable(colorDrawable);
要更改ActionBar
上文字的颜色,请使用此处:
ab.setTitle(Html.fromHtml("<font color='#YOUR_COLORCODE'>ActionBarTitle </font>"));
或强>
int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if (actionBarTitleId > 0) {
TextView title = (TextView) findViewById(actionBarTitleId);
if (title != null) {
title.setTextColor(YOUR COLOR );
}
}
答案 2 :(得分:0)
将主活动的继承更改为Activity(导入android.app.Activity),我认为它解决了使用自定义主题的问题