当前位置:   article > 正文

Android解决AutoLayout不能设置多种设计稿尺寸的问题_autolayout 如何适配多套设计尺寸

autolayout 如何适配多套设计尺寸
张鸿洋大神的AutoLayout确实很好用,极大地解决了Android的适配问题,这个真的可以称得上是进入了全新的适配时代。在此再次感谢大神的无私奉献。
但是还有一个不太完美的地方就是在Androidmanifest里面设置好设计稿尺寸之后就不能修改了,这样在我的项目中就会有一个问题:我们的APP不用支持横屏,但是需要支持一个平板的横屏,所以需要写两套布局,也需要连个设计稿。我查看了AutoLayout的代码,但是没有发现可以更改设计尺寸的方法。最初想到的办法是在BaseActivity中获取屏幕尺寸信息,然后获取Androidmanifest里面 的
 <meta-data
            android:name="design_width"
            android:value="720"/>
        <meta-data
            android:name="design_height"
            android:value="1280"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
值并且通过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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
调用这个方法设置设计尺寸
最后修改这个方法:
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);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
这样就可以根据是否调用了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);
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/283443?site
推荐阅读
相关标签
  

闽ICP备14008679号