0
在我的Android應用程序中,我有一個頁面,我想在該頁面中縮放頁面上相對於屏幕大小的所有對象。爲此我創建了一個自定義佈局。除了1位用戶使用三星Galaxy Note 3外,它在我們測試過的所有設備上都能很好地工作。在該設備上,所有設備的尺寸都過大,並且運行在邊緣,甚至是頂部邊緣。看起來好像佈局本身正在運行在屏幕的邊緣,並在ActionBar選項卡後面。這裏是我的代碼:三星Galaxy Note 3的佈局不同
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));
}
}
雖然此鏈接可以回答這個問題,最好是在這裏有答案的主要部件,並提供鏈接以供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 –
@MarcelGwerder對,我很抱歉。我改變了我的答案。 –
我的版本也相對於版面大小縮放。問題在於它對Galaxy Note設備不正確。 – KairisCharm