做好Android屏幕适配,我们要遵守以下原则:
1,确保你的布局可以适配屏幕
2,根据屏幕参数不同加载不同的布局
3,确保合适的布局匹配合适的屏幕
4,提供不同参数的图片
基于以上原则,在实际开发中,我们可以采取以下几种做法:
a,适配不同屏幕尺寸
1,通过使用wrao_content或者match_parent设置控件的宽和高,而不是使用固定的值
2,通过使用RelativeLayout结合wrap_content和match_parent来适配布局
3,为不同尺寸的屏幕选择不同的布局文件,例如:res/layout/main.xml,res/layout-large/main.xml这两个文件就是用于适配不同屏幕的
4,通过设置最小宽度来适配布局,例如:sw600dp和sw720dp
5,通过使用化名来适配布局,例如:res/layout/main_twopanes.xml
- res/values-large/layout.xml: <resources>
- <item name="main" type="layout">@layout/main_twopanes</item>
- </resources>
- res/values-sw600dp/layout.xml: <resources>
- <item name="main" type="layout">@layout/main_twopanes</item>
- </resources>
6,适配横屏竖屏同样需要像上面一样在values-xxx中引用对应的布局
7,使用Nine-patch图片
b,适配不同屏幕密度
1,使用dp设置layout,使用sp设置textsize
2,在drawable-xxxx里分别放置不同参数的图片
c,使用代码动态加载布局,例如:使用fragment