关于Android适配,常用这几种就够了

前言

Android 适配问题,让每个Android程序员头疼问题,听起来可能有些夸大,但是相信每个Android开发人员都碰到过棘手的适配问题。
关于Android适配的文章,各种论坛,眼花缭乱,有的很有营养,看完收货很大;有的则完全是复制粘贴,鱼目混珠.
我仅仅将自己在开发中用到的,感觉对适配有一定帮助的东西,写下来,希望能帮助到和我遇到过相同问题的人

正文

关于Android适配,google比我们想的更多,给我们提供了DP(DIP)-device independent pixels(设备独立像素).在一些简单布局中还是有用的,碰到一些复杂布局,DP完全满足不了需求,不然也不会让大家头疼的适配问题。

关于px/dp/ppi等概念,大家都很了解,我就不说这些,还不太明白的同学,可以看看

八一八那些px、pt、ppi、dpi、dp、sp之间的关系
详解Android开发中常用的 DPI / DP / SP

其实简单的适配,我们利用LinearLayout的layout_widget就可以完成比例的适配,但这远远不够,可以利用其它方法

这些文章已经说的很明了,所以废话不多说,直接讲讲适配问题

1、dimens

针对一些常见分辨率机型,设置dimens


友盟统计Android设备分辨率

dimens类似于drawable,drawable中我们设置


設置不同drawable

dimens则比drawable灵活,在values文件夹根据限定符去命名dimens

1.屏幕分辨率

例如1920x1080,1280x720,表示适配分辨率为1920x1080或者1280x720的设备
res/values-1920x1080/dimens.xml

2.屏幕像素密度

例如hdpi,适配屏幕像素密度值近似或等于hdpi(320dpi)的设备。
res/values-hdpi/dimens.xml

3.屏幕尺寸

例如 sw720dp,适配最低屏幕可用区域为720dp的设备,同理还有sw1080dp.
res/values-sw720dp/dimens.xml

4.屏幕方向

分为横向(land)和纵向(port),分别适配屏幕方向为横向或纵向的屏幕
res/values-land/dimens.xml
类似的限定符就不一一列举,就列出常见的限定符
但是这些限定符一般不是单独使用,而是组合使用,例如values-land-mdpi-800x480,values-xhdpi-port-1920×1080,

所以适配时,在values-land-mdpi-800x480中,定义 px100表示100px ,
<dimen name= "px100" >100px</ dimen>
则在values-hdpi-port-1280×720,px100则定义为mdpi的两倍
<dimen name= "px100" >200px</ dimen>
在布局中,就可以灵活使用dimens

<LinearLayout
android:layout_width="@dimen/px100"
android:layout_height="@dimen/px100"
android:orientation="vertical" />

针对不同分辨率,设置dimens实现适配,对市场上大部分主流分辨率,适配是没有问题的。
但是!!!Android设置碎片化实在是太严重,随便哪个厂家或者山寨厂商推出奇葩的分辨率,对我们都是莫大的伤害,然并卵,我们还是逃不掉,但是很多时候,丧心病狂的分辨率是我们无法预料的,我们不可能把所以可能出现的组合情况,全部设置dimens。那么,dimens也不能完全满足我们,就得想办法寻找新途径.

2.百分比适配

百分比适配,显而易见,通过设置百分比,实现控件适配,在Google没有提供百分比适配之前,我们使用的android:layout_weight,其实就是一种百分比,但是只能在LinearLayout中使用。
在Android 22开始,google提供了相当方便的百分比适配
使用很简单,依赖compile 'com.android.support:percent:24.0.0'
提供了PercentRelativeLayout、PercentFrameLayout两种布局,布局中,替换RelativeLayout、FrameLayout
那就简单看看PercentRelativeLayout的使用,PercentFrameLayout同理

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/top_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:background="#ff44aacc"
        android:gravity="center"
        android:text="70%"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="70%" />

    <TextView
        android:id="@+id/top_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/top_left"
        android:background="#ffe40000"
        android:gravity="center"
        android:text="30%"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="30%" />

    <View
        android:id="@+id/bottom_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@+id/top_left"
        android:background="#ff00ff"
        app:layout_heightPercent="80%"
        app:layout_widthPercent="30%" />

    <View
        android:id="@+id/bottom_right_top"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@+id/top_left"
        android:layout_toRightOf="@+id/bottom_left"
        android:background="#ff00ff22"
        app:layout_heightPercent="40%"
        app:layout_widthPercent="70%" />

    <View
        android:id="@+id/bottom_right_bottom"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@+id/bottom_right_top"
        android:layout_toRightOf="@+id/bottom_left"
        android:background="#0000ff"
        app:layout_heightPercent="40%"
        app:layout_widthPercent="70%" />
</android.support.percent.PercentRelativeLayout>

效果图


很简单的布局,效果也很直观
一共有layout_widthPercent、layout_heightPercent、 layout_marginPercent 、layout_marginLeftPercent、 layout_marginTopPercent、layout_marginRightPercent 、 layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent属性提供使用
使用 layout_widthPercent、layout_heightPercent
不能删除默认的 layout_width、layout_height
而是设置 android:layout_width="0dp" android:layout_height="0dp"

3.代码适配

例如,要给一个图片动态设置尺寸,宽高都为屏幕宽度的77.8%
在Activity中,初始化 ivImgbackground;

    CircleImageView ivImgbackground = findViewById(R.id.iv_imgbackground);

    /**
     * 动态设置图片大小
     */
    private void setImageSize() {
        final WindowManager manager = this.getWindowManager();
        //得出屏幕宽度
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        widthPixels = outMetrics.widthPixels;
        //重新计算高度
        layoutParams = ivImgbackground.getLayoutParams();
        layoutParams.width = (int) (0.778 * widthPixels);
        layoutParams.height = (int) (0.778 * widthPixels);
        ivImgbackground.setLayoutParams(layoutParams);
    }

其实也是一种另类的百分比适配

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值