以编程方式将余量设置为CardView

时间:2017-05-28 03:15:37

标签: android android-layout android-cardview layoutparams android-layoutparams

我的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>

3 个答案:

答案 0 :(得分:26)

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



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


答案 1 :(得分:2)

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

设置Carview参数

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

答案 2 :(得分:0)

  1. 将LinearlayoutId设为您的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);