无法使 AppBarLayout 透明

rob*_*ert 3 android android-layout android-appbarlayout

我想transparent AppBarLayout在我的应用程序中使用效果,应该很容易。

只是使backgroundAppBarLayout "@null"还是"@android:color:transparent"应该工作,但事实是我错了。

我在 AppBarLayout 中得到了奇怪的阴影。

AppBarLayout 背景不透明

我的布局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>
Run Code Online (Sandbox Code Playgroud)

活动代码

   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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

theme style该的活动"Theme.AppCompat.Light.NoActionBar"

有谁知道AppBarLayout transparent不工作的原因?

rob*_*ert 6

对于有同样问题的人,这里是解决方案:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     appBarLayout.setOutlineProvider(null);
}
Run Code Online (Sandbox Code Playgroud)

我在 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);
    .....
}
Run Code Online (Sandbox Code Playgroud)

源代码中的注释给出了我们得到阴影的确切原因。