简体   繁体   中英

Android UI glitch when setting ActionBar color

Together with friends I have created an Android app and I have a strange issue connected with the UI.

When switching between fragments sometimes the UI tends to scramble, create a glitch displayed below.

在此处输入图片说明

It happens on my Nexus 5 with Dalvik runtime and stock Android and on my friend's Nexus 4 with ART runtime and SlimKat rom. But it is not observable on another Nexus 5 with Dalvik runtime and 4.4.2 (AOSB rom). The weird thing is that it was not happening before I updated to the newest version of the app.

Below is the layout used, where the main_fragment is the place I put the Fragment with the use of transaction.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/main_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</RelativeLayout>

I found out that the glitch appears when this line is executed and it does not matter what I put inside the setBackgroundDrawable method:

activity.getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getCurrentTheme(activity))));

Does anybody else have this problem? Or maybe someone knows a workaround or how to make it work?

Edit: I need to change the ActionBar color in runtime (changing theme in app settings).

I encountered this problem a few days ago when applying a background color to the ActionBar at runtime. This was on a Nexus 5 running stock Android 5.0, and I had success with the workaround suggested in this reply of this issue: Issue 1760

The workaround is to disable Hardware Acceleration either globally or for the particular Activity where this occurs. There is one more possibility, which is to disable hardware acceleration for a View at runtime, but I'm not sure how to apply this to the ActionBar.

Disable hardware acceleration globally

AndroidManifest.xml :

<application android:hardwareAccelerated="false" ...>

Disable hardware acceleration for an Activity

AndroidManifest.xml :

<application android:hardwareAccelerated="true">
    <activity ... />
    <activity android:hardwareAccelerated="false" />
</application>

Disable hardware acceleration for a View

At runtime

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM