以编程方式将余量设置为CardView [英] Setting margin programmatically to CardView

查看:87
本文介绍了以编程方式将余量设置为CardView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML布局中有一个CardView,我需要以编程方式为其设置边距(这是因为我一直都不需要边距.在少数情况下,我会设置或删除边距).

I have a CardView in my XML layout and I need to set margins to it programmatically (This is because I don't need margin at all times. Based on few conditions, I either set or remove the margin).

这是我的做法:

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

这很好.它在我的CardView的底部放置了2dp边距.但是,我在logcat中得到了这个:

This worked fine. It put 2dp margin at the bottom of my CardView. However, I get this in my logcat:

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

所以我将CardView.LayoutParams更改为FrameLayout.LayoutParams,如下所示:

So I changed CardView.LayoutParams to FrameLayout.LayoutParams, like this:

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

再一次,它可以工作,但是我得到与上面相同的错误.

And yet again, it works but I get the same error as above.

所以我又修改了一次以使用LinearLayout.LayoutParams.

So I modified it one more time to use LinearLayout.LayoutParams.

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

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

When I do this, I do NOT get the error however it does not set any margin.

我应该忽略该错误吗?只是看起来不正确.

Should I just ignore the error? It just doesn't seem right.

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

推荐答案

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

In your case you are not specifically interested who is the parent of your CardView, because the only thing you want to change is the margin. All of the *LayoutParams classes are direct/indirect children of MarginLayoutParams, which means you can easily cast to MarginLayoutParams and perform change on that object:




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


这篇关于以编程方式将余量设置为CardView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