【问题标题】:Aligning Button to the bottom of CardView对齐按钮到 CardView 的底部
【发布时间】:2021-05-01 03:29:37
【问题描述】:

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

我正在像这样设置 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
            );

像这样添加 align to bottom 规则,然后将这些参数设置为 Button 的 LayoutParams

params20.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
search.setLayoutParams(params20);

最后我将按钮添加到 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());

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

提前感谢您的回答!

【问题讨论】:

    标签: android android-relativelayout layoutparams


    【解决方案1】:

    我仍然不知道最初的问题是什么,但我通过对 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);
    

    【讨论】:

      【解决方案2】:

      这里有一个例子。阅读 cmets 获取信息。

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

      【讨论】:

      • 首先感谢您的回答!刚刚尝试过(甚至不知道您可以在 addView 方法中包含参数,所以已经学到了一些新东西:D),但不知何故,按钮仍然位于顶部。
      • 它在我的手机上运行良好。真的需要用CardView吗,也可以用RelativeLayout
      • 我在 CardViews 上显示了一些数据,并希望为用户提供单独删除每个数据的选项。如果它适用于您的手机,那么对我来说问题可能是我拥有的其他一些布局。我将 CardView 添加到另一个包含在 ScrollView 中的 RelativeLayout。现在会调查一下
      • 好的,我通过对按钮使用 ConstraintLayout 将 topMargin 设置为合适的值而不是使用 RelativeLayout 来修复它。很奇怪的bug。无论如何,再次感谢您的帮助!
      猜你喜欢
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 2021-06-10
      相关资源
      最近更新 更多