如何让一个应用更好的适配不同分辨率的屏幕

安卓的手机碎片化程度严重,所以我们经常会不得不面对这样的情形:布局变形。
以下是工作中总结的一些常用避免方法:
1.根据density不同将布局放在不同的文件夹下,如mdpi、sw600dp等。density的意思是每英寸的像素数目,所以看起来不同大小的屏幕可能density是一致的。

2.尽量使用dp,sp等单位,因为他们能随着density的变化而呈现不同。

3..9图片的使用会减少工作量,它可以自己拉伸。

4.如果在代码中用到具体的位置数字,比如定义LayoutParams时,必须使用dimens中定义的值,不应该写死数字。

5.如果支持横竖屏切换的应用,则在布局文件中应该对应加入横竖状态的两套布局,除非你确认他们不会转或者不影响感官。

6.如果只是想按比例划分长宽的话,可以使用android:weight属性,这玩意可以根据长度自己划分。

7.尽量使用RelativeLayout,因为他可以指定控件之间、控件与屏幕边缘之间的位置关系,不会因为绝对布局这样或者线性布局这样死板。同时也能减少布局层次。

8.如果是要适配手机和较大屏幕的平板,但是又想内容不同的话,可以指定layout-sw600dp等文件夹,将不同布局放入。
res/layout/main.xml,单面板(默认)布局:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">

    <fragment android:id="@+id/headlines" android:layout_height="fill_parent" android:name="com.example.android.newsreader.HeadlinesFragment" android:layout_width="match_parent" />
</LinearLayout>

res/layout-sw600dp/main.xml,双面板布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal">
    <fragment android:id="@+id/headlines" android:layout_height="fill_parent" android:name="com.example.android.newsreader.HeadlinesFragment" android:layout_width="400dp" android:layout_marginRight="10dp"/>
    <fragment android:id="@+id/article" android:layout_height="fill_parent" android:name="com.example.android.newsreader.ArticleFragment" android:layout_width="fill_parent" />
</LinearLayout>

当然也可以通过代码中获取当前density值来动态加载不同的Fragment。

暂时就是想到这些了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值