做Android开发已经很久了,今天我就跟大家分享屏幕适配的心得及方法,说的不对的地方希望大家指正。
没有做过屏幕适配不要担心,其实屏幕适配也不难。屏幕适配大概有四种方式
1.通过weight熟悉自适应屏幕,比如:
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>
整个布局文件都是通过weight来设置组件的,所以能适应所有屏幕
2.通过自定义尺寸自适应屏幕,如:
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="match_parent"
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_20_80"
android:layout_height="@dimen/height_31_80"
android:layout_margin="@dimen/height_2_80"
android:background="#ccccff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="@dimen/width_20_80"
android:layout_height="@dimen/height_2_80"
android:layout_marginBottom="@dimen/height_1_80"
android:background="#ccffcc"
android:text="5" />
<Button
android:layout_width="@dimen/width_20_80"
android:layout_height="@dimen/height_6_80"
android:layout_marginBottom="@dimen/height_1_80"
android:background="#ccffcc"
android:text="10" />
<Button
android:layout_width="@dimen/width_20_80"
android:layout_height="@dimen/height_9_80"
android:layout_marginBottom="@dimen/height_1_80"
android:background="#ccffcc"
android:text="15" />
<Button
android:layout_width="@dimen/width_20_80"
android:layout_height="@dimen/height_14_80"
android:background="#ccffcc"
android:text="20" />
</LinearLayout>
</LinearLayout>
</LinearLayout>


上面的手机分辨率分别为800x480和480x320的,没有匹配到的手机会自动加载最接近当前手机分辨率的dimens。
3.在Java代码中设置组件宽高值
首先在Java代码中要获取手机屏幕的宽高值,再根据这个值设置组件的宽高值,这个较简单就不详细说明了。
4.为不同屏幕的手机做布局
对于不同屏幕的手机,在res下创建不同分辨率的文件夹,系统会自动加载不同屏幕的布局文件,没有匹配到时加载最接近当前手机分辨率的布局文件,如图:

大家如果还有更好的方法可以说出来大家学习学习,谢谢哈!
本demo下载地址:http://download.youkuaiyun.com/detail/jxgzycxyxhp/8487479
没有做过屏幕适配不要担心,其实屏幕适配也不难。屏幕适配大概有四种方式
1.通过weight熟悉自适应屏幕,比如:
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>
整个布局文件都是通过weight来设置组件的,所以能适应所有屏幕
2.通过自定义尺寸自适应屏幕,如:
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="match_parent"
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_20_80"
android:layout_height="@dimen/height_31_80"
android:layout_margin="@dimen/height_2_80"
android:background="#ccccff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="@dimen/width_20_80"
android:layout_height="@dimen/height_2_80"
android:layout_marginBottom="@dimen/height_1_80"
android:background="#ccffcc"
android:text="5" />
<Button
android:layout_width="@dimen/width_20_80"
android:layout_height="@dimen/height_6_80"
android:layout_marginBottom="@dimen/height_1_80"
android:background="#ccffcc"
android:text="10" />
<Button
android:layout_width="@dimen/width_20_80"
android:layout_height="@dimen/height_9_80"
android:layout_marginBottom="@dimen/height_1_80"
android:background="#ccffcc"
android:text="15" />
<Button
android:layout_width="@dimen/width_20_80"
android:layout_height="@dimen/height_14_80"
android:background="#ccffcc"
android:text="20" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
布局中组件的宽高在res文件中如图
系统会根据手机屏幕加载对于文件夹下的dimens文件属性,效果如下:
上面的手机分辨率分别为800x480和480x320的,没有匹配到的手机会自动加载最接近当前手机分辨率的dimens。
3.在Java代码中设置组件宽高值
首先在Java代码中要获取手机屏幕的宽高值,再根据这个值设置组件的宽高值,这个较简单就不详细说明了。
4.为不同屏幕的手机做布局
对于不同屏幕的手机,在res下创建不同分辨率的文件夹,系统会自动加载不同屏幕的布局文件,没有匹配到时加载最接近当前手机分辨率的布局文件,如图:
大家如果还有更好的方法可以说出来大家学习学习,谢谢哈!
本demo下载地址:http://download.youkuaiyun.com/detail/jxgzycxyxhp/8487479