将 Button 对齐到 CardView 的底部
Aligning Button to the bottom of CardView
所以我在 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
);
像这样添加底部对齐规则,然后将这些参数设置为按钮的 LayoutParams
params20.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
search.setLayoutParams(params20);
最后我将按钮添加到 CardView
cardView.addView(search);
但是这样做 Button 总是停留在顶部。当记录 CardViews 和 Button 的父级时,父级是预期的当前 CardView
使用 2 个 CardViews 登录:
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);
}
}
我仍然不知道最初的问题是什么,但我通过为 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);
所以我在 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
);
像这样添加底部对齐规则,然后将这些参数设置为按钮的 LayoutParams
params20.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
search.setLayoutParams(params20);
最后我将按钮添加到 CardView
cardView.addView(search);
但是这样做 Button 总是停留在顶部。当记录 CardViews 和 Button 的父级时,父级是预期的当前 CardView
使用 2 个 CardViews 登录:
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);
}
}
我仍然不知道最初的问题是什么,但我通过为 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);