android - 对齐按钮到 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());
这也不起作用。也许我通常做错了什么(我刚开始以编程方式创建布局)但我目前不知道该怎么做。
提前感谢您的回答!
解决方案
好吧,这里举个例子。阅读评论以获取信息。
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);
}
}
推荐阅读
- r - 如何制作几个组的维恩图
- r - 文本标签在 ggplot 平面图上被剪裁(剪辑 =“关闭”似乎没有帮助)
- python - 即使数据集很小,CNN 也不会过拟合
- sql - 根据确定范围内其他值的顺序查找来修改列的值
- python - 较大的 kmeans 项目中的用户 word2vec 模型输出
- java - spring.main.allow-bean-definition-overriding=true 是一个不好的做法吗
- amazon-web-services - gsutil rsync Google Store 与 AWS S3 400 ExcessHeaderValues
- mongodb - 设置副本集后如何进行 mongodb 增量备份
- c++ - 模拟 bash 输入以在 C++ 中测试一些代码
- java - 在意图搜索我的 EditText 字符串后,如何开始在我的 Recycler 视图中搜索匹配项