public void makeCard( Context context, View view, String time, String startpt1, String startpt2, String endpt1, String endpt2)
{
CardView card = new CardView(context);
LinearLayout ll = view.findViewById(R.id.rides);
// Set the CardView layoutParams
int w = getResources().getDimensionPixelSize(R.dimen._270sdp);
int h = getResources().getDimensionPixelSize(R.dimen._120sdp);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(w, h);
card.setForegroundGravity(17);
card.setLayoutParams(params);
card.setRadius(60);
card.setCardElevation(60);
Typeface latoregular = getResources().getFont(R.font.latoregular);
Typeface latobold = getResources().getFont(R.font.latobold);
// Initialize a new TextView to put in CardView
TextView tv = new TextView(context);
tv.setLayoutParams(params);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
try {
Date date = format.parse(time);
tv.setText(date.toString());
Log.d("ASD", String.valueOf(date));
} catch (ParseException e) {
e.printStackTrace();
}
tv.setTextSize(14);
tv.setPadding(60,40,0,0);
tv.setTextColor(Color.parseColor("#1fb286"));
tv.setTypeface(latobold);
tv.setTypeface(tv.getTypeface(), Typeface.BOLD);
card.addView(tv);
ll.addView(card);
}
此Cardview紧贴侧面,无论我如何更改布局重力,该卡都不会移动。我可以在左侧设置填充以使其看起来居中,但在不同尺寸的屏幕上,它可能无法完美居中
答案 0 :(得分:1)
您正在创建RelativeLayout.LayoutParams
,并将它们用于LinearLayout
和CardView
(这是FrameLayout
的一种)。这不太可能很好地工作。据我所知,应该使用例如LinearLayout.LayoutParams
代表您要添加到View
的{{1}}(经验法则:使用以父LinearLayout
命名的LayoutParams
)
要使ViewGroup
居中,必须以编程方式设置其CardView
属性:
android:layout_gravity