嗨,我正在使用显示指标为所有设备做一个应用程序。我尝试使用下面的代码,但在某些设备中,如 1024/600,480/800 和密度 160,它工作完美。但在其他一些设备中,如 320/480 密度 160 文本大小是问题和显示图像时应用程序分发。请任何有想法的人建议我....
register.class:
public class register extends Activity {
/** Called when the activity is first created. */
float screenHeight,screenWidth,screendensity;
float textsize1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
screendensity = displaymetrics.densityDpi;
Log.i("screenHeight",""+screenHeight);
Log.i("screenWidth",""+screenWidth);
Log.i("screendensity",""+screendensity);
switch(displaymetrics.densityDpi){
case DisplayMetrics.DENSITY_HIGH:
//set text size
textsize1=10;
break;
case DisplayMetrics.DENSITY_MEDIUM:
//set text size
textsize1=15;
break;
case DisplayMetrics.DENSITY_LOW:
textsize1=8;
//set text size
break;
}
setContentView(R.layout.registerlh);
prntnm=(EditText)findViewById(R.id.parentedit);
prntnm.setTextColor(Color.BLACK);
InputFilter[] FilterArray1 = new InputFilter[1];
FilterArray1[0] = new InputFilter.LengthFilter(20);
prntnm.setFilters(FilterArray1);
RelativeLayout.LayoutParams parentedit1 = (RelativeLayout.LayoutParams) prntnm.getLayoutParams();
parentedit1.height=(int)(50*(screenHeight/600));
parentedit1.width= (int)(300*(screenWidth/1024));
parentedit1.topMargin=(int)(330*(screenHeight/600));
parentedit1.leftMargin=(int)(670*(screenWidth/1024));
}
}