AppleTree iOS

[안드로이드 어플리케이션] 기기별로 다른 화면크기 맞추는 함수 본문

안드로이드

[안드로이드 어플리케이션] 기기별로 다른 화면크기 맞추는 함수

사과나무 2013. 7. 26. 15:21

안드로이드 어플리케이션 개발을 할때 기기별로 화면크기가 달를때 화면 크기를 맞춰주는 함수


1 소스

public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

        setContentView(R.layout.home_coupon_1);

        if(onInit(getApplicationContext()) == false)

        {

        Toast.makeText(getApplicationContext(), "[쿠폰]초기화 작업 중 에러가 발생했습니다.", Toast.LENGTH_SHORT).show();

    finish();

    return;

        }

}


public boolean onInit(Context context)

{

mContext = context;

setLayout();

return true;

   }


public void setLayout()

{

DisplayMetrics outMetrics = null;

Display display = null;

Drawable drawable = null;

int nWidth = 0;

int nHeight = 0;

int nPX = 0;

TextView tvLine = null;

LinearLayout.LayoutParams paramLine = null;


/**********************************************************************


set display


**********************************************************************/

outMetrics = new DisplayMetrics();

display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

getWindowManager().getDefaultDisplay().getMetrics(outMetrics);

/**********************************************************************


set px

**********************************************************************/

nPX = (int)FunctionClass.convertDpToPixel(10, mContext);

drawable = mContext.getResources().getDrawable(R.drawable.line);

//nHeight = FunctionClass.getFixHeight(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), display.getWidth());

nHeight = (int)(1.5 * outMetrics.density);

paramLine = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, nHeight);

   

paramLine.leftMargin = nPX;

paramLine.topMargin = nPX;

paramLine.rightMargin  = nPX;

drawable = null;

display = null;

   /**********************************************************************


set line 1


   **********************************************************************/

tvLine = (TextView)findViewById(R.id.home_coupon_1_textView_Line_1);

tvLine.setLayoutParams(paramLine);

tvLine = null;

   /**********************************************************************


set line 2


   **********************************************************************/

tvLine = (TextView)findViewById(R.id.home_coupon_1_textView_Line_2);

tvLine.setLayoutParams(paramLine);

tvLine = null;

   /**********************************************************************


set line 3


   **********************************************************************/

tvLine = (TextView)findViewById(R.id.home_coupon_1_textView_Line_3);

tvLine.setLayoutParams(paramLine);

tvLine = null;

paramLine = null;

}


#FunctionClass.java

public static float convertDpToPixel(float dp, Context context)

{

Resources resources = context.getResources();

DisplayMetrics metrics = resources.getDisplayMetrics();

float px = Math.round(dp * metrics.density);

return px;

}


2 사용법

onCreate 안에서

setLayout();


3 생각

나중에는 모든 .java 파일마다 setLayout() 함수를 써주는 것이 아니라 재사용할 수 있도록 따로 빼주는 것도 괜찮을 것 같다.

Comments