首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Android View在没有XML的情况下对齐底部(以编程方式)

在没有XML的情况下,您可以使用以下方法在Android中以编程方式对齐View底部:

  1. 使用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);
  2. 使用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底部。您可以根据您的需求选择合适的方法。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

领券