Android 使工具栏透明,就像在报摊应用程序中一样

Android 使工具栏透明,就像在报摊应用程序中一样,android,android-layout,android-toolbar,Android,Android Layout,Android Toolbar,我试图使我的工具栏透明,如下图所示。我试图通过编程实现这一点 android中半透明和透明工具栏的区别是什么 请参考下面的代码和下面的图片,了解我到目前为止的情况。请给我指一下正确的方向 public void hideToolbar() { // toolbar.setVisibility(View.GONE); toolbar.getBackground().setAlpha(0); if (Build.VERSION.SDK_INT > Build.VER

我试图使我的工具栏透明,如下图所示。我试图通过编程实现这一点

android中半透明和透明工具栏的区别是什么

请参考下面的代码和下面的图片,了解我到目前为止的情况。请给我指一下正确的方向

public void hideToolbar() {
    // toolbar.setVisibility(View.GONE);
    toolbar.getBackground().setAlpha(0);

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
}

public void showToolbar() {
    // toolbar.setVisibility(View.VISIBLE);
    toolbar.getBackground().setAlpha(255);

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {

        WindowManager.LayoutParams attrs = getWindow().getAttributes();
        attrs.flags &= (~WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        attrs.flags &= (~WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setAttributes(attrs);
        getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

        getWindow().setStatusBarColor(
                Color.parseColor(Const.Colors.COLOR_STATUS_BLUE));
    }
}


我希望工具栏和状态栏都是透明的。如果您需要我的XML或其他代码,请告诉我。

我可以让您清楚地了解透明和半透明之间的区别

  • 透明意味着您可以完全看穿对象。我们可以 以玻璃为例
  • 半透明是指介于不透明和透明之间的状态,您可以 也称之为半透明。从半透明对象中,您不能 透过它后面的图像看得很清楚,但一定量的光可以 穿过那个物体。我们可以拿纸作为半透明的例子 对象当我们把纸放在光源上方(例如手电筒)时, 你可以看到光,但看不到火炬

我不是android编码专家,但我告诉了你我对android编码的了解。

为了避免工具栏后面内容的颜色与工具栏元素/文本的颜色发生冲突(例如向上/向后箭头),工具栏的许多实现实际上不是透明的,而是带有渐变的半透明

您可以通过下一个代码获得此信息:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar
    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:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/background_toolbar_translucent" />

背景\工具栏\半透明.xml

<?xml version="1.0" encoding="utf-8"?>

<shape
    xmlns:android="http://schemas.android.com/apk/res/android">

    <gradient
        android:angle="270"
        android:endColor="@android:color/transparent"
        android:startColor="@color/black_alpha_40"/>
</shape>

colors.xml

<color name="black_alpha_40">#66000000</color>
66000000

您可以在渐变上使用不同的值,我发现对于白色元素,黑色的40%效果很好。

您是否尝试过
toolbar.setBackgroundResource(Color.TRANSPARENT)