【问题标题】:Transparent status bar with collapsing toolbar in api 19(KitKat)api 19(KitKat)中带有折叠工具栏的透明状态栏
【发布时间】:2017-01-11 18:26:04
【问题描述】:

我正在创建带有折叠工具栏和透明状态栏的应用程序。但是我遇到了 api 19 的问题 当我设置<item name="android:windowTranslucentStatus">true</item> 我得到这个:

我试图通过

getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

但唯一有效的是

Window w = getWindow(); w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

但之后我收到应用栏在状态栏下

我的应用栏和折叠工具栏布局有参数fitsSystemWindows = "true"

在 Api 21 及更高版本中一切正常。

如何在 api 19 中使用折叠工具栏布局制作透明状态栏? 感谢您的帮助。

【问题讨论】:

    标签: android transparent android-collapsingtoolbarlayout android-statusbar


    【解决方案1】:

    我通过这段代码解决了我的问题,它对我来说非常有效:

    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            Window w = getWindow(); // in Activity's onCreate() for instance
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
            CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) mToolBar.getLayoutParams();
            lp.setMargins(0,24,0,0);
            mToolBar.setLayoutParams(lp);
        }
    

    我的 mainActivity.xml 文件是:

            .......blablalba
    <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:fitsSystemWindows="true">
    
            <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginBottom="32dp"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
                android:fitsSystemWindows="true">
    
                <ImageView
                android:layout_width="match_parent"
                android:contentDescription="@string/app_name"
                android:id="@+id/ivHeader"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentBottom="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:layout_gravity="bottom"
                android:layout_height="235dp" />
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                app:layout_collapseMode="pin"
                android:layout_gravity="top"
                android:background="@android:color/transparent" />
    
            </android.support.design.widget.CollapsingToolbarLayout>
    
        </android.support.design.widget.AppBarLayout>
    .....blalblaba
    

    【讨论】:

      【解决方案2】:

      您必须将 fitsSystemWindows = "true" 更改为 fitsSystemWindows = "false" 才能正常工作。

      并且不要编写任何java代码来制作全屏, 只需在 styles.xml 和 v21 styles.xml

      中添加以下行
      <item name="android:windowFullscreen">true</item>
      

      【讨论】:

      • 我试过了,但并没有改变任何东西(也许你知道在哪里可以看到工作示例?
      • 那你想怎么样?给我看看你的活动 xml 代码?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多