我正在为使用display metrics.i的所有设备做一个应用程序。我尝试使用下面的代码但在一些设备如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));
}
}
答案 0 :(得分:0)
使用尺寸不会硬编码值。对于文本使用“sp”和图像和其他使用“dp”
示例:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textsize">15sp</dimen>
</resources>
在每个值文件夹中使用dimen.xml文件(例如values,values-v11,values-v14)并提供适当的大小。
使用这些值的示例:
textView.setTextSize(getResources().getDimension(R.dimen.textsize));