Android解决AutoLayout不能设置多种设计稿尺寸的问题

张鸿洋大神的AutoLayout确实很好用,极大地解决了Android的适配问题,这个真的可以称得上是进入了全新的适配时代。在此再次感谢大神的无私奉献。

但是还有一个不太完美的地方就是在Androidmanifest里面设置好设计稿尺寸之后就不能修改了,这样在我的项目中就会有一个问题:我们的APP不用支持横屏,但是需要支持一个平板的横屏,所以需要写两套布局,也需要连个设计稿。我查看了AutoLayout的代码,但是没有发现可以更改设计尺寸的方法。最初想到的办法是在BaseActivity中获取屏幕尺寸信息,然后获取Androidmanifest里面 的

 data
            android:name="design_width"
            android:value="720"/>
        data
            android:name="design_height"
            android:value="1280"/>

值并且通过Java代码修改,但是发现Androidmanifest里面的东西只能读取不能修改,不知道是不是我用的方法不对,那只能从AutoLayout的源码下手了,通过查看源码发现在AutoLayoutConifg这个类里获取Androidmanifest的设计尺寸,所以需要做的就是修改这部分信息了。

首先定义一个变量private boolean useDynamicDesignSize;用来判断时候需要使用动态的设计尺寸

然后定义一个方法:

 public AutoLayoutConifg useDynamicDesignSize(int with, int height)
    {
        useDynamicDesignSize = true;
        this.mDesignWidth = with;
        this.mDesignHeight = height;

        return this;
    }

调用这个方法设置设计尺寸

最后修改这个方法:

public void init(Context context)
    {
        if(!useDynamicDesignSize)
        {
            getMetaData(context);
        }

        int[] screenSize = ScreenUtils.getScreenSize(context, useDeviceSize);
        mScreenWidth = screenSize[0];
        mScreenHeight = screenSize[1];
        L.e(" screenWidth =" + mScreenWidth + " ,screenHeight = " + mScreenHeight);
    }
这样就可以根据是否调用了useDynamicDesignSize(int with, int height)方法来觉得是使用哪种设计尺寸了,

以下是修改后的AutoLayoutConifg类:

package com.zhy.autolayout.config;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import com.zhy.autolayout.utils.L;
import com.zhy.autolayout.utils.ScreenUtils;

/**
 * Created by zhy on 15/11/18.
 */
public class AutoLayoutConifg
{

    private static AutoLayoutConifg sIntance = new AutoLayoutConifg();


    private static final String KEY_DESIGN_WIDTH  = "design_width";
    private static final String KEY_DESIGN_HEIGHT = "design_height";

    private int mScreenWidth;
    private int mScreenHeight;

    private int mDesignWidth;
    private int mDesignHeight;

    private boolean useDeviceSize;
    private boolean useDynamicDesignSize;

    private AutoLayoutConifg()
    {
    }

    public void checkParams()
    {
        if(mDesignHeight <= 0 || mDesignWidth <= 0)
        {
            throw new RuntimeException("you must set " + KEY_DESIGN_WIDTH + " and " + KEY_DESIGN_HEIGHT + "  in your manifest file.");
        }
    }

    public AutoLayoutConifg useDeviceSize()
    {
        useDeviceSize = true;
        return this;
    }

    public AutoLayoutConifg useDynamicDesignSize(int with, int height)
    {
        useDynamicDesignSize = true;
        this.mDesignWidth = with;
        this.mDesignHeight = height;

        return this;
    }

    public static AutoLayoutConifg getInstance()
    {
        return sIntance;
    }


    public int getScreenWidth()
    {
        return mScreenWidth;
    }

    public int getScreenHeight()
    {
        return mScreenHeight;
    }

    public int getDesignWidth()
    {
        return mDesignWidth;
    }

    public int getDesignHeight()
    {
        return mDesignHeight;
    }


    public void init(Context context)
    {
        if(!useDynamicDesignSize)
        {
            getMetaData(context);
        }

        int[] screenSize = ScreenUtils.getScreenSize(context, useDeviceSize);
        mScreenWidth = screenSize[0];
        mScreenHeight = screenSize[1];
        L.e(" screenWidth =" + mScreenWidth + " ,screenHeight = " + mScreenHeight);
    }

    private void getMetaData(Context context)
    {
        PackageManager  packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo;
        try
        {
            applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            if(applicationInfo != null && applicationInfo.metaData != null)
            {
                mDesignWidth = (int) applicationInfo.metaData.get(KEY_DESIGN_WIDTH);
                mDesignHeight = (int) applicationInfo.metaData.get(KEY_DESIGN_HEIGHT);
            }
        }
        catch(PackageManager.NameNotFoundException e)
        {
            throw new RuntimeException("you must set " + KEY_DESIGN_WIDTH + " and " + KEY_DESIGN_HEIGHT + "  in your manifest file.", e);
        }

        L.e(" designWidth =" + mDesignWidth + " , designHeight = " + mDesignHeight);
    }


}

到此,大功告成,在获得屏幕尺寸之后如果得出结果认为是在横屏平板上运行,调用 AutoLayoutConifg.getInstance().useDynamicDesignSize(1280, 800).init(this);就可以了。

再次感谢鸿洋大神编写的AutoLayout库!

你可能感兴趣的:(安卓)