在没有XML的情况下,您可以使用以下方法在Android中以编程方式对齐View底部:
RelativeLayout
:RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeLayout.setLayoutParams(layoutParams);
TextView textView = new TextView(this);
textView.setText("Hello World!");
RelativeLayout.LayoutParams textViewLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
textViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textView.setLayoutParams(textViewLayoutParams);
relativeLayout.addView(textView);ConstraintLayout
:ConstraintLayout constraintLayout = new ConstraintLayout(this);
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.MATCH_PARENT
);
constraintLayout.setLayoutParams(layoutParams);
TextView textView = new TextView(this);
textView.setText("Hello World!");
ConstraintLayout.LayoutParams textViewLayoutParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
textViewLayoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
textViewLayoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
textView.setLayoutParams(textViewLayoutParams);
constraintLayout.addView(textView);这两种方法都可以在没有XML的情况下对齐View底部。您可以根据您的需求选择合适的方法。
领取专属 10元无门槛券
手把手带您无忧上云