我已经将我的statusBar颜色设置为Lollipop的透明颜色,仅在我的主题中使用以下行:
<item name="android:statusBarColor">@android:color/transparent</item>
现在我需要在它背后绘制,但我不能得到它背后的任何视图。我知道如何使用windowTranslucentStatus
属性,但不想使用此属性,因为它将忽略statusBar设置为透明的颜色。
方法#1:
要实现完全透明的状态栏,您必须使用statusBarColor
,它仅适用于API 21及更高版本。 windowTranslucentStatus
可在API 19及更高版本上使用,但它为状态栏添加了有色背景。但是,设置windowTranslucentStatus
确实实现了将statusBarColor
更改为透明的一件事:它设置了SYSTEM_UI_FLAG_LAYOUT_STABLE
和SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
标志。获得相同效果的最简单方法是手动设置这些标志,这有效地禁用Android布局系统强加的插件,让您自己照顾自己。
你在onCreate
方法中调用这一行:
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
一定要在/res/values-v21/styles.xml中设置透明度:
<item name="android:statusBarColor">@android:color/transparent</item>
或者以编程方式设置透明度:
getWindow().setStatusBarColor(Color.TRANSPARENT);
这种方法的好处在于,通过交换有色半透明状态栏的透明状态栏,也可以在API 19上使用相同的布局和设计。
<item name="android:windowTranslucentStatus">true</item>
方法#2:
如果您只需要在状态栏下绘制背景图像,而不是在其背后放置视图,只需将活动主题的背景设置为所需图像并设置状态栏透明度即可,如方法#所示1。这是几个月前用于创建Android Police article截图的方法。
方法#3:
如果你必须忽略某些布局的标准系统插入,同时保持它们在其他布局中工作,唯一可行的方法是使用经常链接的ScrimInsetsFrameLayout
类。当然,在该类中完成的一些事情对于所有场景都不是必需的。例如,如果您不打算使用合成状态栏覆盖,只需在init()
方法中注释掉所有内容,并且不要在attrs.xml文件中添加任何内容。我已经看到这种方法有效,但我认为你会发现它带来了一些其他影响,可能需要做很多工作才能解决。
我也看到你反对包装多个布局。如果将一个布局包装在另一个布局中,其中两个布局都有match_parent
的高度和宽度,性能影响太小而无法担心。无论如何,你可以通过改变从FrameLayout
扩展到你喜欢的任何其他类型的Layout类的类来完全避免这种情况。它会工作得很好。
StatusBarUtil有很好的图书馆@laobie,有助于在StatusBar中轻松绘制图像。
只需添加你的build.gradle
:
compile 'com.jaeger.statusbarutil:library:1.4.0'
然后在Activity集中
StatusBarUtil.setTranslucentForImageView(Activity activity, int statusBarAlpha, View viewNeedOffset)
在布局中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<ImageView
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:src="@drawable/toolbar_bg"/>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/view_need_offset"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<!-- Your layout code -->
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
欲了解更多信息,请从demo下载github page或克隆并播放所有功能。
注意:支持KitKat及以上版本。
希望能帮助别人!
使用Android Studio 1.4,带有样板代码的模板项目会在AppbarLayout和/或工具栏上设置Overlay
主题。它们也被设置为通过fitSystemWindow
attribute = true在状态栏后面呈现。这将导致只有工具栏直接呈现在状态栏下方,其他所有工具都将呈现在工具栏下方。因此,上面提供的解决方案不会单独使用。您必须进行以下更改。
这将使导航抽屉在透明状态栏后面渲染。
您需要做的就是在主题中设置这些属性
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
接受的答案适用于我使用CollapsingToolbarLayout。但值得注意的是,setSytstemUiVisibility()
会覆盖以前对该函数的任何调用。因此,如果您在同一视图的其他位置使用该函数,则需要包含View.SYSTEM_UI_FLAG_LAYOUT_STABLE
和View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
标志,否则它们将被新调用覆盖。
这就是我的情况,一旦我将两个标志添加到另一个地方,我正在打电话给setSystemUiVisibility()
,接受的答案完美无缺。
这是我用来完成这个的主题:
<style name="AppTheme" parent="@android:style/Theme.NoTitleBar">
<!-- Default Background Screen -->
<item name="android:background">@color/default_blue</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
正确的解决方案是将Activity标记下的XML属性更改为以下样式。它只是有效
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
这适用于我的情况
// Create/Set toolbar as actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Check if the version of Android is Lollipop or higher
if (Build.VERSION.SDK_INT >= 21) {
// Set the status bar to dark-semi-transparentish
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// Set paddingTop of toolbar to height of status bar.
// Fixes statusbar covers toolbar issue
toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
}
// A method to find height of the status bar
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
有关使用statusBars的更多信息:youtube.com/watch?v=_mGDMVRO3iE
试试这个主题
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimaryDark">@android:color/transparent</item>
<item name="colorPrimary">@color/md_blue_200</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
请确保您的布局设置
android:fitsSystemWindows="false"
代替
<item name="android:statusBarColor">@android:color/transparent</item>
使用以下内容:
<item name="android:windowTranslucentStatus">true</item>
并确保删除“MainActivity”布局中的顶部填充(默认情况下添加)。
请注意,这不会使状态栏完全透明,并且状态栏上仍会出现“褪色黑色”覆盖。
来自Cody Toombs的解决方案几乎为我做了诀窍。我不确定这是否与Xamarin相关,但我现在有一个可接受的解决方案:
这是我的设置:
我有一个Android项目,我引用了Android.Support v4和v7包。我定义了两种样式:
价值观/ styles.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyStyle" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
值-V21 / styles.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyStyle" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
AndroidManifest的目标是“MyStyle”:
AndroidManifest.xml中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.agn.test.test">
<uses-sdk android:minSdkVersion="10" />
<application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:theme="@style/MyStyle">
</application>
</manifest>
最后是主要活动中的代码:
[Activity (Label = "Test", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.Main);
//Resource.Layout.Main is just a regular layout, no additional flags. Make sure there is something in there like an imageView, so that you can see the overlay.
var uiOptions = (int)Window.DecorView.SystemUiVisibility;
uiOptions ^= (int)SystemUiFlags.LayoutStable;
uiOptions ^= (int)SystemUiFlags.LayoutFullscreen;
Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
Window.AddFlags (WindowManagerFlags.DrawsSystemBarBackgrounds);
}
}
请注意,我设置了DrawsSystemBarBackgrounds标志,这就完全不同了
Window.AddFlags (WindowManagerFlags.DrawsSystemBarBackgrounds);
我花了很多时间把它弄好,实际上花了太多时间。希望这个答案可以帮助任何人尝试实现同样的目标。
@Cody Toombs的回答导致一个问题,导致导航栏后面的布局。所以我发现使用@Kriti给出的this解决方案
这里是相同的Kotlin代码片段:
if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true)
}
if (Build.VERSION.SDK_INT >= 19) {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
if (Build.VERSION.SDK_INT >= 21) {
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false)
getWindow().setStatusBarColor(Color.TRANSPARENT)
}
private fun setWindowFlag(activity: Activity, bits: Int, on: Boolean) {
val win: Window = activity.getWindow()
val winParams: WindowManager.LayoutParams = win.getAttributes()
if (on) {
winParams.flags = winParams.flags or bits
} else {
winParams.flags = winParams.flags and bits.inv()
}
win.setAttributes(winParams)
}
你还需要添加
android:fitsSystemWindows="false"
您的布局的根视图。
我有同样的问题,所以我创建了ImageView,其背后是状态栏API 19+
在状态栏gist.github.com后面设置自定义图像
public static void setTransparent(Activity activity, int imageRes) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
}
// set flags
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
} else {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
// get root content of system window
//ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
// rootView.setFitsSystemWindows(true);
// rootView.setClipToPadding(true);
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
if (contentView.getChildCount() > 1) {
contentView.removeViewAt(1);
}
// get status bar height
int res = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
int height = 0;
if (res != 0)
height = activity.getResources().getDimensionPixelSize(res);
// create new imageview and set resource id
ImageView image = new ImageView(activity);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
image.setLayoutParams(params);
image.setImageResource(imageRes);
image.setScaleType(ScaleType.MATRIX);
// add image view to content view
contentView.addView(image);
// rootView.setFitsSystemWindows(true);
}
您可以使用ScrimInsetsFrameLayout
android:fitsSystemWindows =“true”应该在scrim布局上设置!
类似于发布的一些解决方案,但在我的情况下,我将状态栏透明并修复了操作栏的位置,带有一些负边距
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(Color.TRANSPARENT);
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) toolbar.getLayoutParams();
lp.setMargins(0, -getStatusBarHeight(), 0, 0);
}
我在工具栏和根视图中使用过
android:fitsSystemWindows="true"