18

我的 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.LayoutParamsFrameLayout.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);

当我这样做时,我没有收到错误,但它没有设置任何边距

我应该忽略错误吗?只是看起来不太对。

编辑:

这是我CardView的XML:

<?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>
4

3 回答 3

43

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

ViewGroup.MarginLayoutParams layoutParams =
        (ViewGroup.MarginLayoutParams) myCardView.getLayoutParams();
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
myCardView.requestLayout();
于 2017-05-28T04:22:43.307 回答
7

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 边距的参数

ViewGroup.MarginLayoutParams cardViewMarginParams = (ViewGroup.MarginLayoutParams) cardView.getLayoutParams(); 
cardViewMarginParams.setMargins(0, 30, 0, 30);
cardView.requestLayout();  //Dont forget this line
于 2018-05-26T12:52:31.463 回答
0
  1. 设置Linearlayoutid 你的CardView孩子
  2. linearlayout从中查找findViewById
  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);
于 2017-05-28T04:04:12.777 回答