判断是否存在虚拟键

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
private final static String SHOW_NAV_BAR_RES_NAME = "config_showNavigationBar";
public String getNavBarOverride() {
if (Build.VERSION>SDK_INT >= Build.VERSION_CODES.KITKAT) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
try {
// 相当于SystemProperties.get
Class c = Class.forName("android.os.SystemProperties");
Method m = c.getDeclaredMethod("get",String.class);
m.setAccessible(true);
return (String)m.invoke(null,"qemu.hw.mainkeys");
} catch (Throwable e) {
return null;
}
}
}

@TargetApi(14)
private boolean hasNavBar(Context context) {
Resources res = context.getResources();
// resources.getIdenttifier() 方法可以获取指定报名下的资源文件ID,后两个参数表示资源类型和默认报名
int resourceId = res.getIdentifier(SHOW_NAV_BAR_RES_NAME,"bool","android");
if(resourceId != 0){
if ("1".equals(getNavBarOverride())) {
hasNav = false;
} else if ("0".equals(getNavBarOverride())) {
hasNav = true;
}
return hasNav;
} else {
return !ViewConfiguration.get(context).hasPermanentMenuKey();
}
}

获取虚拟键的高度

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
public int getNavigationBarHeight(Context context) {
Resources res = context.getResources();
int result = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (hasNavBar(context)) {
String key;
if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
key = NAV_BAR_HEIGHT_RES_NAME;
} else {
if (!isNavigationAtBottom())
return 0;
key = NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME;
}
return getInternalDimensionSize(res,key);
}
}
return result;
}

private int getInternalDimensionSize(Resources res, String key) {
int result = 0;
int resourceId = res.getIdentifier(key, "dimen", "android");
if (resourceId > 0) {
result = res.getDimensionPixelSize(resourceId);
}
return result;
}

private boolean isNavigationAtBottom() {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
float mSmallestWidthDp = getSmallestWidthDp(wm);
return (mSmallestWidthDp >= 600 || mInPortrait);
}

private float getSmallestWidthDp(WindowManager wm) {
DisplayMetrics metrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
wm.getDefaultDisplay().getRealMetrics(metrics);
} else {
//this is not correct, but we don't really care pre-kitkat
wm.getDefaultDisplay().getMetrics(metrics);
}
float widthDp = metrics.widthPixels / metrics.density;
float heightDp = metrics.heightPixels / metrics.density;
return Math.min(widthDp, heightDp);
}

  1. PreferenceScreen 设置页面,可嵌套二级设置页面
  2. PreferenceCategory 设置类,可用Title参数设置标题
  3. CheckBoxPreference 可选设置,可用Title设置标题,用summaryOn和summaryOff参数设置控件选中和未选中时的提示,可用defaultValue设置缺省值
  4. ListPreference 下拉框选择设置,可用Title设置标题,用Summary参数设置说明,点击出现下拉框,用dialogTitle设置下拉框的标题,entries和entryValues分别表示显示的值和代码中获取的真正的值
  5. EditTextPreference 输入框设置,点击输入字符串设置,用Title参数设置标题,Summary参数设置说明,dialogTitle参数设置输入框的标题
  6. RingtonePreference 铃声选择设置,点击后可选择系统铃声,用Title参数设置标题,Summary参数设置说明,dialogTitle参数设置铃声选择的标题