Android RelativeLayout添加规则不起作用
伙计们,我有一个cardview,我想用rule在上面添加2个按钮,但是addRule()方法不起作用。在图中,图A正在出现,但我想让图B,我的意思是,我想把按钮设置为右对齐,底部对齐,第二个按钮与第一个相邻。当我运行它时,计算一个正在发生的事件。有什么建议吗 试试这个:-Android RelativeLayout添加规则不起作用,android,android-relativelayout,Android,Android Relativelayout,伙计们,我有一个cardview,我想用rule在上面添加2个按钮,但是addRule()方法不起作用。在图中,图A正在出现,但我想让图B,我的意思是,我想把按钮设置为右对齐,底部对齐,第二个按钮与第一个相邻。当我运行它时,计算一个正在发生的事件。有什么建议吗 试试这个:- RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout); CardView cardView = new C
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
CardView cardView = new CardView(this);
ViewGroup.LayoutParams cardParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
cardView.setLayoutParams(cardParams);
mainLayout.addView(cardView);
RelativeLayout relativeLayout = new RelativeLayout(this);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(layoutParams);
cardView.addView(relativeLayout);
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.RIGHT);
relativeLayout.addView(linearLayout);
Button removeButton = new Button(this);
Button modifyButton = new Button(this);
removeButton.setText("Remove");
modifyButton.setText("Modify");
linearLayout.addView(removeButton);
linearLayout.addView(modifyButton);
您是否将这些按钮添加到实际的RelativeLayout?如果您只是将它们添加到支持库中的CardView小部件中,但由于CardView不是RelativeLayout.LayoutParams指的是直接父项,因此无法工作,则您是在CardView中添加按钮。如果可能的话,使用xml,您可以在带有水平方向的线性布局中包含按钮。我正在制作卡片程序。请更新我的代码。以编程方式制作cardview。试试这个。
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
CardView cardView = new CardView(this);
ViewGroup.LayoutParams cardParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
cardView.setLayoutParams(cardParams);
mainLayout.addView(cardView);
RelativeLayout relativeLayout = new RelativeLayout(this);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(layoutParams);
cardView.addView(relativeLayout);
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.RIGHT);
relativeLayout.addView(linearLayout);
Button removeButton = new Button(this);
Button modifyButton = new Button(this);
removeButton.setText("Remove");
modifyButton.setText("Modify");
linearLayout.addView(removeButton);
linearLayout.addView(modifyButton);