Experience of slightly changing the existing project of android to adapt to the tablet
- 2021-12-12 05:48:05
- OfStack
Recently, projects that have been developed for several years need to be adapted to the tablet. At the same time, the tablet needs to be switched between horizontal and vertical screens, and can be used normally on the mobile phone. After investigation, with the minimum changes, necessary modifications are made to the project to complete the corresponding requirements. Now, the general ideas and involved contents are sorted out as follows:
The manifest file releases the restriction of forcing horizontal and vertical screens
When switching between horizontal and vertical screens, the page needs to be redrawn, and the life cycle will go again. In order to prevent repeated life cycle, set it in the manifest file
android:configChanges="keyboardHidden|orientation|screenSize"
Split the screen of the interface
Left and right split screens, base for horizontal and vertical judgment, full screen or a few copies of the screen processing (initial loading)
@Override
protected void onStart() {
super.onStart();
setScreenDirection();
}
private void setScreenDirection() {
boolean screenDirection = ScreenDirectionUtil.getInstance().getScreenDirection(this);
if (screenDirection) {
ScreenDirectionUtil.getInstance().setScreenProportion(this,1,1,2);
}else {
ScreenDirectionUtil.getInstance().setScreenProportion(this,5,3,2);
}
}
Because configChanges is set, the life cycle will no longer go when switching between horizontal and vertical screens. Judge the direction of horizontal and vertical screens in onConfigurationChanged and dynamically configure the screen proportion
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE) {
// Horizontal screen
ScreenDirectionUtil.getInstance().setScreenProportion(this,5,3,2);
}else if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT) {
// Vertical screen
ScreenDirectionUtil.getInstance().setScreenProportion(this,1,1,2);
}
}
Set the penetration click of activity, and the level 1 page on the left side of the tablet can be clicked directly
Set style for activity in manifest file
android:theme="@style/transparent_activity"
<style name="transparent_activity" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
</style>
Penetrable click setting in activity
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
Dynamic setting method of screen proportion
/**
* Set the horizontal scale of the screen
*
* @param context
* @param proportion The screen is divided into several parts
* @param number Number of screen copies occupied
* @param position 1: Left 2 : Right
*/
public void setScreenProportion(Activity context, int proportion,int number,int position) {
// To get screen width, height
Display display = context.getWindowManager().getDefaultDisplay();
Window win = context.getWindow();
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
if (proportion > 1){
lp.width = (int) (display.getWidth() * number / proportion);
}else {
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
}
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
// Set the top display of the dialog box
lp.gravity = Gravity.TOP;
// Set the top display of the dialog box
if (position == 1) {
lp.gravity = Gravity.LEFT;
}else {
lp.gravity = Gravity.RIGHT;
}
win.setAttributes(lp);
}
Method for judging whether it is a flat plate or not
/**
* Judge whether it is a flat plate or not
* @param context
* @return true: Flat plate false : Not a tablet
*/
public boolean isPad(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
Judge whether it is a horizontal screen or a vertical screen
/**
* Get screen orientation
*
* @param context
* @return true: Vertical screen false: Horizontal screen
*/
public boolean getScreenDirection(Context context) {
DisplayMetrics d = context.getResources().getDisplayMetrics();
if (d.heightPixels > d.widthPixels) {
return true;
}
return false;
}
The above is the main related matters needing attention and the methods used
The above is the android existing project slightly changed the details of the adapter tablet, more about the android existing project adapter tablet information please pay attention to other related articles on this site!