当前位置:首页 » 安卓系统 » android沉浸式

android沉浸式

发布时间: 2022-04-11 16:14:50

Ⅰ 如何实现android沉浸式状态栏

沉浸式通知栏Android4.4以上才支持的新特性。4.3不支持。 具体实现方式如下: 1.新建个公共style,设置android:fitsSystemWindows=true <!-- 设置应用布局时是否考虑系统窗口布局;true --> <style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar"> <item name="android:fitsSystemWindows">true</item> </style> 2. 修改AndroidManifest.xml,让所有的activity样式默认设置为AppBaseTheme(*不同项目要灵活处理,咱这个项目的activity样式都是统一的所以这样设置没问题,但是实际情况下不同的activity可能调用的样式不一样,需要自行按自己的项目来设置) <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppBaseTheme" android:name="****"> 3.新增沉浸式通知栏实现类,实现原理很简单。 1)判断当前系统版本是不是4.4以上,判断代码如下: if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) 2)如果大于4.4则设置状态栏透明化,代码如下: window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 3)获取activity的根rootView(DecorView),然后创建一个新的view stateBarView并把它添加到rootView(这里手动给它设置个ID,下次进来时先判断rootView是否已创建stateBarView,如果已创建则直接获取该View这样可以防止重复创建,导致内存泄露) 以下是具体代码实现: import android.annotation.SuppressLint;import android.app.Activity;import android.content.res.Resources;import android.graphics.drawable.Drawable;import android.os.Build;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.FrameLayout.LayoutParams;/** * 沉浸式通知栏公共类 * @author hurrican * */@SuppressLint({ "InlinedApi", "ResourceAsColor" })public class ImmersedNotificationBar { private Activity activity ; //设置沉浸式通知栏的ID(防止重复创建) private final static int IMMERSED_NOTIFICATION_BAR_ID = 12345678 ; private final static String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height" ; public ImmersedNotificationBar(Activity activity){ this.activity = activity ; } //获取状态栏高度 private int getStatusBarHeight(Resources res){ int statusBarHeight = 0; int resourceId = res.getIdentifier(STATUS_BAR_HEIGHT_RES_NAME, "dimen", "android"); if (resourceId > 0) { statusBarHeight = res.getDimensionPixelSize(resourceId); } return statusBarHeight ; } //添加顶部状态栏 private View addStateBar(Activity activity,ViewGroup rootView,int statusBarHeight){ //创建新的View,并添加到rootView顶部) View statusBarView ; if(null!=rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID)){ statusBarView = rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID); }else{ statusBarView = new View(activity); rootView.addView(statusBarView); } statusBarView.setId(IMMERSED_NOTIFICATION_BAR_ID) ; LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,statusBarHeight); params.gravity = Gravity.TOP; statusBarView.setLayoutParams(params); statusBarView.setVisibility(View.VISIBLE); return statusBarView ; } /** * 设置状态栏颜色 * @param ColorId */ public void setStateBarColor(int ColorId){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的顶级布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化状态栏 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //获取状态栏目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundColor(ColorId) ; } } /** * 设置状态栏颜色 * @param ColorId */ public void setStateBarDrawable(Drawable drawable){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的顶级布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化状态栏 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //获取状态栏目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundDrawable(drawable) ; } }}

Ⅱ android 怎么做带标题栏的沉浸式状态栏

您可以尝试着将AppTheme设置成没有标题栏的样式,在res->values->styles中进行修改:,希望能帮到您,谢谢。

Ⅲ 为什么主流的安卓应用都不愿去支持沉浸式状态栏

国内的Android App,特别是不少大公司的作品,很多是单纯出于全平台支持的战略需求。Android App只是乖乖的跟在iOS的屁股后面走。连界面都直接生搬硬套的山寨过来,又何况这些细节的API呢?对一群用着iOS并且歧视着Android的产品经理/设计师/程序员,采用Android的规范对他们有哪怕一毛钱的好处? 当然,这个问题在全世界范围内都大规模的存在,之所以在国产app里特别凸显,原因有二: 其一,是国外app,能走到大众视线中的往往已是精品,渣滓多半已被淘替;其二,对抄袭成风的国内app开发行业要他们设计一套UI都是勉为其难了,何况再为和iOS相差无几的Android再设计一套? 最后,不得不说的是除了开发者的懒惰和不负责,Android系统本身独立设计风格的缺失也负有非常严重且无可推卸的责任,虽然4.0以来有了明显改观,但总体来说还是任重道远。 作者:1ittlecup

Ⅳ 我Android7.1.1能沉浸状态栏吗要怎么沉浸

方法一:系统的方式沉浸式状态栏实现
1、//当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
2、布局加入:
android:fitsSystemWindows="true"
android:clipToPadding="true"

方法二:实现思路,添加隐藏布局,然后我们动态的计算状态栏的高度,然后把这个高度设置成这个隐藏的布局的高度,便可以实现
/**
* 通过反射的方式获取状态栏高度
*
* @return
*/
private int getStatusBarHeight() {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
return getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}

方法三、用的github上的第三方库
1.库地址:github.com/jgilfelt/SystemBarTint
2.添加依赖库:
compile ‘com.readystatesoftware.systembartint:systembartint:1.0.3’
3、 android:fitsSystemWindows="true"
android:clipToPadding="true
4、 SystemBarTintManager tintManager = new SystemBarTintManager(this);
// 激活状态栏
tintManager.setStatusBarTintEnabled(true);
// enable navigation bar tint 激活导航栏
tintManager.setNavigationBarTintEnabled(true);
//设置系统栏设置颜色
//tintManager.setTintColor(R.color.red);
//给状态栏设置颜色
tintManager.setStatusBarTintResource(R.color.mask_tags_1);
//Apply the specified drawable or color resource to the system navigation bar.
//给导航栏设置资源
tintManager.setNavigationBarTintResource(R.color.mask_tags_1);

Ⅳ 如何实现Android沉浸式状态栏

styles.xml设置如下:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:actionModeBackground">@drawable/context_menu</item>
</style>

<style name="TranslucentTheme" parent="AppTheme.NoActionBar">
</style>

V21的styles.xml设置如下:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:">true</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:statusBarColor">@color/colorPrimary</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:actionModeBackground">@drawable/context_menu</item>
</style>

<style name="TranslucentTheme" parent="AppTheme.NoActionBar">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>

再在要显示的toolbar里加上属性:
android:fitsSystemWindows="true"

主题的属性设置为:

<style name="TranslucentTheme" parent="AppTheme.NoActionBar">

Ⅵ android中怎么实现沉浸式状态栏

styles.xml设置如下:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:actionModeBackground">@drawable/context_menu</item>
</style>

<style name="TranslucentTheme" parent="AppTheme.NoActionBar">
</style>

V21的styles.xml设置如下:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:">true</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:statusBarColor">@color/colorPrimary</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:actionModeBackground">@drawable/context_menu</item>
</style>

<style name="TranslucentTheme" parent="AppTheme.NoActionBar">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>

再在要显示的toolbar里加上属性:
android:fitsSystemWindows="true"

主题的属性设置为:

<style name="TranslucentTheme" parent="AppTheme.NoActionBar">

Ⅶ Android沉浸式状态栏 如何改变状态图标和文字的颜色

在Android4.4设备上支持沉浸式状态栏,只需要添加values-v19/styles.xml 下添加
?

1
2

<code class="language-xml hljs "><style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar" type="text/css"><item name="android:windowTranslucentNavigation">false</item>
<item name="android:windowTranslucentStatus">true</item></style></code>

然后在可以扩展的控件添加属性android:fitsSystemWindows="true"
就阔以了。
但在MIUI V6下如果扩展的颜色比较浅,会导致状态栏的文字无法看清。在其他ROM上会有渐变的灰色区域。
MIUI提供了新的解决方案,在MIUI V6上状态栏支持灰黑色和白色两种字体颜色,开发者可以直接设置当前界面状态栏的文字颜色。
具体代码:
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

<code class="language-java hljs ">/**
* 只支持MIUI V6
* @param context
* @param type 0--只需要状态栏透明 1-状态栏透明且黑色字体 2-清除黑色字体
*/
public static void setStatusBarTextColor(Activity context,int type){
if (!isMiUIV6()){
DebugLog.d("isMiUIV6:"+false);
return;
}
DebugLog.d("isMiUIV6:"+true);
Window window = context.getWindow();
Class clazz = window.getClass();
try {
int tranceFlag = 0;
int darkModeFlag = 0;
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_TRANSPARENT");
tranceFlag = field.getInt(layoutParams);
field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
if (type == 0){
extraFlagField.invoke(window, tranceFlag, tranceFlag);//只需要状态栏透明
}else if(type == 1){
extraFlagField.invoke(window, tranceFlag | darkModeFlag, tranceFlag | darkModeFlag);//状态栏透明且黑色字体
}else {
extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
}
}catch (Exception e){

}
}

private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
private static boolean isMiUIV6() {
try {
final BuildProperties prop = BuildProperties.newInstance();
String name = prop.getProperty(KEY_MIUI_VERSION_NAME, "");
if ("V6".equals(name)){
return true;
}else {
return false;
}
// return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
// || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
// || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
} catch (final IOException e) {
return false;
}
}</code>

Ⅷ 安卓开发中怎样设置沉浸式状态栏

这个特性是andorid4.4支持的,最少要api19才可以使用。下面介绍一下使用的方法,非常得简单:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

}

}

