我在Android应用程序中遇到兼容性问题

时间:2016-05-15 02:39:06

标签: android

我厌倦了这个,我是Android的新手,我制作了一个应用程序,但是我遇到了很多兼容问题,我不知道如何调整每个屏幕到我的应用程序,以及如何制作它适用于api min 15.现在它是19,因为我有很多API问题。

Problem android Screen

所以我决定制作像

这样的文件

RES \布局  水库\布局sw600dp  RES \布局sw720dp

但是,它仍然无法在小屏幕上工作,有人可以帮助我吗?如何调整到每个屏幕或至少使用。如果有人可以帮助我,我会很高兴。

1 个答案:

答案 0 :(得分:0)

我有3种方法可以做到这一点!

  1. 您可以创建多个布局

    res \ layout res \

    See link on Google Developer
    See link on Stackoverflow

  2. 您可以创建多个维度

    This is example on Stackoverflow

  3. 使用代码

    • 创建课程 SetViewSizeByPixel.class

    这是代码

  4. 公共类SetViewSizeByPixel {

    private LayoutParams params = null;
    private DisplayMetrics metrics = null;
    private Context mContext;
    public static final int match_parent = -1;
    
    public SetViewSizeByPixel(Context context) {
        metrics = getMetrics(context);
        this.mContext = context;
    }
    
    /**
     * 
     */
    
    public int RH(int x) {
        return metrics.heightPixels * x / 1920;
    }
    
    public int RW(int x) {
        return metrics.widthPixels * x / 1080;
    }
    
    public int RH720p(int x) {
        return metrics.heightPixels * x / 1280;
    }
    
    public int RW720p(int x) {
        return metrics.widthPixels * x / 720;
    }
    
    public int RH(float x) {
        return (int) (metrics.heightPixels * x / 1920);
    }
    
    public int RW(float x) {
        return (int) (metrics.widthPixels * x / 1080);
    }
    
    /**
     * Metrics
     */
    
    public DisplayMetrics getMetrics(Activity activity) {
        metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }
    
    public DisplayMetrics getMetrics(Context context) {
        return getMetrics((Activity) context);
    }
    
    public DisplayMetrics getMetrics() {
        return metrics;
    }
    
    public int heightPixels() {
        return metrics.heightPixels;
    }
    
    public int widthPixels() {
        return metrics.widthPixels;
    }
    
    public float density() {
        return metrics.density;
    }
    
    public int densityDpi() {
        return metrics.densityDpi;
    }
    
    /**
     * Size
     */
    
    public boolean height(View view, int height) {
        return size(view, -1, height);
    }
    
    public boolean width(View view, int width) {
        return size(view, width, -1);
    }
    
    public boolean square(View view, int x) {
        return size(view, x, x);
    }
    
    public boolean size(View view, int width, int height) {
        try {
            params = view.getLayoutParams();
            if (width != -1)
                params.width = width;
            else
                params.width = LayoutParams.MATCH_PARENT;
    
            if (height != -1)
                params.height = height;
            else
                params.height = LayoutParams.MATCH_PARENT;
            view.setLayoutParams(params);
            return true;
        } catch (Exception e) {
            Log.e("SetSizeByPercent", e.toString());
            return false;
        }
    }
    
    /**
     * Size with RW, RH.
     */
    
    public boolean heightR(View view, int height) {
        return size(view, -1, this.RH(height));
    }
    
    public boolean widthR(View view, int width) {
        return size(view, this.RW(width), -1);
    }
    
    public boolean squareR(View view, int x) {
        return size(view, this.RW(x), this.RH(x));
    }
    
    public boolean sizeR(View view, int width, int height) {
        return size(view, RW(width), RH(height));
    }
    
    /**
     * Margin R
     */
    public void marginTopR(View v, int top) {
        marginTop(v, RH(top));
    }
    
    public void marginR(View v, int lef, int top, int rig, int bot) {
        margin(v, RH(lef), RH(top), RH(rig), RH(bot));
    }
    
    public void marginR(View v, int x) {
        margin(v, RW(x), RH(x), RW(x), RH(x));
    }
    
    /**
     * Margin
     */
    public void marginLeft(View v, int lef) {
        margin(v, lef, -1, -1, -1);
    }
    
    public void marginTop(View v, int top) {
        margin(v, -1, top, -1, -1);
    }
    
    public void marginRight(View v, int rig) {
        margin(v, -1, -1, rig, -1);
    }
    
    public void marginBottom(View v, int bot) {
        margin(v, -1, -1, -1, bot);
    }
    
    public void marginLeft(View v, int lef, boolean keepOldMargin) {
        int k = keepOldMargin ? -1 : 0;
        margin(v, lef, k, k, k);
    }
    
    public void marginTop(View v, int top, boolean keepOldMargin) {
        int k = keepOldMargin ? -1 : 0;
        margin(v, k, top, k, k);
    }
    
    public void marginRight(View v, int rig, boolean keepOldMargin) {
        int k = keepOldMargin ? -1 : 0;
        margin(v, k, k, rig, k);
    }
    
    public void marginBottom(View v, int bot, boolean keepOldMargin) {
        int k = keepOldMargin ? -1 : 0;
        margin(v, k, k, k, bot);
    }
    
    public void margin(View v, int margin) {
        margin(v, margin, margin, margin, margin);
    }
    
    public void margin(View v, int lef, int top, int rig, int bot) {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            lef = (lef == -1) ? p.leftMargin : lef;
            top = (top == -1) ? p.topMargin : top;
            rig = (rig == -1) ? p.rightMargin : rig;
            bot = (bot == -1) ? p.leftMargin : bot;
    
            p.setMargins(lef, top, rig, bot);
            v.requestLayout();
        }
    }
    
    public LayoutParams getParams() {
        return params;
    }
    
    public void setParams(LayoutParams params) {
        this.params = params;
    }
    
    /**
     * Text Size
     */
    public void textSize(TextView tv, int size) {
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }
    
    public void textSize(View view, int size) {
        try {
            ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        } catch (Exception e) {
            Log.e("", e.toString());
        }
    }
    
    public void textSizeR(TextView view, int size) {
        view.setTextSize(TypedValue.COMPLEX_UNIT_PX, RH(size));
    }
    
    public void textSizeR(View view, int size) {
        try {
            ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, RH(size));
        } catch (Exception e) {
            Log.e("", e.toString());
        }
    }
    
    /**
     * Animation & TypeFace
     */
    public Animation getAnimation(int id) {
        return AnimationUtils.loadAnimation(mContext, id);
    }
    
    public Typeface getTypeface(String typefacePath) {
        return Typeface.createFromAsset(mContext.getAssets(), typefacePath);
    }
    
    // public void setSize(View v,int w,int h){
    // float hrate = MyActivity.Height/1920f;
    // size(v, (int) (w*hrate),(int)(h*hrate));
    // }
      }
    

    这是示例

    onCreate()

    中的

    // TODO Auto-generated method stub
        SetViewSizeByPixel setsize = new SetViewSizeByPixel(HighScoreActivity.this);
        DisplayMetrics metris = setsize.getMetrics(HighScoreActivity.this);
    
        // Main_Menu
        setsize.size(layout_header, metris.widthPixels * 1024 / 1024, metris.heightPixels * 60 / 600);
        layout_header.setPadding(metris.widthPixels * 10 / 1024, metris.heightPixels * 10 / 600,
                metris.widthPixels * 10 / 1024, metris.heightPixels * 10 / 600);
        setsize.textSize(txt_top, metris.widthPixels * 15 / 600);
        setsize.textSize(txt_time, metris.widthPixels * 15 / 600);
        setsize.textSize(txt_main_menu, metris.widthPixels * 15 / 600);
        setsize.size(txt_top, metris.widthPixels * 200 / 1024, metris.heightPixels * 40 / 600);
        setsize.size(txt_time, metris.widthPixels * 600 / 1024, metris.heightPixels * 40 / 600);
        setsize.size(txt_main_menu, metris.widthPixels * 204 / 1024, metris.heightPixels * 40 / 600);
    

    注意:

    • 您只需进行屏幕设备测试。并使用photoshop显示图片。

      metris.widthPixels * 600/1024

      600是600px,用Photoshop中的图像测量所需的宽度 1024是1024px,设备屏幕测试的宽度