您的位置:首页 > 移动开发 > Android开发

android屏幕适配方案

2016-01-15 16:28 447 查看
以前看了android的屏幕适配方案,有很多种,其中自己用到的一种是:先找一款主流的分辨率的android机,如:1080*1920的分辨率做基准,然后在这个基准上,调整好一切布局,图片。适配其他手机分辨率的手机,用百分比来调节。例如:在480*800的主流手机上,写了一个height=520,那么在480*800的手机上,这个控件的高度则为480height = 520 * 屏幕高度/1920. 这样的方案做屏幕适配。 项目遇到一个问题。在剩余空间里,在1080*1920的手机上适配很好,但是在480*800的低分辨率手机上则显示不完整。查看了原因:因为我的图片资源是240*240的。布局的ImageView的宽高是自适应的。所以会导致图片太大,而其余的字体,图片就不能完全显示。所以解决方案有2种:一是做低分辨率的资源图。二是外层嵌套scrollView控件,让其滑动显示剩余不能完全的部分。 动态布局首先要得到xml控件的宽高:前几天,在自定义控件的时候碰到个问题,就是在如何获取自定义控件的高宽。在自定义控件类的构造函数中,本来以为可以轻松获取,但事实不是这样。我测试了下面代码:先是布局代码:android:id=@+id/myViewandroid:layout_width=match_parentandroid:layout_height=150pxandroid:background=#ff0000 />再是MyView的构造函数的代码:public MyView(Context context, AttributeSet attrs) {super(context, attrs);a=在MyView构造函数中 : MeasuredWidth:+this.getMeasuredWidth()+;+MeasuredHeight:+this.getMeasuredHeight()+;+Width:+this.getWidth()+;+Height:+this.getHeight()+ ;String h=,w=;for(int i =0 ;i < attrs.getAttributeCount();i++){if(layout_height.equals(attrs.getAttributeName(i))){h=attrs.getAttributeValue(i);}else if(layout_width.equals(attrs.getAttributeName(i))){w=attrs.getAttributeValue(i);}}b=在构造函数attrs中 : width:+w+;+height:+h+ ;}编译得到a=在MyView构造函数中 : MeasuredWidth:0;MeasuredHeight:0;Width:0;Height:0.b=在构造函数attrs中 : width:-1;height:150.0px结果显示当width为match_parent等数值时,只显示-1等,不能满足我的需求。然后我试着在相应Activity的onCreate中获取高宽,获得的全部是0.但我在onCreate中的加了个点击控件获取高宽事件,能正确获取高宽。我在网上查了下资料,因为在onCreate中控件还未被度量,所以获取肯定为0.网上有获取三个方法,方法如下:方法一,在onCreate中添加如下代码:int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);myView.measure(w, h);int height = myView.getMeasuredHeight();int width = myView.getMeasuredWidth();tvValues.append(方法一: height:+height + ,width: + width+ );方法二可以实现,代码如下:ViewTreeObserver vto2 = myView.getViewTreeObserver();vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {myView.getViewTreeObserver().removeGlobalOnLayoutListener(this);tvValues.append( 方法二: height:+myView.getHeight() + ,width: + myView.getWidth()+ );}});但我发现removeGlobalOnLayoutListener在API 级别 16 开始已经废弃,如果去掉,系统会读取多次。再来看看方法三,代码如下:ViewTreeObserver vto = myView.getViewTreeObserver();vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {public boolean onPreDraw() {myView.getViewTreeObserver().removeOnPreDrawListener(this);int height = myView.getMeasuredHeight();int width = myView.getMeasuredWidth();tvValues.append(方法三: height:+height + ,width: + width + .. );return true;}});我在网上资料的基础上添加了myView.getViewTreeObserver().removeOnPreDrawListener(this);这一条,这个可以保证系统运行一次。_____________________________________________________________________________________________________________ 出现的问题:(1)用上面的2个回调监听函数,监听函数会一直调用,关闭不了,而且屏幕跳转会出现闪屏的现象。所以放弃了 ?
(2) 选择使用第一种方法,如果xml里面是用的wrap_content宽高的,可以用getMeasureHeight()方法获取到控件的值,但是动态设置的值只有自己记住这个值大小  ?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: