//反射获取状态栏ID
private void initStatusBar() {
if (statusBarView == null) {
int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
statusBarView = getWindow().findViewById(identifier);
}
if (statusBarView != null) {
statusBarView.setBackgroundResource(R.drawable.home_toolbar_bg);
}
}
private boolean hasStatusBar() {
return true;
}
1.语言切换
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
if (lang.equals("zh")) {
configuration.setLocale(Locale.CHINESE);
PreferenceUtils.setLang(this, "zh");
} else {
configuration.setLocale(Locale.ENGLISH);
PreferenceUtils.setLang(this, "en");
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
this.recreate();
2.版本versionName大小
public static boolean isNewestApp(String localVersion, String netVersion) {
if (localVersion.equals(netVersion)) {
return true;
}
if (netVersion.equals("")) {
return true;
}
String[] newVersion = netVersion.split("\\.");
String[] oldVersion = localVersion.split("\\.");
int length = oldVersion.length < newVersion.length ? newVersion.length : oldVersion.length;
for (int i = 0; i < length; i++) {
if (Integer.parseInt(newVersion[i]) > Integer.parseInt(oldVersion[i])) {
return false;
} else if (Integer.parseInt(newVersion[i]) < Integer.parseInt(oldVersion[i])) {
return true;
}
// 相等 比较下一组值
}
return false;
}
3.保存文件
public static String decryptedFile(Context context, String name) {
FileInputStream fis = null;
FileOutputStream fos = null;
File file = null;
File files = null;
try {
File dir = new File(AppConfig.getRootPath(context) + "temp/");
if (!dir.exists()) {
dir.mkdirs();
}
if (!TextUtils.isEmpty(name)) {
file = new File(AppConfig.getRootPath(context), name);
files = new File(dir, name);
if (files.exists()) {
files.delete();
files.createNewFile();
} else {
files.createNewFile();
}
}
fis = new FileInputStream(file);
byte[] buf = new byte[2048];
int len = 0;
int total = fis.available();
fos = new FileOutputStream(files);
int i = 0;
while ((len = fis.read(buf)) != -1) {
if (i == 0) {
fos.write(buf, 8, len - 8);
} else {
fos.write(buf, 0, len);
}
i++;
}
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) fis.close();
if (fos != null) fos.close();
return files.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
4.TabLayout
tabIndicatorColor//选中下划线颜色
tabTextColor//字体颜色
tabSelectedTextColor//选中时文字颜色
5.状态栏透明
if (Build.VERSION.SDK_INT >= 21) {
View decorView = activity.getWindow().getDecorView();
//SYSTEM_UI_FLAG_HIDE_NAVIGATION隐藏底部导航栏
//SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 全屏
//SYSTEM_UI_FLAG_LAYOUT_STABLE 让应用主题内容占用系统状态栏的空间
//View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
int options = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(options);//设置系统UI的可见性
//设置状态栏和导航栏颜色为透明
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
}
适配5.0以下
<item name="android:windowBackground">@drawable/tpv_window_background</item>
6.状态栏渐变色
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
@Override
public boolean queueIdle() {
if (hasStatusBar()) {
initStatusBar();
getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
initStatusBar();
}
});
}
return false;
}
});
}
//背景代码xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="0"
android:endColor="@color/home_toolbar_bg_end"
android:startColor="@color/home_toolbar_bg_start" />
</shape>
7.权限
权限组 | 权限 | 权限说明 | |||||
CALENDAR | READ_CALENDAR | 读取日历 | |||||
WRITE_CALENDAR | 修改日历 | ||||||
CAMERA | CAMERA | 相机 | |||||
CONTACTS | READ_CONTACTS | 读取手机联系人 | |||||
WRITE_CONTACTS | 写入联系人 | ||||||
GET_ACCOUNTS | 允许访问Gmail列表 | ||||||
LOCATION | ACCESS_FINE_LOCATION | 允许通过移动网络或WIFI定位 | |||||
ACCESS_COARSE_LOCATION | 允许通过移动网络或WIFI定位 | ||||||
MICROPHONE | RECORD_AUDIO | 录音或录制视频 | |||||
PHONE | READ_PHONE_STATE | 允许访问电话状态 | |||||
CALL_PHONE | 允许程序拨打电话 | ||||||
READ_CALL_LOG | 读取通话记录 | ||||||
WRITE_CALL_LOG | 保存用户的联系人数据 | ||||||
ADD_VOICEMAIL | 允许一个应用程序添加语音邮件系统 | ||||||
USE_SIP | 允许使用SIP视频服务 | ||||||
PROCESS_OUTGOING_CALLS | 允许程序拦截,监视或修改播出电话 | ||||||
SENSORS | BODY_SENSORS | 使用的传感器来测量身体变化(心跳) | |||||
SMS | SEND_SMS | 发送短信 | |||||
READ_SMS | 读取短信 | ||||||
RECEIVE_SMS | 允许程序接收短信 | ||||||
RECEIVE_WAP_PUSH | 接收WAP PUSH信息 | ||||||
RECEIVE_MMS | 接收彩信 | ||||||
STORAGE | READ_EXTERNAL_STORAGE | 读取外部存储空间(有写权限后无需添加) | |||||
WRITE_EXTERNAL_STORAGE | 程序写入外部存储,如SD卡上写文件 |
8.EditText自动弹出软件盘
etVCode.requestFocus();
9.support 升级 androidx Glide 报错
错误: 程序包android.support.annotation不存在
解决方案:
annotationProcessor 'androidx.annotation:annotation:1.0.2'