android 如何更改背景操作栏
android how change background action bar
您好,我想更改我的操作栏背景颜色。我找到了很多教程,但是当我启动我的应用程序时它崩溃并给我错误消息。
这就是我所做的。
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.access0(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 的样式时出现问题..怎么了?
您可以像这样在 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 );
}
}
将您的主要 activity 的继承更改为 Activity(导入 android.app.Activity),我认为它解决了使用自定义主题的问题
您在异常消息中有答案
You need to use a Theme.AppCompat theme (or descendant) with this activity.
使用Theme.AppCompat。
此外,您还可以使用工具栏来自定义您的操作栏
您可以将其添加到布局 xml 中,例如标准视图,并通过 setSupportActionBar(toolbarObject) 方法将其设置在您的 activity 中,如下所示
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 主题
您好,我想更改我的操作栏背景颜色。我找到了很多教程,但是当我启动我的应用程序时它崩溃并给我错误消息。
这就是我所做的。 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.access0(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 的样式时出现问题..怎么了?
您可以像这样在 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 );
}
}
将您的主要 activity 的继承更改为 Activity(导入 android.app.Activity),我认为它解决了使用自定义主题的问题
您在异常消息中有答案
You need to use a Theme.AppCompat theme (or descendant) with this activity.
使用Theme.AppCompat。
此外,您还可以使用工具栏来自定义您的操作栏 您可以将其添加到布局 xml 中,例如标准视图,并通过 setSupportActionBar(toolbarObject) 方法将其设置在您的 activity 中,如下所示
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 主题