【问题标题】:Translucent StatusBar on kitkat with FrameLayout above the Toolbar and using CoordinatorLayoutkitkat 上的半透明状态栏,工具栏上方有 FrameLayout 并使用 CoordinatorLayout
【发布时间】:2016-02-05 11:04:44
【问题描述】:

我试图在kitkat 上使用以下FrameLayout 实现Translucent StatusBar,它的工作没有 CoordinatorLayout 像这样:

Layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="1">

    <FrameLayout
        android:id="@+id/statusbar"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:background="@color/colorPrimaryDark" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="@color/colorPrimary" />

</LinearLayout>

当然还有Styles.xml-v19 中的这一行:

<item name="android:windowTranslucentStatus">true</item>

当然还有onCreate

if (Build.VERSION.SDK_INT < 19) {

            FrameLayout statusbar = (FrameLayout) findViewById(R.id.statusbar);
            statusbar.setVisibility(View.GONE);
        }

这很有效:

但是,我想知道我们如何使用 CoordinatorLayoutAppbarLayout 实现这一目标?

这是我的尝试:

<android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbarCollapse"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <FrameLayout
                    android:id="@+id/statusbar"
                    android:layout_width="match_parent"
                    android:layout_height="25dp"
                    android:background="?attr/colorPrimaryDark" />

                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbarmain"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="@color/ColorPrimary"
                        app:layout_scrollFlags="scroll|enterAlways"
                        app:popupTheme="@style/ToolbarStylepopuptheme"
                        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />


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

            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:tabIndicatorColor="@android:color/white"
                app:tabIndicatorHeight="2dp"
                app:tabMode="scrollable" />

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

而且没用。

更新:这个也不起作用:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ui.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/statusBar"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:background="@color/colorPrimaryDark"
            android:translationZ="8dp" />

        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/root_coordinator"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.widget.NestedScrollView
                android:id="@+id/nestedscrollview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

...
...

有什么解决方案可以实现吗?

类似于ADM app on GooglePlay 设计。

另外,让我们提一下,我在使用CoordinatorLayout 时遇到了与SystemBarTint 相同的问题(相同条件)。

我不知道他们是怎么做到的。

另外,这里有一个很好的教程,https://youtu.be/YCThIedqbCQ,但它对我不起作用。

【问题讨论】:

    标签: android android-4.4-kitkat android-coordinatorlayout android-statusbar


    【解决方案1】:

    你不能这样做吗?似乎您在不需要时将人造状态栏移动到协调器布局中?

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="1">
    
    <FrameLayout
        android:id="@+id/statusbar"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:background="@color/colorPrimaryDark" />
    
    <Coordinator>
        <AppBarLayout>
            <CollapsibleToolbarLayout>
                <Toolbar/>
            </CollapsibleToolbarLayout>
        </AppBarLayout>
        <NestedScrollView/>
    </Coordinator>
    </LinearLayout>
    

    【讨论】:

    • 将它包裹在FrameLayout 中,这样状态栏FrameLayout 就在上面,所有东西都在它下面滑动。
    • 这完全错过了 fitSystemWindows 的意义......但考虑到它的严重程度,我不会责怪任何使用这种解决方法的人。
    • @Su-AuHwang - 那么,你的建议是什么?
    • @Mohsen 我只是决定没有 API
    猜你喜欢
    • 2015-08-06
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    相关资源
    最近更新 更多