您好我正在尝试以编程方式创建一个CardView,以及另一个活动,
所以我有我的DrinkActivity,当我点击FAB时,我希望CardView出现在我的摘要活动中。我有我的代码,但我正在返回null尝试从另一个活动中引用id。
// Initialize a new CardView
CardView card = new CardView(mContext);
// Set the CardView layoutParams
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
card.setLayoutParams(params);
// Set CardView corner radius
card.setRadius(9);
// Set cardView content padding
card.setContentPadding(15, 15, 15, 15);
// Set a background color for CardView
card.setCardBackgroundColor(Color.parseColor("#FFC6D6C3"));
// Set the CardView maximum elevation
card.setMaxCardElevation(15);
// Set CardView elevation
card.setCardElevation(9);
// Initialize a new TextView to put in CardView
TextView tv = new TextView(mContext);
tv.setLayoutParams(params);
tv.setText("CardView\nProgrammatically");
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
tv.setTextColor(Color.RED);
// Put the TextView in CardView
card.addView(tv);
// Finally, add the CardView in root layout
mRelativeLayout.addView(card);
}
任何帮助将不胜感激!谢谢!