以编程方式将边距设置为 CardView

Setting margin programmatically to CardView

我的 XML 布局中有一个 CardView,我需要以编程方式为其设置边距(这是因为我不需要任何时候都需要边距。基于一些条件,我设置或删除边距)。

我是这样做的:

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

这很好用。它把 2dp 边距放在我的 CardView 底部。但是,我在 logcat:

中得到了这个
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>
  1. 设置 Linearlayout id 你的 CardView child
  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);

在您的情况下,您 特别感兴趣谁是您 CardView 的父级,因为您唯一想要更改的是边距。所有 *LayoutParams 类 都是 MarginLayoutParams 的 direct/indirect 个子对象,这意味着您可以轻松转换为 MarginLayoutParams 并对该对象执行更改:

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

1) 设置 Cardview 参数以使其以编程方式显示

设置车视参数

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

2) 设置cardview周围边距的显示参数

设置 Cardview 边距的参数

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