【Android Training - 01】适配不同的屏幕[Lesson 1-支持不同的屏幕大小]
Dependencies and prerequisites[前提条件]
"wrap_content", the width or height of the view is set to the minimum size necessary to fit the content within that view, while?"match_parent"?(also known as"fill_parent"?before API level 8) makes the component expand to match the size of its parent view.
Use RelativeLayout[使用相对布局][如果我们需要一个相对的位置而不是仅仅是直线型的布局,那么我们可以使用RelativeLayout]
上面图片是:Screenshot on a QVGA screen (small screen).
上面图片是:Screenshot on a WSVGA screen (large screen).
Use Size Qualifiers[使用大小标识符][虽然我们可以用上面的方法定义布局文件,让其在不同的情况下进行拉伸等动作,可是在某些比较大的屏幕,比如平板与TV上面还是不太适合,我们最好可以在这种情况下使用两套不同的布局文件来适配大小,我们可以使用大小标示符来标记不同的布局,让机器在运行程序的时候根据自身的大小来选择显示哪个布局][通常在?Tablets and TVs的AP上使用"two?pane"的布局,这样]
?
?
res/layout/main.xml, single-pane (default) layout:?single pane, with action bar- 7" tablet, landscape:?dual?pane, wide, with action bar
- 10" tablet, portrait:?dual pane, narrow, with action bar
- 10" tablet, landscape:?dual pane, wide, with action bar我们可以使用上面说的别名方法,对上面的需求抽取出一些公共的布局元素,定义下面几个布局:
res/layout/onepane.xml:
res/layout/onepane_with_bar.xml:res/layout/twopanes.xml:res/layout/twopanes_narrow.xml:?
这样以来,我们需要在setContentView的时候选择main_layout就可以了,系统会选择到相应的Value里面取出对应的布局文件进行显示
?
?
res/values/layouts.xml:
可以看到这样的图片不会对那个下拉图标进行拉伸,这样就达到了我们需要的效果,不然下拉图标也被拉伸会显得很不合适。