以编程方式修复Android的屏幕尺寸问题

问题描述:

我正在创建android应用程序。这将动态地产生问题。但基于手机屏幕尺寸视图并不正确。我不知道如何解决这个问题。我附上了来自不同手机的图像。 屏幕尺寸-3.7" ,屏幕尺寸-5.0"以编程方式修复Android的屏幕尺寸问题

enter image description hereenter image description here

所有元素的大小是根据画面size.can任意一个帮助Android的显示响应屏幕上的变化。

我的代码...

lView.setBackgroundColor(Color.parseColor("#FFFFFF")); 
lView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 220); 
GradientDrawable gdtitle = new GradientDrawable(); 

gdtitle.setCornerRadius(5); 
ImageView title = new ImageView(Main2Activity.this); 
title.setImageResource(R.drawable.logo); 
title.setLayoutParams(layoutParams2); 
title.setBackgroundDrawable(gdtitle); 
lView.setOrientation(LinearLayout.VERTICAL); 
lView.addView(title); 

GradientDrawable gd3 = new GradientDrawable(); 
gd3.setCornerRadius(30); 
gd3.setColor(Color.parseColor("#003366")); 
gd3.setStroke(0, 0xFF000000); 

TextView uname1 = new TextView(Main2Activity.this); 
uname1.setText("Hello , "+Sessionname); 
uname1.setGravity(Gravity.LEFT); 
uname1.setTextColor(Color.parseColor("#003366")); 
uname1.setTextSize(20); 
uname1.setLayoutParams(l2); 

et1 = new TextView(Main2Activity.this); 
et1.setHeight(100); 
et1.setTextColor(Color.WHITE); 
et1.setHintTextColor(Color.WHITE); 
et1.setGravity(Gravity.CENTER); 
et1.setHint("Select Date"); 
et1.setBackgroundDrawable(gd3); 
et1.setTextSize(15); 
et1.setLayoutParams(l2); 

lView.setOrientation(LinearLayout.HORIZONTAL); 

LinearLayout.LayoutParams l4 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 
l4.gravity=Gravity.CENTER; 
lHorizontalView1.setOrientation(LinearLayout.HORIZONTAL); 
lHorizontalView1.setLayoutParams(l4); 
lHorizontalView1.addView(uname1); 
lHorizontalView1.addView(et1); 
lView.addView(lHorizontalView1); 

你硬编码在像素你的价值观。例如,对于您的身高,您可以拨打setHeight(100)即表示将此View设置为100px高。但是,不同的屏幕密度看起来完全不同。给定一个固定的屏幕尺寸(比如说5英寸),一个设备的分辨率可以是480x800,另一个可以是​​,你的设置100px看起来就是你现在的样子。

使用Display Metrics代替检索屏幕密度和作为单元使用device independent pixelsdipdp)的像素(px)来代替。例如,您可以设置基于设备的这样的密度您的身高:

setHeight(100 * getResources().getDisplayMetrics().density);

然而,您所需的高度可能要小,因为这将成为100dp所以你可能将它设置的东西比如50个。玩耍,直到你得到你想要的结果。

那么你的View将跨越设备看起来统一。