robert
robert

Reputation: 550

Failed to make AppBarLayout transparent

I want to use transparent AppBarLayout effect in my app, it should be very easy.

Just make the background of AppBarLayout "@null" or "@android:color:transparent" should work, but the truth is I'm wrong.

I got weird shadow in AppBarLayout.

AppBarLayout background is not transparent

My layout xml:

   <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways" />

    </android.support.design.widget.AppBarLayout>


</android.support.design.widget.CoordinatorLayout>

Activity code

   public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setBackgroundDrawableResource(R.drawable.bg_005);
        setTranslucent(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
    }

    public static void setTranslucent(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = activity.getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }
}

The theme style of the activity is "Theme.AppCompat.Light.NoActionBar"

Anyone know the reason why the AppBarLayout transparent don't work?

Upvotes: 3

Views: 1146

Answers (2)

robert
robert

Reputation: 550

For someone who had the same problem, here is the solution:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     appBarLayout.setOutlineProvider(null);
}

I found the clue in the source code of AppBarLayout

if (Build.VERSION.SDK_INT >= 21) {
    // Use the bounds view outline provider so that we cast a shadow, even without a
    // background
    ViewUtilsLollipop.setBoundsViewOutlineProvider(this);
    .....
}

the comments in the source code gave the exact reason why we got shadows.

Upvotes: 6

Srinivas Nahak
Srinivas Nahak

Reputation: 1876

No you can't even I've tried a lot to solve this issue but it was not resolved . You can avoid this issue by using some other layouts . By the way it shows shadow in >21 devices but in <21 devices the shadow is not shown .

Upvotes: 0

Related Questions