屏幕配合是我们在开发中经常遇到的问题之一
画面尺寸和密度因机型而异
目前市场上有许多适合屏幕的解决方案
今天介绍一个我自己认为好的东西
自动化
这是Android端可以根据设备的画面尺寸自动调整宽度和高度的工具
其特征是非常容易使用。
作者写的介绍网站如下。
33559 www.Jian Shu.com/p/4a a23d 69 d 481
代码位置:
3359 github.com/jessyancoding/Android autosize
这里介绍基本的使用方法
首先是依赖
私人依赖
implementation ' me.jessy an : autosize :1.1.2 '
共享依存关系
API ' me.jessy an : autosize :1.1.2 '
第二步是在你的配置列表中定位你屏幕宽度的高度
注意用dp配置
例如,下面是宽度为1280*900的高度值
要获取屏幕宽度的高度,请先获取px,然后根据密度进行转换
以前写过这:
(原始)共享自己写的几个工具类(十一)配置文件的操作工具类
到此为止,其实自动调整已经完成了
其实有几个高级的使用方法
这个方案默认根据幅度来适应
也可以根据高度进行变换
放置在APP的onCreate方法中
AutoSizeConfig.getInstance ().setbaseonwidth ) ) false;
当然,也可以定制各个活动和片段
首先实现CustomAdapt这个类
如果你不想采用这个方案
实现CancelAdap这一类
然后有几种实现这些的方法
boolean isBaseOnWidth () :是否适合宽度? false适合高度吗
浮动高度索引) ) :使用前面的方法确定是否适合宽度或高度,然后返回到要重新设置的宽度或高度。
如果返回0,则宽度为使用配置清单继续配置的高度
注:如果也要在Fragment中自定义
必须在APP的onCreate方法中这样配置
AutoSizeConfig.getInstance ().setcustomfragment ) ) true;
也可以用更自由的方式定制适配器
将:放置在APP的onCreate方法中
AutoSizeConfig.getInstance ().setautoadaptstrategy (newautoadaptstrategy ) );
实现原理:
在setContentView之前修改DisplayMetrics类的内容。
核心代码是以下:
autosize.autoconvertdensity (activity,sizeInDp,isBaseOnWidth ) )。
autosize.set density (活动、拒绝、拒绝dpi、scaledDensity、xdpi ) )。
autosize.sen density (显示度量,density,densityDpi,scaledDensity,xdpi ) )
如果遇到某些界面中添加autosize、某些界面中取消autosize、在APP应用程序之间切换纵横画面等突然变大或变小的问题,请参考此过程
1、在适当的生命周期或方法内调用setScreenWidthHeight和autoConvertDensity
2、不使用getResources ().getConfiguration ).orientation判断横向屏幕,改为使用DisplayMetrics的纵横比
具体来看下面的例子吧
publicclassbaseautosizeactivityextendsactivityimplementscustomadapt {
@Override
公共布尔型isbaseonwidth (
返回真;
}
@Override
公共浮动获取索引
int sizeInDp;
if (autosizeconfig.getinstance (.get screen width ) autosize config.getinstance ).getScreenHeight ) ) }
size indp=autosize config.getinstance ().getDesignWidthInDp );
} else {
size indp=autosize config.getinstance ().getDesignHeightInDp );
}
返回大小索引;
}
@Override
publicvoidonconfigurationchanged (配置网络配置) {
super.onconfigurationchanged (new config );
setScreenWidthHeight (;
autosize.autoconvertdensityofcustomadapt (this,this );
}
@Override
保护性void on resume
super.onResume (;
setScreenWidthHeight (;
autosize.autoconvertdensityofcustomadapt (this,this );
}
@Override
保护性void on restart
super.onRestart (;
setScreenWidthHeight (;
autosize.autoconvertdensityofcustomadapt (this,this );
}
@Override
公共资源获取资源(
setScreenWidthHeight (;
try {
autosize compat.autoconvertdensityofcustomadapt (super.get resources ),this );
}catch(exceptione ) {
e .打印堆栈跟踪(;
}
return super.getResources (;
}
公共void setscreenwidthheight (
int [ ] screen size=screen utils.get screen size (getapplication context (;
int width=屏幕大小[0];
int height=屏幕大小[1];
AutoSizeConfig.getInstance ().setscreenwidth ) ) width;
AutoSizeConfig.getInstance ().setscreenheight ) ) height;
}
}