Android自适应屏幕

做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>


布局中组件的宽高在res文件中如图

系统会根据手机屏幕加载对于文件夹下的dimens文件属性,效果如下:






上面的手机分辨率分别为800x480和480x320的,没有匹配到的手机会自动加载最接近当前手机分辨率的dimens。

3.在Java代码中设置组件宽高值


首先在Java代码中要获取手机屏幕的宽高值,再根据这个值设置组件的宽高值,这个较简单就不详细说明了。

4.为不同屏幕的手机做布局

对于不同屏幕的手机,在res下创建不同分辨率的文件夹,系统会自动加载不同屏幕的布局文件,没有匹配到时加载最接近当前手机分辨率的布局文件,如图:

大家如果还有更好的方法可以说出来大家学习学习,谢谢哈!


本demo下载地址:http://download.youkuaiyun.com/detail/jxgzycxyxhp/8487479
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智行码上说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值