1、布局統(tǒng)一用LinearLayout通過比重控制布局大虚莼ァ特幔;
2、適配器對(duì)item布局進(jìn)行手動(dòng)設(shè)置大姓⒆颉蚯斯;
3、對(duì)話框手動(dòng)設(shè)置外框大卸稀拍嵌;
4、文字一律需要手動(dòng)設(shè)置大醒摺横辆;
5、19以下統(tǒng)一全屏茄猫,19或以上統(tǒng)一使用沉浸式
/**
* 寬度適配
* @param width 設(shè)計(jì)圖寬度狈蚤,如1280
* @return 寬度縮放比例
*/
public static float getWidthSizeScale(Activity activity, float width) {
DisplayMetrics metric = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
float screenWidth = metric.widthPixels; // 屏幕寬度(像素)
if (metric.heightPixels > screenWidth) {
screenWidth = getHasVirtualKey(activity);
}
return screenWidth / width;
}
/**
* 高度適配
* @param height 設(shè)計(jì)圖高度:如800
* @return 高度縮放比例
*/
public static float getHeightSizeScale(Activity activity, float height) {
DisplayMetrics metric = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
float screenWidth = metric.widthPixels; // 屏幕寬度(像素)
float screenHeight = getHasVirtualKey(activity);
if (metric.heightPixels > screenWidth) {
screenHeight = metric.widthPixels;
}
return screenHeight / height;
}
/**
* 通過反射,獲取包含虛擬鍵的整體屏幕高度
*/
private static int getHasVirtualKey(Activity activity) {
int dpi = 0;
Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
@SuppressWarnings("rawtypes")
Class c;
try {
c = Class.forName("android.view.Display");
@SuppressWarnings("unchecked")
Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, dm);
dpi = dm.heightPixels;
} catch (Exception e) {
e.printStackTrace();
}
return dpi;
}
/**
* 文字適配
*/
public static float getTextScale(Activity activity) {
DisplayMetrics metric = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
float widthPixels = metric.widthPixels;
float densityDpi = metric.densityDpi;
if (metric.heightPixels > widthPixels) {
widthPixels = metric.heightPixels;
}
return widthPixels / densityDpi / 5;
}
沉浸式設(shè)置
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 沉浸式退出監(jiān)聽
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(
new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
initWindow();
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus){
initWindow();
}
}
private void initWindow() {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
}
}