//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

只要加入这两行代码,就可以实现沉浸式通知栏了。

给大家看看这个界面的布局:

<linearlayout android:background="#ffffff" android:cliptopadding="true" android:fitssystemwindows="true" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">

<textview android:background="#009959" android:layout_height="100dp" android:layout_width="match_parent"><button android:background="#ff669d/" android:layout_height="50dp" android:layout_width="100dp"></button></textview></linearlayout>

大家看红色的那部分,加入那两行以后,界面仍然会是沉浸式的,但状态栏那部分,就不会再重叠了,像加了padding一样,如下图:

大家看图,绿色的textView和红色的一个button都被下移了,状态栏是白色的,是背景linearLayout的颜色。很明显,这也不是我们想要的,我们希望状态栏和我们放在顶部的控件是同一个颜色,同时,控件内容也不和状态栏重复,其实,只要把那两行代码放到我们顶部的控件就可以了。代码如下:

<linearlayout android:background="#ffffff" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">

<textview android:background="#009959" android:cliptopadding="true" android:fitssystemwindows="true" android:layout_height="100dp" android:layout_width="match_parent" android:text="你好,请问你有男朋友吗/"><button android:background="#ff669d/" android:layout_height="50dp" android:layout_width="100dp"></button></textview></linearlayout>

