首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >半透明/透明状态栏+ CoordinatorLayout +工具栏+片段

半透明/透明状态栏+ CoordinatorLayout +工具栏+片段
EN

Stack Overflow用户
提问于 2016-01-13 16:27:23
回答 9查看 50.9K关注 0票数 25

我有以下设置:

我使用的是AppCompat

  • MainActivity,,它包含一个片段和一个工具栏,在滚动down

  • Fragment时隐藏的RecyclerView

  • all视图应该适合屏幕在

中具有相应的android:fitsSystemWindows="true"

问题是,在这种情况下,我无法使状态栏变得透明。我所做的如下:

创建activity并调用setContent

  • Then I
  1. 尝试调整activity以编程方式获得半透明工具栏,如下所示:

@TargetApi(Build.VERSION_CODES.LOLLIPOP) public void themeNavAndStatusBar(Activity activity) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) themeNavAndStatusBar;Window w= activity.getWindow();TargetApi w.setFlags( TargetApiw.setFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);w.setNavigationBarColor(activity.getResources().getColor(android.R.color.transparent));w.setStatusBarColor(activity.getResources().getColor(android.R.color.transparent));}

  • 将活动(@+id/frame_container)中的占位符替换为片段

在这种情况下,状态栏是纯色的,视图不是在它下面绘制的。为什么?

我想要什么

我想要一个工具栏,这是滚动的屏幕和完全隐藏,而这个工具栏下的内容应该fitScreen,并绘制在透明的导航栏后面。

布局

以下是我的主要活动:

代码语言:javascript
复制
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/clMain"
    android:fitsSystemWindows="true"
    android:background="?attr/main_background_color"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:fitsSystemWindows="true"
        android:background="@null"
        app:elevation="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="?actionBarThemeStyle"
            app:popupTheme="?actionBarPopupThemeStyle"
            app:layout_scrollFlags="scroll|enterAlways">

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

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:id="@+id/ivToolbarDataSource"
                        android:layout_gravity="center_vertical"
                        android:layout_marginRight="2dp"
                        android:layout_width="24dp"
                        android:layout_height="24dp" />

                    <TextView
                        android:id="@+id/tvToolbarTitle"
                        style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                        android:theme="?actionBarThemeStyle"
                        android:layout_gravity="center_vertical"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                </LinearLayout>

                <TextView
                    android:id="@+id/tvToolbarSubTitle"
                    style="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle"
                    android:theme="?actionBarThemeStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>



        </android.support.v7.widget.Toolbar>

        <!-- BUG: http://stackoverflow.com/questions/30541409/coordinatorlayoutappbarlayout-does-not-draw-toolbar-properly -->
        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"/>

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

    <FrameLayout
        android:id="@+id/frame_container"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="32dp"
        android:src="@drawable/ic_local_offer_white_24dp"
        app:backgroundTint="?attr/colorPrimary"
        app:borderWidth="0dp"
        app:fabSize="normal"
        app:rippleColor="?attr/colorPrimaryDark"
        app:layout_anchorGravity="bottom|right|end"
        app:layout_behavior="com.test.classes.ScrollAwareFABBehavior"/>

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

这是我的片段,它将被放在主活动中:

代码语言:javascript
复制
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/srlImages"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvImages"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.SwipeRefreshLayout>

    <TextView
        android:id="@+id/tvEmpty"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

编辑-屏幕截图

我使用亮/暗的基础主题和手工主题(因为用户可以选择任何颜色作为主色/强调色),所以不要介意工具栏是白色的(它是默认的主题背景色和主色)。我还添加了一个黑色边框,这样您就可以看到活动结束的位置……

  • 第一个屏幕截图:显示工具栏,nothing is scrolled
  • Second屏幕截图:我刚开始滚动工具栏,现在应该滚动离开
  • 第三个屏幕截图:主要内容现在应该滚动到导航栏下面...

最后,我当然会让工具栏和导航栏变得半透明,以获得更好的视觉效果。

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2016-01-26 14:48:13

对我来说,原因不是它本身不工作,而是我使用Mike Penz的material抽屉库,这个库在工具栏后面使用全屏+偏移+自定义背景,所以我必须解决关于特殊设置的问题……

我会奖励那些在我看来信息最丰富的答案的分数…

票数 1
EN

Stack Overflow用户

发布于 2016-01-18 20:18:54

编辑您的styles.xml (v21),添加以下样式

代码语言:javascript
复制
 <style name="AppTheme.Home" parent="AppTheme.Base">
    <!-- Customize your theme here. -->
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:windowTranslucentNavigation">true</item>

</style>

您可以根据自己的喜好更改父主题,但现在可以在AndroidManifest.xml文件中为特定活动声明此主题:

代码语言:javascript
复制
 <activity
        android:theme="@style/AppTheme.Home"
        android:name=".HomeActivity"
        android:launchMode="singleTop"
        android:screenOrientation="portrait" />

这将使您的内容在透明的操作栏下可见。

现在使用以下命令在StatusBar下方正确对齐工具栏,在oncreate中调用此命令:

代码语言:javascript
复制
 toolbar.setPadding(0, getStatusBarHeight(), 0, 0);

使用以下方法获取statusbar高度:

代码语言:javascript
复制
public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

从您的协调器布局标记中删除以下内容:

代码语言:javascript
复制
android:fitsSystemWindows="true"

现在,为了折叠或隐藏工具栏,您可以参考this tutorial

请确保您使用的是以下版本的设计支持库,因为它没有bug:

compile 'com.android.support:design:23.1.0'

票数 13
EN

Stack Overflow用户

发布于 2016-01-25 19:10:48

在看了你对你的问题的描述后,我认为照片的样式符合你的要求。

好的,这里有一些关于你的问题的提示。在我测试之后,它就能工作了。

  • 如果要在状态栏后面显示内容,需要在Android版本级别大于19时加入<item name="android:windowTranslucentStatus">true</item> (即KitKat)
  • If ),当Android版本级别大于19时需要在style中加入<item name="android:windowTranslucentStatus">true</item>(即),当Android版本级别大于19时需要在style中加入<item name="android:windowTranslucentNavigation">true</item> (即KitKat)
    • If ),当内容向上滚动时要平滑隐藏Toolbar,内容向下滚动时要平滑显示Toolbar。您需要根据当前的codes.Of课程将app:layout_collapseMode="parallax"添加到Toolbar的属性中,您需要使用CollapsingToolbarLayout CoordinatorLayoutAppBarLayout.

    协调Toolbar

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34761671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档