个人浅谈android适配

发布时间:2019-06-23 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了个人浅谈android适配脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

由于AndROId设备众多繁杂,尺寸各有不一,所以android的适配也必将任重道远。
综合了一下目前主要的android适配方案主要有以下几种

1.创建不同的资文件

创建不同的资源文件 values-1920X1080,values-1280x720等等

2.百分比布局支持库

使用谷歌正式提供的百分比布局支持库(PErcent-support-lib),它提供两种布局

PercentFrameLayout,PercentRelativeLayout

支持的属性有:

@H_360_16@
layout_widthPercent
layout_heightPercent 
layout_marginPercent
layout_marginLeftPercent
layout_marginTopPercent
layout_marginRightPercent 
layout_marginBottomPercent
layout_marginStartPercent
layout_marginEndPercent

例子:PercentRelativeLayout

<?XMl version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout

>"http://schemas.android.COM/apk/res/android"
>"http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">

<TextView
    android:id="@+id/row_one_ITem_one"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_alignParentTop="true"
    android:background="#7700ff00"
    android:text="w:70%,h:20%"
    android:gravity="center"
    app:layout_heightPercent="20%"
    app:layout_widthPercent="70%"/>
      
<TextView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_below="@id/row_two_item_one"
    android:background="#770000ff"
    android:gravity="center"
    android:text="width:100%,height:10%"
    app:layout_heightPercent="10%"
    app:layout_widthPercent="100%"/>

</android.support.percent.PercentRelativeLayout>

但是百分比库还是存在一些问题:

  1. 当使用图片时,无法设置高的比例

       比如我们的图片宽高是200*100的,我们在使用过程中我们设置宽高为20%、10%,这样会造成图片的比例失调。为什么呢?因为20%参考的是屏幕的宽度,而10%参考的是屏幕的高度。

    2.很难使用百分比定义一个正方形的控件

    比如,我现在界面的右下角有一个FloatingActionButton,我希望其宽度和高度都为屏幕宽度的10%,很难做到

    3.一个控件的margin四个方向值一致

    有些时候,我设置margin,我希望四边的边距一致的,但是如果目前设置5%,会造成,上下为高度的5%,左右边距为宽度的5%。

    综合上述这些问题,可以发现目前的percent-support-lib并不能完全满足我们的需求,

3.Android AutoLayout适配方式

一款第三方的支持库
AutoLayout所用单位px:这里的px并非是GOOGLE不建议使用的px,在内部会进行转化处理。
支持的属性:
layout_width
layout_height
layout_margin(left,top,right,bottom)
pading(left,top,right,bottom)
textSize
maxWidth, minWidth, maxHeight, minHeight
目前还在完善支持的布局和view。
详细的配置和使用详见https://github.com/hongyangAndroid/AndroidAutoLayout


综上分析

从程序的稳定适用性个人觉得还是通过创建不同的资源文件来适配比较稳妥。
---以上个人根据网络资源总结整理,经验不足,欢迎多多指教---

脚本宝典总结

以上是脚本宝典为你收集整理的个人浅谈android适配全部内容,希望文章能够帮你解决个人浅谈android适配所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。