就是那两行红色的代码,放在绿色的textView上,这样,就会是下面的效果:

这就是我们想要的了。

Ⅸ android 沉浸式状态栏和透明状态栏的区别

注意!两种方法的区别:
第一种:为顶部栏跟随当前activity的布局文件的背景的颜色,使用方便,不过也有点问题就是,如果有底部虚拟导航键的话,导航键的背景跟顶部的颜色一样,比如:

第二种:是通过设置顶部栏的颜色来显示的,可以解决第一种的不足,比如:

第一种使用方法:

第一、首先在values、values-v19、values-v21文件夹下的styles.xml都设置一个 Translucent System Bar 风格的Theme,如下图:

values/style.xml:
<style name="TranslucentTheme" parent="AppTheme">
<!--在Android 4.4之前的版本上运行,直接跟随系统主题-->
</style>123

values-v19/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>1234

values-v21/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>123456

第二、在清单文件中配置需要沉浸式状态栏的activity加入theme
<activity android:name=".ImageActivity" android:theme="@style/TranslucentTheme" />
<activity android:name=".ColorActivity" android:theme="@style/TranslucentTheme" />12

第三、在Activity的布局文件中的跟布局加入“android:fitsSystemWindows=”true””,但是,这里需要区分一下,就是背景是图片还是纯色:

1.当背景为图片时,布局可以这么写:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/imgs_bj"
android:fitsSystemWindows="true">

</RelativeLayout>12345678

效果:

2.当背景为纯色,我们需要对布局划分一下,标题布局与内容布局,先把根布局背景设置成标题布局的背景色,然后标题背景色可以不用设置直接使用根布局的背景色,最后内容布局背景色设置为白色
<?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="match_parent"
android:background="@color/colorPrimary" //根布局背景设置成“标题布局”想要的颜色
android:fitsSystemWindows="true"
android:orientation="vertical">

<!--标题布局-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@color/color_31c27c">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="这是标题"
android:textColor="@android:color/white"
android:textSize="20sp" />

</RelativeLayout>

<!--内容布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" //内容区域背景设置成白色
android:gravity="center"
android:orientation="vertical">

<Button
android:layout_marginTop="120dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="显示信息"
android:onClick="showMsg"
/>
</LinearLayout>

</LinearLayout>

热点内容
块直存储 发布:2025-04-09 01:39:01 浏览:210
山西电信服务器地址云主机 发布:2025-04-09 01:31:47 浏览:981
win7linux传文件 发布:2025-04-09 01:30:21 浏览:501
安卓完美解锁机怎么升级系统 发布:2025-04-09 01:28:50 浏览:89
滚轮JAVA 发布:2025-04-09 01:27:32 浏览:127
如何将电脑投屏到安卓平板 发布:2025-04-09 01:25:56 浏览:354
sql查询的类型 发布:2025-04-09 01:22:14 浏览:58
linuxjdkbin 发布:2025-04-09 01:17:36 浏览:85
gl8商用选什么配置 发布:2025-04-09 01:09:23 浏览:83
编程伴我成长 发布:2025-04-09 01:08:10 浏览:163