状态栏的设置,最后采取的是隐藏状态栏,透明状态栏需要在页面中调整布局
This commit is contained in:
@ -68,4 +68,5 @@ dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
implementation 'androidx.preference:preference:1.1.1'
|
||||
//implementation fileTree(include: ['*.aar'], dir: 'libs')
|
||||
api 'com.readystatesoftware.systembartint:systembartint:1.0.3'
|
||||
}
|
||||
@ -16,6 +16,7 @@
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true"
|
||||
android:fitsSystemWindows="true"
|
||||
android:name=".application.InitApplication"
|
||||
android:theme="@style/Theme.PdaWeb"
|
||||
android:usesCleartextTraffic="true">
|
||||
|
||||
@ -31,6 +31,7 @@ import chaoran.business.engine.SettingEngine;
|
||||
import chaoran.business.engine.impl.TekVoiceEngine;
|
||||
import chaoran.business.engine.VoiceEngine;
|
||||
import chaoran.business.utils.LocalAddressUtil;
|
||||
import chaoran.business.utils.StatusBarUtil;
|
||||
|
||||
/**
|
||||
* 流程:联网认证设备型号,验证通过,查找设备品牌进行调用驱动操作
|
||||
@ -90,6 +91,10 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
|
||||
webView.setWebViewClient(disposeView());
|
||||
WebSettings settings = webView.getSettings();
|
||||
settings.setJavaScriptEnabled(true);
|
||||
|
||||
// StatusBarUtil.transparencyBar( this); // 设置全部透明,需要在页面设置一个参数进行布局的样式跳转,不同的手机端,状态栏高度不一样(apk设置状态栏高度无效,这个是安卓9的一个bug),所以在此采取隐藏状态栏
|
||||
StatusBarUtil.hideStatusBar( this); // 设置全部透明
|
||||
|
||||
//设置接口进行windows暴露
|
||||
settings.setDomStorageEnabled(true);
|
||||
//语音引擎
|
||||
@ -98,7 +103,7 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
|
||||
webView.addJavascriptInterface(settingEngine, "NetworkSettingEngine");
|
||||
//重新加载页面
|
||||
webView.addJavascriptInterface(this, "View");
|
||||
webView.addJavascriptInterface(new LocalAddressUtil(), "Localpda");
|
||||
webView.addJavascriptInterface(new LocalAddressUtil(this), "Localpda");
|
||||
webView.loadUrl(url());
|
||||
}
|
||||
|
||||
@ -281,4 +286,5 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@ -2,6 +2,7 @@ package chaoran.business.utils;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.bluetooth.le.ScanSettings;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
import android.webkit.JavascriptInterface;
|
||||
@ -13,6 +14,16 @@ import java.net.SocketException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class LocalAddressUtil {
|
||||
|
||||
private Context context;
|
||||
|
||||
private int[] heights;
|
||||
|
||||
public LocalAddressUtil(Context context) {
|
||||
this.context = context;
|
||||
this.heights = StatusBarUtil.getStatusBarHeight(context);
|
||||
}
|
||||
|
||||
@SuppressLint("JavascriptInterface")
|
||||
@JavascriptInterface
|
||||
public String getIpAddress() {
|
||||
@ -77,6 +88,15 @@ public class LocalAddressUtil {
|
||||
return info;
|
||||
}
|
||||
|
||||
public String getHeight(int type) {
|
||||
String info = "";
|
||||
switch (type) {
|
||||
case 1 : info = String.valueOf(this.heights[0]); break;
|
||||
case 2 : info = String.valueOf(this.heights[1]); break;
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
public void test(){
|
||||
Log.e("test", "MANUFACTURER=" + Build.MANUFACTURER);
|
||||
Log.e("test", "BRAND=" + Build.BRAND);
|
||||
|
||||
218
app/src/main/java/chaoran/business/utils/StatusBarUtil.java
Normal file
218
app/src/main/java/chaoran/business/utils/StatusBarUtil.java
Normal file
@ -0,0 +1,218 @@
|
||||
package chaoran.business.utils;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.os.Build;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import com.readystatesoftware.systembartint.SystemBarTintManager;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* 状态栏工具类
|
||||
*/
|
||||
public class StatusBarUtil {
|
||||
/**
|
||||
* 修改状态栏为全透明
|
||||
*
|
||||
* @param activity
|
||||
*/
|
||||
@TargetApi(19)
|
||||
public static void transparencyBar(Activity activity) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
Window window = activity.getWindow();
|
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
||||
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
||||
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
|
||||
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
|
||||
window.setStatusBarColor(Color.TRANSPARENT);
|
||||
|
||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
Window window = activity.getWindow();
|
||||
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
|
||||
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取屏幕的高度和状态栏高度
|
||||
* @param context
|
||||
* @return 第一个是屏幕,第二个是状态栏
|
||||
*/
|
||||
public static int[] getStatusBarHeight(Context context) {
|
||||
int[] result = new int[]{0,0};
|
||||
int resourceld = context.getResources().getIdentifier(
|
||||
"status_bar_height", "dimen", "android"
|
||||
);
|
||||
if (resourceld > 0) {
|
||||
result[0] = context.getResources().getDisplayMetrics().heightPixels;
|
||||
result[1] = context.getResources().getDimensionPixelSize(resourceld);
|
||||
System.out.println( " 屏幕高度:: 状态栏高度:: " + result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//隐藏状态栏
|
||||
public static void hideStatusBar(Activity activity){
|
||||
if (Build.VERSION.SDK_INT >= 21) {
|
||||
activity.getWindow()
|
||||
.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改状态栏颜色,支持4.4以上版本
|
||||
*
|
||||
* @param activity
|
||||
* @param colorId
|
||||
*/
|
||||
public static void setStatusBarColor(Activity activity, int colorId) {
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
Window window = activity.getWindow();
|
||||
window.setStatusBarColor(activity.getResources().getColor(colorId));
|
||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
//使用SystemBarTint库使4.4版本状态栏变色,需要先将状态栏设置为透明
|
||||
transparencyBar(activity);
|
||||
SystemBarTintManager tintManager = new SystemBarTintManager(activity);
|
||||
tintManager.setStatusBarTintEnabled(true);
|
||||
tintManager.setStatusBarTintResource(colorId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 状态栏亮色模式,设置状态栏黑色文字、图标,
|
||||
* 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
|
||||
*
|
||||
* @param activity
|
||||
* @return 1:MIUUI 2:Flyme 3:android6.0
|
||||
*/
|
||||
public static int StatusBarLightMode(Activity activity) {
|
||||
int result = 0;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
if (MIUISetStatusBarLightMode(activity, true)) {
|
||||
result = 1;
|
||||
} else if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) {
|
||||
result = 2;
|
||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
||||
result = 3;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 已知系统类型时,设置状态栏黑色文字、图标。
|
||||
* 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
|
||||
*
|
||||
* @param activity
|
||||
* @param type 1:MIUUI 2:Flyme 3:android6.0
|
||||
*/
|
||||
public static void StatusBarLightMode(Activity activity, int type) {
|
||||
if (type == 1) {
|
||||
MIUISetStatusBarLightMode(activity, true);
|
||||
} else if (type == 2) {
|
||||
FlymeSetStatusBarLightMode(activity.getWindow(), true);
|
||||
} else if (type == 3) {
|
||||
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 状态栏暗色模式,清除MIUI、flyme或6.0以上版本状态栏黑色文字、图标
|
||||
*/
|
||||
public static void StatusBarDarkMode(Activity activity, int type) {
|
||||
if (type == 1) {
|
||||
MIUISetStatusBarLightMode(activity, false);
|
||||
} else if (type == 2) {
|
||||
FlymeSetStatusBarLightMode(activity.getWindow(), false);
|
||||
} else if (type == 3) {
|
||||
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置状态栏图标为深色和魅族特定的文字风格
|
||||
* 可以用来判断是否为Flyme用户
|
||||
*
|
||||
* @param window 需要设置的窗口
|
||||
* @param dark 是否把状态栏文字及图标颜色设置为深色
|
||||
* @return boolean 成功执行返回true
|
||||
*/
|
||||
public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
|
||||
boolean result = false;
|
||||
if (window != null) {
|
||||
try {
|
||||
WindowManager.LayoutParams lp = window.getAttributes();
|
||||
Field darkFlag = WindowManager.LayoutParams.class
|
||||
.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
|
||||
Field meizuFlags = WindowManager.LayoutParams.class
|
||||
.getDeclaredField("meizuFlags");
|
||||
darkFlag.setAccessible(true);
|
||||
meizuFlags.setAccessible(true);
|
||||
int bit = darkFlag.getInt(null);
|
||||
int value = meizuFlags.getInt(lp);
|
||||
if (dark) {
|
||||
value |= bit;
|
||||
} else {
|
||||
value &= ~bit;
|
||||
}
|
||||
meizuFlags.setInt(lp, value);
|
||||
window.setAttributes(lp);
|
||||
result = true;
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 需要MIUIV6以上
|
||||
*
|
||||
* @param activity
|
||||
* @param dark 是否把状态栏文字及图标颜色设置为深色
|
||||
* @return boolean 成功执行返回true
|
||||
*/
|
||||
public static boolean MIUISetStatusBarLightMode(Activity activity, boolean dark) {
|
||||
boolean result = false;
|
||||
Window window = activity.getWindow();
|
||||
if (window != null) {
|
||||
Class clazz = window.getClass();
|
||||
try {
|
||||
int darkModeFlag = 0;
|
||||
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
|
||||
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
|
||||
darkModeFlag = field.getInt(layoutParams);
|
||||
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
|
||||
if (dark) {
|
||||
extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
|
||||
} else {
|
||||
extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
|
||||
}
|
||||
result = true;
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
//开发版 7.7.13 及以后版本采用了系统API,旧方法无效但不会报错,所以两个方式都要加上
|
||||
if (dark) {
|
||||
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
||||
} else {
|
||||
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user