4种Android屏幕自适应解决方案(一)

来源:互联网 发布:linux重定向到文件 编辑:程序博客网 时间:2024/05/16 15:31
原文:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=172374&page=1&extra=#pid1466107
下面介绍的四种方式我都用过的,不过自己并没有整理,看到上面链接文中整理的感觉挺全面,就在这里留个爪印。。

一、细说layout_weight
    目前最为推荐的Android多屏幕自适应解决方案。
    该属性的作用是决定控件在其父布局中的显示权重,一般用于线性布局中。其值越小,则对应的layout_width或layout_height的优先级就越高,一般横向布局中,决定的是layout_width的优先级;纵向布局中,决定的是layout_height的优先级。
    传统的layout_weight使用方法是将当前控件的layout_widthlayout_height都设置成fill_parent,这样就可以把控件的显示比例完全交给layout_weight;这样使用的话,就出现了layout_weight越小,显示比例越大的情况。不过对于2个控件还好,如果控件过多,且显示比例也不相同的时候,控制起来就比较麻烦了,毕竟反比不是那么好确定的。
    于是就有了现在最为流行的0px设值法。看似让人难以理解的layout_height=0px的写法,结合layout_weight,却可以使控件成正比例显示,轻松解决了当前Android开发最为头疼的碎片化问题之一。
    先看下面的stylesstyle_layout.xml

<?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_widthlayout_height两个属性被我封装成了4style
    根据实际布局情况,选用当中的一种,不需要自己设置,看过我前一个ActivityGroupDemo的同学应该非常熟悉了
    然后我的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:layout_weight="1"
android:background="#aa0000" />

<View
style="@style/layout_horizontal"
android:layout_weight="4"
android:background="#00aa00" />

<View
style="@style/layout_horizontal"
android:layout_weight="3"
android:background="#0000aa" />

<View
style="@style/layout_horizontal"
android:layout_weight="2"
android:background="#aaaaaa" />
</LinearLayout>

<LinearLayout
style="@style/layout_vertical"
android:layout_weight="2"
android:orientation="vertical" >

<View
style="@style/layout_vertical"
android:layout_weight="4"
android:background="#ffffff" />

<View
style="@style/layout_vertical"
android:layout_weight="3"
android:background="#aa0000" />

<View
style="@style/layout_vertical"
android:layout_weight="2"
android:background="#00aa00" />

<View
style="@style/layout_vertical"
android:layout_weight="1"
android:background="#0000aa" />
</LinearLayout>

</LinearLayout>


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

1.png 
二、自定义尺寸法

    这个是我自己想出来的方法,可能是个比较笨的方法,所以没有多少人提过用这种方法解决自适应的问题。虽然这个方法缺点也很多,但有时候也是个不错的方法。
    先看下面这张图
2-1.jpg 
    可以看到我定义了两套尺寸文件,我们可以看下其中一个文件

<?xml version="1.0" encoding="utf-8"?>
<resources>

