setPadding()在动态创建CardView时不起作用

时间:2017-12-31 09:32:02

标签: android android-cardview

setPadding在我的代码中不起作用。看来所有卡都连接在一起,未应用填充

card = new CardView(mContext);

            // Set the CardView layoutParams
LayoutParams params = new LayoutParams(
    LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT
    );
card.setLayoutParams(params);

            //Set CardView Padding
card.setPadding(15,15,15,15);

            // Set CardView corner radius
card.setRadius(8);

            // Set cardView content padding
card.setContentPadding(15, 15, 15, 15);

2 个答案:

答案 0 :(得分:1)

试试这个你可以使用Set 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(10);

                // Set cardView content padding
                card.setContentPadding(15, 15, 15, 15);


                // Set the CardView maximum elevation
                card.setMaxCardElevation(15);

                // Set CardView elevation
                card.setCardElevation(10);

修改

LayoutParams params = new LayoutParams(
    LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT
);
params.setMargins(10, 10, 10,10);

答案 1 :(得分:1)

使用card.setContentPadding(10, 10, 10, 10); 而不是card.setPadding(15,15,15,15);