安卓如何更改后台操作栏



嗨,我会更改我的操作栏背景颜色。我找到了很多教程,但是当我启动我的应用程序时,它崩溃了,给我错误消息。

这就是我所做的。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>

用户活动和添加项活动是显示操作栏的活动。

我在/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>

当我启动用户活动时,我的应用程序崩溃,出现此错误:

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的样式进行这两项活动时,会出现问题。怎么了?

您在异常消息中有答案

    You need to use a Theme.AppCompat theme (or descendant) with this activity.

使用Theme.AppCompat。

此外,您可以使用工具栏自定义操作栏您可以在布局xml(如标准视图)中添加它,并通过setSupportActionBar(toolbarObject)方法在活动中设置它,如下所示

布局.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>

你的活动.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 主题

>您可以尝试在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 );
    }
}

主要活动的继承更改为活动(导入android.app.Activity),我认为它解决了使用自定义主题的问题

相关内容

  • 没有找到相关文章

最新更新