Android材质主题alpha颜色问题

时间:2015-03-20 07:49:05

标签: android android-theme material-design

我已经使用构建版本5.0创建了一个应用程序,我在下面写了主题

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimaryDark">@color/actionbar_color</item>
    <item name="colorPrimary">@color/actionbar_color</item>
    <item name="colorAccent">@color/actionbar_yellow_color</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:textColorPrimary">@color/edit_text_color</item>
    <item name="android:windowBackground">@drawable/ic_app_background</item>
</style>

当我设置actionbar_color =&#34;#4DFFFFFF&#34;我的应用程序崩溃了。如果我使用任何非alpha颜色,它的工作正常我的意思是如果我为actionbar_color =&#34;# FFFFFF &#34;设置颜色代码它的工作正常。

错误是:TaskDescription的主要颜色应该是不透明的 我已经检查了这个解决方案Android 5.0: howto change Overview Screen Task Title background color,但是对于alpha颜色有同样的错误。 我对此不透明度颜色Hex transparency in colors

进行了此操作

崩溃日志

03-20 05:59:41.323: E/AndroidRuntime(2133): FATAL EXCEPTION: main
03-20 05:59:41.323: E/AndroidRuntime(2133): Process: com.mytaxback, PID: 2133
03-20 05:59:41.323: E/AndroidRuntime(2133): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mytaxback/com.mytaxback.LoginActivity}: java.lang.RuntimeException: A TaskDescription's primary color should be opaque
03-20 05:59:41.323: E/AndroidRuntime(2133):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at android.os.Looper.loop(Looper.java:135)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at android.app.ActivityThread.main(ActivityThread.java:5221)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at java.lang.reflect.Method.invoke(Native Method)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at java.lang.reflect.Method.invoke(Method.java:372)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-20 05:59:41.323: E/AndroidRuntime(2133): Caused by: java.lang.RuntimeException: A TaskDescription's primary color should be opaque
03-20 05:59:41.323: E/AndroidRuntime(2133):     at android.app.ActivityManager$TaskDescription.(ActivityManager.java:536)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at android.app.Activity.onApplyThemeResource(Activity.java:3677)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:140)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:85)
03-20 05:59:41.323: E/AndroidRuntime(2133):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2244)
03-20 05:59:41.323: E/AndroidRuntime(2133):     ... 10 more

我在5.0 OS模拟器中遇到了这个问题。它适用于ICS,KITKAT 给我任何建议。 谢谢,

3 个答案:

答案 0 :(得分:1)

我为动作栏/工具栏和状态栏指定了背景颜色,这里,actionbar_color颜色是alpha颜色

        mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(mToolbar);
        TextView mTitle = (TextView) mToolbar.findViewById(R.id.toolbar_title);
        mTitle.setText(getString(R.string.login_title));
        mTitle.setTypeface(Oswald_Medium);
        mToolbar.setNavigationIcon(R.drawable.ic_back_arrow);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mToolbar.setBackgroundColor(getResources().getColor(R.color.actionbar_color));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.actionbar_color));
        }

我注意到当我使用eclipse IDE编写这个主题时,它会导致错误,同时在Android Studio中运行相同的alpha颜色。如果您发现我已经为Eclipse编写了上述代码的更好的解决方案,请在这里发表评论,不需要AndroidStudio的代码

答案 1 :(得分:1)

您不能在原色中使用alfa。 你可以看到android.app.ActivityManager#TaskDescription

的源代码
/**
    * Creates the TaskDescription to the specified values.
    *
    * @param label A label and description of the current state of this task.
    * @param icon An icon that represents the current state of this task.
    * @param colorPrimary A color to override the theme's primary color. This color must be opaque.
    */
    public TaskDescription(String label, Bitmap icon, int colorPrimary) {
      if ((colorPrimary != 0) && (Color.alpha(colorPrimary) != 255)) {
        throw new RuntimeException("A TaskDescription's primary color should be opaque");
      }

      mLabel = label;
      mIcon = icon;
      mColorPrimary = colorPrimary;
    }

答案 2 :(得分:0)

在Lollipop设备上遇到同样的问题,删除Alpha颜色修复了我的问题。请参阅此链接以了解问题:http://androidxref.com/7.1.1_r6/xref/frameworks/base/core/java/android/app/ActivityManager.java#982