以编程方式将视图添加到 RelativeLayout 内的 CardView
Adding Views programatically to a CardView that is inside a RelativeLayout
我需要为 RelativeLayout 创建 CardView。在 CardViews 内部,还有其他视图,如 ImageViews 和 TextViews。它们已添加,但似乎不遵守我添加到 LayoutParams 的规则。这是我的代码:
CardView infoCardView=new CardView(HotelActivity.this);
infoCardView.setId(2);
RelativeLayout.LayoutParams cardViewParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams iconLp=new RelativeLayout.LayoutParams(20,20);
RelativeLayout.LayoutParams textLP=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//imagen is another ImageView that is being drawn ok in the RelativeLayout. Also, the cardView is drawn below the ImageView, which is ok.
cardViewParams.addRule(RelativeLayout.BELOW,imagen.getId());
infoCardView.setLayoutParams(cardViewParams);
iconLp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
iconLp.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
iconLp.setMarginStart(20);
//Icono del cardView
ImageView icono=new ImageView(HotelActivity.this);
icono.setId(3);
//icono.setLayoutParams(iconLp);
icono.setBackground(getDrawable(android.R.drawable.ic_dialog_info));
infoCardView.addView(icono,iconLp);
//TextView título del CardView
TextView tvInfo=new TextView(HotelActivity.this);
tvInfo.setId(4);
tvInfo.setTextSize(17);
tvInfo.setText(getString(R.string.info));
tvInfo.setTypeface(tvInfo.getTypeface(), Typeface.BOLD);
textLP.addRule(RelativeLayout.END_OF,icono.getId());
textLP.setMarginStart(20);
tvInfo.setTextColor(getColor(R.color.white));
//tvInfo is NOT added to the END of icono, instead is overlapping it.
infoCardView.addView(tvInfo, textLP);
infoCardView.setRadius(9);
infoCardView.setBackgroundColor(Color.parseColor(almacen.getEvento().getBgColor()));
infoCardView.setElevation(9);
rl.addView(infoCardView,cardViewParams);
我做错了什么?
谢谢。
已解决。问题是,我正在为 CardView 设置一些对其不可用的规则,例如 CENTER_VERTICAL。所以,我添加了另一个 RelativeLayout 作为 CardView 的子级,将视图添加到那个 RelativeLayout,将那个 RelativeLayout 添加到 CardView,现在视图绘制正确了。
我需要为 RelativeLayout 创建 CardView。在 CardViews 内部,还有其他视图,如 ImageViews 和 TextViews。它们已添加,但似乎不遵守我添加到 LayoutParams 的规则。这是我的代码:
CardView infoCardView=new CardView(HotelActivity.this);
infoCardView.setId(2);
RelativeLayout.LayoutParams cardViewParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams iconLp=new RelativeLayout.LayoutParams(20,20);
RelativeLayout.LayoutParams textLP=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//imagen is another ImageView that is being drawn ok in the RelativeLayout. Also, the cardView is drawn below the ImageView, which is ok.
cardViewParams.addRule(RelativeLayout.BELOW,imagen.getId());
infoCardView.setLayoutParams(cardViewParams);
iconLp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
iconLp.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
iconLp.setMarginStart(20);
//Icono del cardView
ImageView icono=new ImageView(HotelActivity.this);
icono.setId(3);
//icono.setLayoutParams(iconLp);
icono.setBackground(getDrawable(android.R.drawable.ic_dialog_info));
infoCardView.addView(icono,iconLp);
//TextView título del CardView
TextView tvInfo=new TextView(HotelActivity.this);
tvInfo.setId(4);
tvInfo.setTextSize(17);
tvInfo.setText(getString(R.string.info));
tvInfo.setTypeface(tvInfo.getTypeface(), Typeface.BOLD);
textLP.addRule(RelativeLayout.END_OF,icono.getId());
textLP.setMarginStart(20);
tvInfo.setTextColor(getColor(R.color.white));
//tvInfo is NOT added to the END of icono, instead is overlapping it.
infoCardView.addView(tvInfo, textLP);
infoCardView.setRadius(9);
infoCardView.setBackgroundColor(Color.parseColor(almacen.getEvento().getBgColor()));
infoCardView.setElevation(9);
rl.addView(infoCardView,cardViewParams);
我做错了什么?
谢谢。
已解决。问题是,我正在为 CardView 设置一些对其不可用的规则,例如 CENTER_VERTICAL。所以,我添加了另一个 RelativeLayout 作为 CardView 的子级,将视图添加到那个 RelativeLayout,将那个 RelativeLayout 添加到 CardView,现在视图绘制正确了。