三星Galaxy Note 3的布局不同

时间:2013-11-26 16:48:59

标签: android layout samsung-mobile

在我的Android应用程序中,我有一个页面,我想在页面中缩放相对于屏幕大小的页面上的所有对象。我为此创建了一个自定义布局。它在我们测试过的所有设备上运行得非常漂亮,除了1个用户使用三星Galaxy Note 3.在该设备上,一切都缩放得太大,并且在边缘,甚至是最高边缘运行。似乎布局本身已经越过屏幕边缘,并且在ActionBar选项卡后面。这是我的代码:

package org.mesonet.app;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;



public class ScalableLayout extends LinearLayout
{
    static final int STANDARD_DPI = 160;
    private float mBaseWidthInches;
    private float mBaseHeightInches;

    private float mWidthScale;
    private float mHeightScale;
    private float mTextScale;

    private boolean mFirstMeasure = true;



    public ScalableLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        TypedValue dimen = new TypedValue();
        getResources().getValue(R.dimen.base_width, dimen, true);
        mBaseWidthInches = dimen.getFloat();

        getResources().getValue(R.dimen.base_height, dimen, true);
        mBaseHeightInches = dimen.getFloat();

        DisplayMetrics display = new DisplayMetrics();

        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(display);

        float xDpi = display.xdpi;
        float yDpi = display.ydpi;

        if(Math.round(xDpi) == STANDARD_DPI)
            xDpi = display.densityDpi;
        if(Math.round(yDpi) == STANDARD_DPI)
            yDpi = display.densityDpi;

        float currentWidthInches = display.widthPixels / xDpi;
        float currentHeightInches = display.heightPixels / yDpi;

        mWidthScale = currentWidthInches / mBaseWidthInches;
        mHeightScale = currentHeightInches / mBaseHeightInches;

        mTextScale = (mHeightScale / display.scaledDensity) / getResources().getConfiguration().fontScale;
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        View view;

        for(int i = 0; i < getChildCount(); i++)
        {
            view = getChildAt(i);

            if(mFirstMeasure)
            {
                LayoutParams params = (LayoutParams)view.getLayoutParams();
                params.leftMargin *= mWidthScale;
                params.topMargin *= mHeightScale;
                params.rightMargin *= mWidthScale;
                params.bottomMargin *= mHeightScale;

                if(params.width != LayoutParams.WRAP_CONTENT && params.width != LayoutParams.MATCH_PARENT)
                    params.width *= mWidthScale;

                if(params.height != LayoutParams.WRAP_CONTENT && params.height != LayoutParams.MATCH_PARENT)
                    params.height *= mHeightScale;

                if(view instanceof TextView)
                    ((TextView)view).setTextSize(((TextView)view).getTextSize() * mTextScale);

                view.setLayoutParams(params);
            }
        }

        mFirstMeasure = false;

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }



    @Override
    protected void onLayout(boolean inChanged, int inLeft, int inTop, int inRight, int inBottom)
    {
       if(Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1)
       {
           super.onLayout(inChanged, inLeft, inTop, inRight, inBottom);
           return;
       }

       super.onLayout(inChanged, Math.round(inLeft * mWidthScale), Math.round(inTop * mHeightScale), Math.round(inRight * mWidthScale), Math.round(inBottom * mHeightScale));
    }
}

1 个答案:

答案 0 :(得分:0)

我创建了ScalableLayout。

这可能是你想要的,我希望这会对你有所帮助。

此ScalableLayout缩放布局上相对于布局大小的所有对象。

https://github.com/ssomai/ScalableLayout