参考该文章理论知识加代码
但是呢,该篇博文里个人觉得代码封装的不是很好,于是自己封装了下,使用XStream生成xml。上面那篇文章里没有对横竖屏进行适配,代码里完善了这一点,对横竖屏进行了适配。
在开始码代码前,贴一张图,结合前面那篇文章的理论知识一起看。
然后呢看最终适配的效果,这里以320*480为基准,屏幕上放一个TextView,宽度为x160,高度为y240,效果图如下
然后呢,不要惊讶,你会发现里面的两个pad并没有适配,其实呢,我也母鸡呀,但是我开了一个模拟器,启动了一个pad,其实是适配了。于是就没有然后了,有兴趣的再研究下吧。
先封装Screen类
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 47 48 |
package cn.edu.zafu.model; /** * @author lizhangqu * @description * @date */ public class Screen { private int width; private int height; public Screen(int width, int height) { this.width = width; this.height = height; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public String getDir(boolean portrait) { // landscape是横向,portrait是纵向 if (portrait) { return String.format("values-%dx%d", height, width); } else { return String.format("values-land-%dx%d", height, width); } } @Override public String toString() { return "Screen [width=" + width + ", height=" + height + "]"; } } |
其次是Rescource类,使用注解
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 |
package cn.edu.zafu.model; import java.util.ArrayList; import java.util.List; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamImplicit; /** * @author lizhangqu * @description * @date */ @XStreamAlias("resources") public class Resource { @XStreamImplicit(itemFieldName = "dimen") List<Dimen> dimens = new ArrayList<Dimen>(); public Resource() { } public Resource(List<Dimen> dimens) { چ这一点,对横竖屏进行了适配。 在开始码代码前,贴一张图,结合前面那篇文章的理论知识一起看。 然后呢看最终适配的效果,这里以320*480为基准,屏幕上放一个TextView,宽度为x160,高度为y240,效果图如下 然后呢,不要惊讶,你会发现里面的两个pad并没有适配,其实呢,我也母鸡呀,但是我开了一个模拟器,启动了一个pad,其实是适配了。于是就没有然后了,有兴趣的再研究下吧。 先封装Screen类
其次是Rescource类,使用注解
|