Android 屏幕适配之框架(AndroidScreenAdaptation)适配

AndroidScreenAdaptation框架

 

1.链接

https://github.com/yatoooon/AndroidScreenAdaptation

 

 

 

 

2.说明

完全不用改变自己的布局编写习惯,你原先是怎么写布局,就怎么写布局.不用去继承适配类,不用在最外层包裹适配布局,不用新建茫茫多的分辨率适配文件夹,不要求强制使用px为单位,支持代码动态添加view适配,可以实时预览布局,满足旋转和分屏适配,全面屏或带虚拟按键手机适配也没问题。

 

 

 

 

3.使用

 

3.1.添加依赖

implementation 'me.yatoooon:screenadaptation:1.1.1'

 

 

3.2.全局入口初始化

public class MyApplication extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
        ScreenAdapterTools.init(this);
    }
}

 

 

3.3.清单文件中配置

        <meta-data
            android:name="designwidth"
            android:value="1080" />
        <meta-data
            android:name="designdpi"
            android:value="480" />
        <meta-data
            android:name="fontsize"
            android:value="1.0" />
        <meta-data
            android:name="unit"
            android:value="px" />

 

字段说明:

 

designwidth:设计图的宽,单位是像素,推荐用markman测量,量出来如果是750px那么请尽量去找ui设计师要一份android的设计图。

 

designdpi:设计图对应的标准dpi,根据下面的那张图找到对应的dpi,比如1080就对应480dpi,如果拿到的是其他宽度的设计图,那么选择一个相近的dpi就好了。

 

fontsize:全局字体的大小倍数

 

 

unit:单位 是PX还是DP。

 

 

设置好这四个配置(单位选择PX)后 再编写xml布局文件时直接写UI设计图上的PX单位即可。

 

比如 在1080X1920 480dpi的设备 上UI设计图上有一个TextView的尺寸为100X150(PX) 字体大小为30px

 

designwidth:1080

 

designdpi:480

 

fontsize:1.0

 

unit:px

 

TextView 定义如下:

<TextView
            android:layout_width="100px"
            android:layout_height="150px"
            android:textSize="30px"


            android:text="你好"
            android:gravity="center"/>

 

即View的宽高直接写成设计图上的px单位即可。。。

 

 

 

3.4.使用

public class ScreenActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen);
        ScreenAdapterTools.getInstance().loadView(getWindow().getDecorView());
    }

}

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <TextView
        android:layout_width="400px"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20px"
        android:layout_marginTop="20px"
        android:layout_marginRight="20px"
        android:layout_marginBottom="20px"
        android:background="#ccc"
        android:gravity="center"
        android:text="Hello World!"
        android:textSize="50px" />

    <TextView
        android:layout_width="600px"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="20px"
        android:layout_marginTop="20px"
        android:layout_marginRight="20px"
        android:layout_marginBottom="20px"
        android:background="#ccc"
        android:gravity="center"
        android:text="Hello World!"
        android:textSize="50px" />


</RelativeLayout>

 

activity_screen布局中使用px还是dp取决于清单文件中下面的配置

 

px:

 <meta-data
     android:name="unit"
     android:value="px" />

 

dp:

 <meta-data
     android:name="unit"
     android:value="dp" />

 

 

3.5.效果

 

3.5.1.1080x1920 480dpi 设备

 

适配前

 

适配后

 

 

3.5.2.480x800 240dpi 设备

 

适配前

 

 

适配后

 

 

3.5.3.720x1280 320dpi 设备

 

适配前

 

 

适配后

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值