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

ᴛʜᴇ*_*ᴛᴇʟ 12 android android-layout layoutparams android-cardview android-layoutparams

我有一个CardViewXML布局,我需要以编程方式设置边距(这是因为我不需要任何时间的边距.基于几个条件,我设置或删除边距).

我是这样做的:

CardView.LayoutParams layoutParams = (CardView.LayoutParams) myCardView.getLayoutParams();
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
myCardView.setLayoutParams(layoutParams);
Run Code Online (Sandbox Code Playgroud)

这很好.它把2dp保证金放在我的底部CardView.但是,我在logcat中得到了这个:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
Run Code Online (Sandbox Code Playgroud)

所以我改成CardView.LayoutParamsFrameLayout.LayoutParams,像这样:

FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) myCardView.getLayoutParams();
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
myCardView.setLayoutParams(layoutParams);
Run Code Online (Sandbox Code Playgroud)

然而,它仍然有效,但我得到与上面相同的错误.

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

LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) myCardView.getLayoutParams();
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
myCardView.setLayoutParams(layoutParams);
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我没有得到错误,但它没有设置任何余量.

我应该忽略错误吗?这似乎不对.

编辑:

这是我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>
Run Code Online (Sandbox Code Playgroud)

azi*_*ian 30

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



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



fri*_*ker 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)
Run Code Online (Sandbox Code Playgroud)

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

设置 Cardview 边距的参数

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