我正在开发Android应用程序,但基于屏幕尺寸元素大小变化。有任何解决方案可以平均划分屏幕尺寸。我在不同的屏幕上获得不同的尺寸,如5英寸和3.7英寸
我的代码是......
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 gd = new GradientDrawable();
gd.setColor(Color.parseColor("#FFFFFF")); // Changes this drawbale to use a single color instead of a gradient
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
int Height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
TextView uname1 = new TextView(Main2Activity.this);
uname1.setText("Hello , " + Sessionname);
uname1.setGravity(Gravity.CENTER);
uname1.setTextColor(Color.parseColor("#003366"));
uname1.setTextSize(20);
uname1.setWidth(width);
uname1.setLayoutParams(l2);
et1 = new TextView(Main2Activity.this);
et1.setHeight(Height);
et1.setWidth(width);
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);
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);
GradientDrawable gd4 = new GradientDrawable();
gd4.setCornerRadius(30);
gd4.setColor(Color.parseColor("#5CB85C"));
gd4.setStroke(3, 0xFFFFFFFF);
Intime = new TextView(Main2Activity.this);
Intime.setHint("In Time");
Intime.setTextColor(Color.WHITE);
Intime.setHintTextColor(Color.WHITE);
Intime.setTextSize(15);
Intime.setHeight(Height);
Intime.setWidth(width);
Intime.setGravity(Gravity.CENTER);
Intime.setLayoutParams(l2);
Intime.setBackgroundDrawable(gd4);
lView.setOrientation(LinearLayout.HORIZONTAL);
Outtime = new TextView(Main2Activity.this);
Outtime.setHint("Out Time");
Outtime.setTextSize(15);
Outtime.setHeight(Height);
Outtime.setWidth(width);
Outtime.setGravity(Gravity.CENTER);
Outtime.setTextColor(Color.WHITE);
Outtime.setHintTextColor(Color.WHITE);
Outtime.setLayoutParams(l2);
Outtime.setBackgroundDrawable(gd4);
lView.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout lHorizontalView = new LinearLayout(Main2Activity.this);
LinearLayout.LayoutParams l3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
l3.gravity = Gravity.CENTER;
lHorizontalView.setOrientation(LinearLayout.HORIZONTAL);
lHorizontalView.setLayoutParams(l3);
lHorizontalView.addView(Intime);
lHorizontalView.addView(Outtime);
lView.addView(lHorizontalView);
答案 0 :(得分:3)
您应该使用 DisplayMetrics
print "at rows $i and $j"
示例强>
DisplayMetrics metrics = getResources().getDisplayMetrics();
int DeviceTotalWidth = metrics.widthPixels;
int DeviceTotalHeight = metrics.heightPixels;
答案 1 :(得分:0)
对于按钮父视图,请使用linearlayout并设置
param.weight = 1
。
答案 2 :(得分:0)
使用LinearLayouts并为其中的视图指定权重将使用相同的比例分隔视图,无论屏幕大小如何。在这里,我已经为两个视图分配了权重1,因此它们将在我的旁边显示在屏幕上。
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
android:textSize="16sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2"
android:textSize="16sp" />
</LinearLayout>
答案 3 :(得分:0)
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
textView1.setLayoutParams(layoutParams);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
textView2.setLayoutParams(layoutParams1);
我采用线性布局,参数权重为1并将其除以两个视图。在这里我使用了文本视图你可以在你的代码中查看任何视图。如果遇到任何问题,请告诉我