[英]How to divide screen two equal part vertically in ConstraintLayout in android
[英]how can we divide screen in two equal width in android programatically
我正在開發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);
您應該使用DisplayMetrics
DisplayMetrics metrics = getResources().getDisplayMetrics();
int DeviceTotalWidth = metrics.widthPixels;
int DeviceTotalHeight = metrics.heightPixels;
例
RelativeLayout rl_firstObj=(RelativeLayout)findViewById(R.id.rl_first);
rl_firstObj.getLayoutParams().width=DeviceTotalWidth*50/100;
RelativeLayout rl_secondObj=(RelativeLayout)findViewById(R.id.rl_second);
rl_secondObj.getLayoutParams().width=DeviceTotalWidth*50/100;
對於按鈕父視圖,使用linearlayout和set
param.weight = 1
對於每個按鈕。
使用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>
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,並將其划分為兩個視圖。在這里我使用了文本視圖你可以在你的代碼中查看任何視圖。如果遇到任何問題請告訴我
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.