以编程方式对齐 Android 元素

Posted

技术标签:

【中文标题】以编程方式对齐 Android 元素【英文标题】:Programmatically Align Android Elements 【发布时间】:2017-06-05 08:36:41 【问题描述】:

我使用数据库值以编程方式创建 android 视图。但我不知道如何对齐元素。我正在创建 LinearLayout 但它没有正确对齐。 我的实际输出是

预期输出

我的代码

lView = new LinearLayout(Main2Activity.this);
// lView.setPadding(0,150,0,0);
lView.setBackgroundColor(Color.parseColor("#FFFFFF"));
lView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams l2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
l2.gravity=Gravity.CENTER;

GradientDrawable gd3 = new GradientDrawable();
gd3.setCornerRadius(30);
gd3.setColor(Color.parseColor("#003366"));
gd3.setStroke(0, 0xFF000000);
et1.setGravity(Gravity.CENTER);
et1.setHint("Select Date");
et1.setBackgroundDrawable(gd3);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(et1);

GradientDrawable gd4 = new GradientDrawable();
gd4.setCornerRadius(30);
gd4.setColor(Color.parseColor("#5CB85C"));
gd4.setStroke(3, 0xFFFFFFFF);
 Intime = new TextView(Main2Activity.this);
Intime.setHint("Select In Time");
Intime.setTextColor(Color.WHITE);
Intime.setHintTextColor(Color.WHITE);
Intime.setTextSize(20);
Intime.setHeight(150);
Intime.setWidth(600);
Intime.setGravity(Gravity.CENTER);
Intime.setLayoutParams(l2);
Intime.setBackgroundDrawable(gd4);
lView.setOrientation(LinearLayout.HORIZONTAL);
lView.addView(Intime);

Outtime = new TextView(Main2Activity.this);
Outtime.setHint("Select Out Time");
Outtime.setTextSize(20);
Outtime.setHeight(150);
Outtime.setWidth(600);
Outtime.setGravity(Gravity.CENTER);
Outtime.setTextColor(Color.WHITE);
Outtime.setHintTextColor(Color.WHITE);
Outtime.setLayoutParams(l2);
Outtime.setBackgroundDrawable(gd4);
lView.setOrientation(LinearLayout.HORIZONTAL);
lView.addView(Outtime);

【问题讨论】:

您应该使用RelativeLayout 或嵌套的LinearLayout。如果您使用嵌套的线性布局,则顶部布局必须是垂直方向的,而内部布局必须是水平方向的。 【参考方案1】:

你必须在 xml 中做对齐 Sample this code output

https://i.stack.imgur.com/Kln1g.png

 <LinearLayout 
android:id="@+id/linearLayout4"
android:layout_
android:layout_
android:layout_below="@+id/linearLayout3"
android:layout_centerHorizontal="true"
android:orientation="horizontal" 
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_marginTop="20dp">
<Button
    android:id="@+id/button1"
    android:layout_
    android:layout_
    android:text="Button" />
<Button
    android:id="@+id/button2"
    android:layout_
    android:layout_
    android:text="Button" /></LinearLayout>

【讨论】:

我正在以编程方式创建元素。我没有使用 xml 文件。我不能使用 xml,因为页面是动态的。【参考方案2】:
lView = new LinearLayout(Main2Activity.this);
// lView.setPadding(0,150,0,0);
lView.setBackgroundColor(Color.parseColor("#FFFFFF"));
lView.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT)
    );
LinearLayout.LayoutParams l2 = new LinearLayout.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT
    );
l2.gravity=Gravity.CENTER;

GradientDrawable gd3 = new GradientDrawable();
gd3.setCornerRadius(30);
gd3.setColor(Color.parseColor("#003366"));
gd3.setStroke(0, 0xFF000000);
et1.setGravity(Gravity.CENTER);
et1.setHint("Select Date");
et1.setBackgroundDrawable(gd3);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(et1);

GradientDrawable gd4 = new GradientDrawable();
gd4.setCornerRadius(30);
gd4.setColor(Color.parseColor("#5CB85C"));
gd4.setStroke(3, 0xFFFFFFFF);
Intime = new TextView(Main2Activity.this);
Intime.setHint("Select In Time");
Intime.setTextColor(Color.WHITE);
Intime.setHintTextColor(Color.WHITE);
Intime.setTextSize(20);
Intime.setHeight(150);
Intime.setWidth(600);
Intime.setGravity(Gravity.CENTER);
Intime.setLayoutParams(l2);
Intime.setBackgroundDrawable(gd4);

// lView.addView(Intime);

Outtime = new TextView(Main2Activity.this);
Outtime.setHint("Select Out Time");
Outtime.setTextSize(20);
Outtime.setHeight(150);
Outtime.setWidth(600);
Outtime.setGravity(Gravity.CENTER);
Outtime.setTextColor(Color.WHITE);
Outtime.setHintTextColor(Color.WHITE);
Outtime.setLayoutParams(l2);
Outtime.setBackgroundDrawable(gd4);
lView.setOrientation(LinearLayout.HORIZONTAL);
// lView.addView(Outtime);
    
LinearLayout lHorizontalView=new LinearLayout(Main2Activity.this);

LinearLayout.LayoutParams l3 = new LinearLayout.LayoutParams(
    ViewGroup.LayoutParams.MATCH_PARENT, 
    ViewGroup.LayoutParams.WRAP_CONTENT
    );
l3.gravity=Gravity.CENTER; 

lHorizontalView.setOrientation(LinearLayout.HORIZONTAL);
lHorizontalView.setLayoutParams(l3);
lHorizontalView.addView(Intime);
lHorizontalView.addView(Outtime);

lView.addView(lHorizontalView);

【讨论】:

你好,左对齐,根据屏幕大小不同,有什么解决办法吗? 可以在LinearLayout.LayoutParams中指定权重属性 你能分享你的输出吗? 我认识的朋友。我刚刚更改了 LinearLayout.LayoutParams.WRAP_CONTENT

以上是关于以编程方式对齐 Android 元素的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式Android的TextView

android - 获取以编程方式创建的视图的估计高度

android - 以编程方式将 ID 设置为视图

多个ListViews在android中以编程方式创建

Android:将元素放在另一个元素下方并在屏幕底部对齐

Android 以编程方式设计布局