首页 > 解决方案 > 对齐按钮到 CardView 的底部

问题描述

所以我在 for 循环中创建了一些 CardView,我想在每个 CardView 的底部添加一个按钮。

我正在像这样设置 CardView 的 LayoutParams

RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    height
            );
cardview.setLayoutParams(params4);

并为 Button 创建一组新参数

RelativeLayout.LayoutParams params20 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            );

像这样添加 align to bottom 规则,然后将这些参数设置为 Button 的 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-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-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-0,0 #2}

我已经尝试像这样将 params20 的锚点显式设置为父 CardView

params20.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, cardView.getId());

这也不起作用。也许我通常做错了什么(我刚开始以编程方式创建布局)但我目前不知道该怎么做。

提前感谢您的回答!

标签: androidandroid-relativelayoutlayoutparams

解决方案


好吧,这里举个例子。阅读评论以获取信息。

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);
    }
}

推荐阅读