首页 > 移动平台 > 详细

Android布局多分辨率适配处理

时间:2015-10-07 12:01:57      阅读:256      评论:0      收藏:0      [点我收藏+]

如今Android已经占据了全球移动操作系统80%的市场份额,每年厂商们都会推出各自品牌的Android手机,与此同时Android系统的碎片化也在加剧.Android手机多分辨率的处理是每个开发者都要面对的问题.本人也分享一点个人的见解与经验,权供参考~~

首先,在Eclipse工程中,drawable-ldpi,,drawable-mdpi,,drawable-xdpi和drawable-xhdpi这几个文件夹用来存放不同分辨率的图片(当然你也可以自定义新的文件夹),其中ldpi分辨率现在已经很少使用.这些分辨率的详细参数可以在网上搜索到.mdpi,xdpi,xhdpi这三种分辨率的图片的尺寸比例通常为2:3:4,就是说如果你有一张在xhdpi下大小合适的图片,将它的长宽(长宽比不变)缩小为3/4,1/2便可分别适配到xdpi和mdpi下.

其次重点说下XML布局的编写.我们看一下某流行IM应用的一个布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@id/root_fr" android:background="@color/sns_bg_color" android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <com.tencent.mm.ui.KeyboardLinearLayout android:id="@id/root" android:layout_width="fill_parent" android:layout_height="fill_parent">
        <ScrollView android:id="@id/scroll_view" android:background="@color/white" android:layout_width="fill_parent" android:layout_height="fill_parent">
            <LinearLayout android:orientation="vertical" android:id="@id/upload_content" android:background="@color/white" android:paddingLeft="@dimen/LargePadding" android:paddingRight="@dimen/LargePadding" android:layout_width="fill_parent" android:layout_height="fill_parent">
                <com.tencent.mm.plugin.sns.ui.RangeWidget android:id="@id/rang_widget_top" android:background="@drawable/comm_list_item_selector" android:paddingTop="@dimen/MiddlePadding" android:paddingBottom="@dimen/MiddlePadding" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/LargePadding" />
                <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/LargePadding">
                    <com.tencent.mm.plugin.sns.ui.SnsEditText android:gravity="center_vertical" android:id="@id/sns_desc_tv" android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_marginTop="@dimen/LargePadding" android:hint="@string/sns_upload_sns_hint" android:maxLines="7" android:singleLine="false" android:layout_weight="1.0" android:inputType="textCapSentences|textMultiLine" style="@style/MMEditText" />
                </LinearLayout>
                <LinearLayout android:orientation="horizontal" android:id="@id/widget_content" android:paddingBottom="@dimen/MiddlePadding" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/LargePadding" android:minHeight="@dimen/NormalListHeight" />
                <com.tencent.mm.plugin.sns.ui.LocationWidget android:id="@id/location_widget" android:background="@drawable/comm_list_item_selector" android:paddingTop="@dimen/MiddlePadding" android:paddingBottom="@dimen/MiddlePadding" android:layout_width="fill_parent" android:layout_height="wrap_content" />
                <com.tencent.mm.plugin.sns.ui.RangeWidget android:id="@id/rang_widget_bottom" android:background="@drawable/comm_list_item_selector" android:paddingTop="@dimen/MiddlePadding" android:paddingBottom="@dimen/MiddlePadding" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/LargestPadding" />
                <com.tencent.mm.plugin.sns.ui.AtContactWidget android:id="@id/at_contact_widget" android:background="@drawable/comm_list_item_selector" android:paddingTop="@dimen/MiddlePadding" android:paddingBottom="@dimen/MiddlePadding" android:layout_width="fill_parent" android:layout_height="wrap_content" />
                <com.tencent.mm.plugin.sns.ui.SnsUploadConfigView android:id="@id/config_view" android:layout_width="fill_parent" android:layout_height="wrap_content" />
            </LinearLayout>
        </ScrollView>
    </com.tencent.mm.ui.KeyboardLinearLayout>
    <LinearLayout android:gravity="bottom" android:layout_gravity="bottom" android:layout_width="fill_parent" android:layout_height="fill_parent">
        <com.tencent.mm.plugin.sns.ui.SnsUploadSayFooter android:gravity="bottom" android:layout_gravity="bottom" android:id="@id/say_footer" android:layout_width="fill_parent" android:layout_height="wrap_content" />
    </LinearLayout>
</FrameLayout>

众所周知,在Android的布局编写中,我们是用dp这个单位来取代px的.事实上,如果对一些尺寸较大的控件用dp来表示其长宽效果也是不尽人意的.从上面代码中可以发现,控件的

layout_width和layout_height属性大多使用"fill_parent"和"wrap_content",而各种margin和padding代表的间距距离则用dimens这个文件中的值.dimens是你在res\values\目录下建的xml文件,用来保存尺寸大小.下面是一个dimens的例子:

<?xml version="1.0" encoding="utf-8"?> 
  
<resources> <dimen name="text_width">100px</dimen>
  
<dimen name="text_height">50px</dimen> 
  
<dimen name="btn_width">30mm</dimen> 
  
<dimen name="btn_height">10mm</dimen> 
  
</resources>

我们可以新建values-mdpi,values-hdpi等多个目录,并在每个目录下都重写dimens.xml里对应分辨率的值,这样应用在不同分辨率的手机中运行时就会自动调用到不同的dimen的值.以上就是使用dimens的好处.

很多时候,我们控件的资源是动态加载的,例如需要从网上下载图片的ImageView.这样我们需要保证控件的大小一直都是固定的,仅用"wrap_parent"表示其长宽是不够的.Android系统为我们提供了五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、AbsoluteLayout(绝对布局)、TablelLayout(表格布局)、RelativeLayout(相对布局).其中RelativeLayout最为灵活,属性也较多,是最推荐采用的布局.LinearLayout也较为常用,它与RelativeLayout不同之处有一点就是LinearLayout多了一个"layout_weight"属性.我们用来固定控件大小和位置就是靠这个属性.顾名思义,"weight"代表着控件的"权重",但它并不是简单的按照值的大小比例去排列控件,具体它的规则就不多说了.

Android布局多分辨率适配处理

原文:http://www.cnblogs.com/medicus/p/3905727.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!