我有以下设置:
我使用的是AppCompat
Fragment
时隐藏的RecyclerView
中具有相应的android:fitsSystemWindows="true"
问题是,在这种情况下,我无法使状态栏变得透明。我所做的如下:
创建activity并调用setContent
@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,并绘制在透明的导航栏后面。
布局
以下是我的主要活动:
<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>
这是我的片段,它将被放在主活动中:
<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>
编辑-屏幕截图
我使用亮/暗的基础主题和手工主题(因为用户可以选择任何颜色作为主色/强调色),所以不要介意工具栏是白色的(它是默认的主题背景色和主色)。我还添加了一个黑色边框,这样您就可以看到活动结束的位置……
最后,我当然会让工具栏和导航栏变得半透明,以获得更好的视觉效果。
发布于 2016-01-26 14:48:13
对我来说,原因不是它本身不工作,而是我使用Mike Penz的material抽屉库,这个库在工具栏后面使用全屏+偏移+自定义背景,所以我必须解决关于特殊设置的问题……
我会奖励那些在我看来信息最丰富的答案的分数…
发布于 2016-01-18 20:18:54
编辑您的styles.xml (v21),添加以下样式
<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文件中为特定活动声明此主题:
<activity
android:theme="@style/AppTheme.Home"
android:name=".HomeActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait" />
这将使您的内容在透明的操作栏下可见。
现在使用以下命令在StatusBar下方正确对齐工具栏,在oncreate中调用此命令:
toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
使用以下方法获取statusbar高度:
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
从您的协调器布局标记中删除以下内容:
android:fitsSystemWindows="true"
现在,为了折叠或隐藏工具栏,您可以参考this tutorial
请确保您使用的是以下版本的设计支持库,因为它没有bug:
compile 'com.android.support:design:23.1.0'
发布于 2016-01-25 19:10:48
在看了你对你的问题的描述后,我认为照片的样式符合你的要求。
好的,这里有一些关于你的问题的提示。在我测试之后,它就能工作了。
<item name="android:windowTranslucentStatus">true</item>
(即KitKat)<item name="android:windowTranslucentStatus">true</item>
(即),当Android版本级别大于19时需要在style中加入<item name="android:windowTranslucentNavigation">true</item>
(即KitKat)Toolbar
,内容向下滚动时要平滑显示Toolbar
。您需要根据当前的codes.Of课程将app:layout_collapseMode="parallax"
添加到Toolbar
的属性中,您需要使用CollapsingToolbarLayout
CoordinatorLayout
和AppBarLayout
.协调Toolbar
https://stackoverflow.com/questions/34761671
复制相似问题