无法使AppBarLayout透明化

3
我希望在我的应用程序中使用透明的AppBarLayout效果,这应该很容易。
只需将AppBarLayoutbackground设置为"@null""@android:color/transparent"即可,但事实上我错了。
AppBarLayout中出现了奇怪的阴影。

AppBarLayout background is not transparent

我的布局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>

活动代码

   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);
        }
    }
}

活动主题样式"Theme.AppCompat.Light.NoActionBar"

有人知道为什么AppBarLayout透明不起作用吗?


请使用android:background="@android:color/transparent"代替android:background="@null"。 - Shivam Kumar
2个回答

6

如果有人遇到相同的问题,这里是解决方案:

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

我在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);
    .....
}

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


还有在API 21之前的解决方案吗? - Bruno Bieri

0

不行,我已经尝试了很多次来解决这个问题,但是没有解决。你可以通过使用其他布局来避免这个问题。顺便说一下,在大于21的设备上显示阴影,但在小于21的设备上不显示阴影。


我已经解决了这个问题,源代码总是可以找到线索的正确位置。感谢您的答案。 - robert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接