如何解决将按钮对齐到 CardView 的底部
所以我在 for 循环中创建了一些 CardView,我想在每个 CardView 的底部添加一个 Button。
我像这样设置 CardView 的 LayoutParams
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,height
);
cardview.setLayoutParams(params4);
并为按钮创建一组新的参数
RelativeLayout.LayoutParams params20 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT
);
像这样添加对齐到底部的规则,然后将这些参数设置为按钮的 LayoutParams
params20.addRule(RelativeLayout.ALIGN_PARENT_BottOM);
search.setLayoutParams(params20);
最后我将 Button 添加到 CardView
cardView.addView(search);
但是这样做按钮总是保持在顶部。当记录 CardViews 和 Button 的父级时,父级是当前 CardView
使用 2 个 CardView 登录:
2021-01-27 10:15:45.843 7764-7764/lenno.plugin.smartplaner E/deine: cardview: androidx.cardview.widget.CardView{d12f026 VFE...CL. ......I. 0,0-0,0 #1}........id: 1
2021-01-27 10:15:45.843 7764-7764/lenno.plugin.smartplaner E/deine1: parent: androidx.cardview.widget.CardView{d12f026 VFE...CL. ......I. 0,0 #1}
2021-01-27 10:15:45.855 7764-7764/lenno.plugin.smartplaner E/deine: cardview: androidx.cardview.widget.CardView{ee5dc0a VFE...CL. ......I. 0,0 #2}........id: 2
2021-01-27 10:15:45.856 7764-7764/lenno.plugin.smartplaner E/deine1: parent: androidx.cardview.widget.CardView{ee5dc0a VFE...CL. ......I. 0,0 #2}
我已经尝试过像这样将 params20 的锚点显式设置为父 CardView
params20.addRule(RelativeLayout.ALIGN_PARENT_BottOM,cardView.getId());
也没有用。也许我在一般情况下做错了什么(我刚刚开始以编程方式创建布局)但我目前不知道该怎么做。
提前感谢您的回答!
解决方法
这里有一个例子。阅读评论以获取信息。
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
//CardView
CardView cardview = new CardView(this);
cardView.setId(1);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,1000);
cardview.setLayoutParams(params4);
//Button
Button btn1 = new Button(this);
btn1.setText("Button at the bottom");
btn1.setId(2); //Only needed you wanna put another view close to this view. See below.
RelativeLayout.LayoutParams params20 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params20.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,1 /* ID of cardView */); //This will cause this view (button) to be positioned at the bottom of parent view.
//Add the Button to the CardView. Make sure to include the layout params too.
cardview.addView(btn1,params20);
//If you wanna add another view above btn1,here's how.
Button btn2 = new Button(this);
btn2.setText("I'm above button one");
RelativeLayout.LayoutParams params21 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params21.addRule(RelativeLayout.ABOVE,2 /* ID of btn1 */);
//Add btn2 to the CardView.
cardview.addView(btn2,params21);
//Main layout
setContentView(cardview);
}
}
,
我仍然不知道最初的问题是什么,但我通过对 Button 使用 ConstraintLayout 并将 topMargin 设置为合适的值来修复它。
ConstraintLayout.LayoutParams params20 = new ConstraintLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params20.topMargin = 850;
search.setLayoutParams(params20);
cardView.addView(search);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。