读书人

android:荧屏自适应

发布时间: 2013-04-20 19:43:01 作者: rapoo

android:屏幕自适应

<?xml version="1.0" encoding="utf-8"?><resources>???<!-- 全屏幕拉伸-->??<style name="layout_full">????? <item name="android:layout_width">fill_parent</item>????? <item name="android:layout_height">fill_parent</item>????</style>???<!-- 固定自身大小-->??<style name="layout_wrap">????? <item name="android:layout_width">wrap_content</item>????? <item name="android:layout_height">wrap_content</item>????</style>?<!-- 横向分布-->??<style name="layout_horizontal" parent="layout_full">????? <item name="android:layout_width">0px</item>????</style> ????<!-- 纵向分布-->??<style name="layout_vertical" parent="layout_full">????? <item name="android:layout_height">0px</item>????</style> ??? ? ? ?</resources>??
可以看到,layout_width和layout_height两个属性被我封装成了4个style? ? 根据实际布局情况,选用当中的一种,不需要自己设置,看过我前一个ActivityGroup的Demo的同学应该非常熟悉了? ? 然后我的Demo的布局如下(weight_layout.xml)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"??? ? ? style="@style/layout_full"??? ? ? android:orientation="vertical">??? ? ? <LinearLayout ??? ? ? ? ? ? ? style="@style/layout_vertical"??? ? ? ? ? ? ? android:layout_weight="1"??? ? ? ? ? ? ? android:orientation="horizontal">??? ? ???? ? ? ? <View??? ? ???? ? ? ? ? ? ? ? style="@style/layout_horizontal"??? ? ???? ? ? ? ? ? ? ? android:background="#aa0000"??? ? ???? ? ? ? ? ? ? ? android:layout_weight="1"/>??? ? ???? ? ? ? <View??? ? ???? ? ? ? ? ? ? ? style="@style/layout_horizontal"??? ? ???? ? ? ? ? ? ? ? android:background="#00aa00"??? ? ???? ? ? ? ? ? ? ? android:layout_weight="4"/>??? ? ???? ? ? ? <View??? ? ???? ? ? ? ? ? ? ? style="@style/layout_horizontal"??? ? ???? ? ? ? ? ? ? ? android:background="#0000aa"??? ? ???? ? ? ? ? ? ? ? android:layout_weight="3"/>??? ? ???? ? ? ? <View??? ? ???? ? ? ? ? ? ? ? style="@style/layout_horizontal"??? ? ???? ? ? ? ? ? ? ? android:background="#aaaaaa"??? ? ???? ? ? ? ? ? ? ? android:layout_weight="2"/>? ? ? ???? ? ? ? ??? ? ? </LinearLayout> ??? ? ? <LinearLayout ??? ? ? ? ? ? ? style="@style/layout_vertical"??? ? ? ? ? ? ? android:layout_weight="2"??? ? ? ? ? ? ? android:orientation="vertical">??? ? ? ? ? ? ? <View??? ? ???? ? ? ? ? ? ? ? style="@style/layout_vertical"??? ? ???? ? ? ? ? ? ? ? android:background="#ffffff"??? ? ???? ? ? ? ? ? ? ? android:layout_weight="4"/>? ? ? ? ??? ? ???? ? ? ? <View??? ? ???? ? ? ? ? ? ? ? style="@style/layout_vertical"??? ? ???? ? ? ? ? ? ? ? android:background="#aa0000"??? ? ???? ? ? ? ? ? ? ? android:layout_weight="3"/>??? ? ???? ? ? ? <View??? ? ???? ? ? ? ? ? ? ? style="@style/layout_vertical"??? ? ???? ? ? ? ? ? ? ? android:background="#00aa00"??? ? ???? ? ? ? ? ? ? ? android:layout_weight="2"/>??? ? ???? ? ? ? <View??? ? ???? ? ? ? ? ? ? ? style="@style/layout_vertical"??? ? ???? ? ? ? ? ? ? ? android:background="#0000aa"??? ? ???? ? ? ? ? ? ? ? android:layout_weight="1"/>???? ? ? </LinearLayout></LinearLayout>

整个界面布局看起来非常直观,只是嵌套的逻辑要自己理下。显示效果如下图,其中左面一个是480x800的界面,右面的是320x480的界面(后面的图也如此),可以看出显示比例和代码中完全一致,我就不多说了,大家对照下就能看出来了。

读书人网 >Android

热点推荐