<dimen name="height_1_80">6px</dimen>
<dimen name="height_2_80">12px</dimen>
<dimen name="height_3_80">18px</dimen>
<dimen name="height_4_80">24px</dimen>
<dimen name="height_5_80">30px</dimen>
<dimen name="height_6_80">36px</dimen>
<dimen name="height_7_80">42px</dimen>
<dimen name="height_8_80">48px</dimen>
<dimen name="height_9_80">54px</dimen>
<dimen name="height_10_80">60px</dimen>
<dimen name="height_11_80">66px</dimen>
<dimen name="height_12_80">72px</dimen>
<dimen name="height_13_80">78px</dimen>
<dimen name="height_14_80">84px</dimen>
<dimen name="height_15_80">90px</dimen>
<dimen name="height_16_80">96px</dimen>
<dimen name="height_17_80">102px</dimen>
<dimen name="height_18_80">108px</dimen>
<dimen name="height_19_80">114px</dimen>
<dimen name="height_20_80">120px</dimen>
<dimen name="height_21_80">126px</dimen>
<dimen name="height_22_80">132px</dimen>
<dimen name="height_23_80">138px</dimen>
<dimen name="height_24_80">144px</dimen>
<dimen name="height_25_80">150px</dimen>
<dimen name="height_26_80">156px</dimen>
<dimen name="height_27_80">162px</dimen>
<dimen name="height_28_80">168px</dimen>
<dimen name="height_29_80">174px</dimen>
<dimen name="height_30_80">180px</dimen>
<dimen name="height_31_80">186px</dimen>
<dimen name="height_32_80">192px</dimen>
<dimen name="height_33_80">198px</dimen>
<dimen name="height_34_80">204px</dimen>
<dimen name="height_35_80">210px</dimen>
<dimen name="height_36_80">216px</dimen>
<dimen name="height_37_80">222px</dimen>
<dimen name="height_38_80">228px</dimen>
<dimen name="height_39_80">234px</dimen>
<dimen name="height_40_80">240px</dimen>
<dimen name="height_41_80">246px</dimen>
<dimen name="height_42_80">252px</dimen>
<dimen name="height_43_80">258px</dimen>
<dimen name="height_44_80">264px</dimen>
<dimen name="height_45_80">270px</dimen>
<dimen name="height_46_80">276px</dimen>
<dimen name="height_47_80">282px</dimen>
<dimen name="height_48_80">288px</dimen>
<dimen name="height_49_80">294px</dimen>
<dimen name="height_50_80">300px</dimen>
<dimen name="height_51_80">306px</dimen>
<dimen name="height_52_80">312px</dimen>
<dimen name="height_53_80">318px</dimen>
<dimen name="height_54_80">324px</dimen>
<dimen name="height_55_80">330px</dimen>
<dimen name="height_56_80">336px</dimen>
<dimen name="height_57_80">342px</dimen>
<dimen name="height_58_80">348px</dimen>
<dimen name="height_59_80">354px</dimen>
<dimen name="height_60_80">360px</dimen>
<dimen name="height_61_80">366px</dimen>
<dimen name="height_62_80">372px</dimen>
<dimen name="height_63_80">378px</dimen>
<dimen name="height_64_80">384px</dimen>
<dimen name="height_65_80">390px</dimen>
<dimen name="height_66_80">396px</dimen>
<dimen name="height_67_80">402px</dimen>
<dimen name="height_68_80">408px</dimen>
<dimen name="height_69_80">414px</dimen>
<dimen name="height_70_80">420px</dimen>
<dimen name="height_71_80">426px</dimen>
<dimen name="height_72_80">432px</dimen>
<dimen name="height_73_80">438px</dimen>
<dimen name="height_74_80">444px</dimen>
<dimen name="height_75_80">450px</dimen>
<dimen name="height_76_80">456px</dimen>
<dimen name="height_77_80">462px</dimen>
<dimen name="height_78_80">468px</dimen>
<dimen name="height_79_80">474px</dimen>
<dimen name="height_80_80">480px</dimen>
</resources>


?
这个是values-480x320文件夹下dimens_height.xml文件中的代码,我把整个高度分成了80等分,这是因为大部分屏幕的宽度或高度都是80的整数倍(个别特殊的除外),不同的等分在不同的分辨率中设定不同的尺寸值。
    由于每一套界面都要写一套,所以有些同学可能觉着不太好,不过这个写起来比较简单,而且以后也不用改,所以有时候也可以考虑用一下!


    再看我Demo的布局代码(dimen_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<View
android:layout_width="@dimen/width_76_80"
android:layout_height="@dimen/height_10_80"
android:layout_margin="@dimen/width_2_80"
android:background="#ffcccc" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<View
android:layout_width="@dimen/width_30_80"
android:layout_height="@dimen/height_50_80"
android:layout_margin="@dimen/height_5_80"
android:background="#ccccff" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:layout_width="@dimen/width_30_80"
android:layout_height="@dimen/height_5_80"
android:layout_marginBottom="@dimen/height_1_80"
android:background="#ccffcc"
android:text="5" />

<Button
android:layout_width="@dimen/width_30_80"
android:layout_height="@dimen/height_10_80"
android:layout_marginBottom="@dimen/height_1_80"
android:background="#ccffcc"
android:text="10" />

<Button
android:layout_width="@dimen/width_30_80"
android:layout_height="@dimen/height_15_80"
android:layout_marginBottom="@dimen/height_1_80"
android:background="#ccffcc"
android:text="15" />

<Button
android:layout_width="@dimen/width_30_80"
android:layout_height="@dimen/height_20_80"
android:background="#ccffcc"
android:text="20" />
</LinearLayout>
</LinearLayout>

</LinearLayout>


?

    以上是我写的统一的布局代码,来看下在两个不同分辨率的模拟器上的显示效果吧(大家注意我的代码中有margin这样的值也用到了自定义尺寸,如果这个margin使用layout_weight来控制的话,无疑要多嵌套一层线性布局,所以说没有一个方法是十全十美的,这第2个方法有时候用起来反而还要方便一些)

2.png