以编程方式设置CardView的边距

19

我在我的XML布局中有一个CardView,我需要通过编程的方式设置它的边距(因为我并不总是需要边距。根据一些条件,我要么设置或删除边距)。

这是我的做法:

CardView.LayoutParams layoutParams = (CardView.LayoutParams) myCardView.getLayoutParams();
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
myCardView.setLayoutParams(layoutParams);

这个很好用。它在我的CardView底部加上了2dp的边距。但是,在我的日志中,我得到了以下信息:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams

所以我将 CardView.LayoutParams 改成了 FrameLayout.LayoutParams,像这样:

FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) myCardView.getLayoutParams();
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
myCardView.setLayoutParams(layoutParams);

然而,它再次起作用了,但我收到了与上述相同的错误。

所以我再次修改它,使用 LinearLayout.LayoutParams

LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) myCardView.getLayoutParams();
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
myCardView.setLayoutParams(layoutParams);
当我这样做时,我没有收到错误信息,但是它没有设置任何边距。我应该忽略这个错误吗?这似乎不太对。编辑:这是我的 XML 中的 CardView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:background="@android:color/white"
    android:minHeight="@dimen/item_min_height"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/cardViewAppointmentWeek"
        app:cardElevation="2dp"
        android:layout_marginBottom="2dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="horizontal"
            android:background="?android:attr/selectableItemBackground"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            ...

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

你能给我展示一下你 XML 代码中的卡片视图吗? - redAllocator
@red_allocator 我已经更新了我的问题,展示了XML中的CardView - ᴛʜᴇᴘᴀᴛᴇʟ
你的CardView的直接父视图是什么? - azizbekian
@azizbekian LinearLayout。我已经更新了我的问题,这样你就可以看到我的布局。 - ᴛʜᴇᴘᴀᴛᴇʟ
4个回答

46

在您的情况下,您并不特别关心您的CardView的父级是谁,因为您想要更改的唯一内容是边距。所有的*LayoutParams类都是MarginLayoutParams的直接/间接子类,这意味着您可以轻松地将其强制转换为MarginLayoutParams并对该对象进行更改:

ViewGroup.MarginLayoutParams layoutParams =
        (ViewGroup.MarginLayoutParams) myCardView.getLayoutParams();
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
myCardView.requestLayout();

这并不适用于ViewGroup#LayoutParams的子类*LayoutParams类。 - B W
1
谢谢你提供的解决方案,省了我很多时间! - hetsgandhi

9

1)设置CardView参数,使其可以在程序中显示

设置CardView参数

CardView cardView = new CardView(context);
LinearLayout.LayoutParams cardViewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);  
cardView.setLayoutParams(cardViewParams)

2) 设置卡片视图周围的边距参数

设置卡片视图边距的参数

ViewGroup.MarginLayoutParams cardViewMarginParams = (ViewGroup.MarginLayoutParams) cardView.getLayoutParams(); 
cardViewMarginParams.setMargins(0, 30, 0, 30);
cardView.requestLayout();  //Dont forget this line

0

Kotlin 在 Kotlin 中通过代码设置布局边距有些棘手。

val cardView = CardView(context)
(cardView.layoutParams as? ViewGroup.MarginLayoutParams)?.leftMargin = 2
(cardView.layoutParams as? ViewGroup.MarginLayoutParams)?.bottomMargin = 2
cardView.requestLayout()

0
  1. Linearlayout 设置为你的 CardView 的子元素
  2. 通过 findViewById 查找 linearlayout
  3. 设置 LayoutParams
LinearLayout linearLayout = (LinearLayout)myCardView.findViewById(R.id.linearlayoutid);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);

1
我不想设置填充。我想设置边距。这是完全不同的。 - ᴛʜᴇᴘᴀᴛᴇʟ
你的意思是LinearLayout必须移动以适应margin吗? - redAllocator
不,我不想对我的LinearLayout做任何操作。请阅读我的原始问题。我想以编程方式在我的CardView上设置2dp的边距。 - ᴛʜᴇᴘᴀᴛᴇʟ
你正在为 LinearLayout 设置边距,但我需要在 CardView 上设置它,因为 CardView 有一个名为 cardElevation 的字段。如果将 cardElevation 设置为 2dp,则阴影直到底部有 2dp 边距才可见。因此,在 LinearLayout 上设置边距对我没有帮助。 - ᴛʜᴇᴘᴀᴛᴇʟ
你是认真的吗?Padding 在视图的宽度和高度内计算并应用于内部。Margin 在视图的宽度和高度之外计算并应用于外部。在尝试回答问题之前,请确保理解问题,并告诉别人理解某件事情。 - ᴛʜᴇᴘᴀᴛᴇʟ
CardView的父布局是LinearLayout 设置父布局LinearLayout的padding - redAllocator

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接