【问题标题】:ClassCastException of LayoutParams to MarginLayoutParams after setting a view's width设置视图宽度后 LayoutParams 的 ClassCastException 到 MarginLayoutParams
【发布时间】:2014-06-13 19:05:46
【问题描述】:

我编写了一个小型代理类,以便可以使用 ObjectAnimator 为视图的边缘设置动画。在检查这是否有效并且所有动画都正确之后,我想在动画之前调整视图的大小,但是在我设置宽度之后,我的动画失败并出现 ClassCastException。我不知道为什么。

private class handleClickListener implements View.OnClickListener {
    private static final int LEFT_MARGIN_VISIBLE = 0;
    private static final int LEFT_MARGIN_HIDDEN = -196;

    public void onClick(View view) {
        LinearLayout row = (LinearLayout) view.getParent().getParent();
        RelativeLayout options = (RelativeLayout) row.findViewById(R.id.options);
        LayoutProxy layoutProxy = new LayoutProxy(options);

        // First make sure the options are the right width to fill the screen with the handle
        int rowWidth = row.getWidth();
        int handleWidth = view.getWidth();
        layoutProxy.setWidth(rowWidth - handleWidth);
        ObjectAnimator animation;

        if (layoutProxy.getLeftMargin() == Util.dipsToPixels(options, LEFT_MARGIN_VISIBLE)) {
            animation = ObjectAnimator.ofInt(layoutProxy, "leftMargin", Util.dipsToPixels(view, LEFT_MARGIN_VISIBLE), Util.dipsToPixels(view, LEFT_MARGIN_HIDDEN)); 
        } else {
            animation = ObjectAnimator.ofInt(layoutProxy, "leftMargin", Util.dipsToPixels(view, LEFT_MARGIN_HIDDEN), Util.dipsToPixels(view, LEFT_MARGIN_VISIBLE)); 
        }

        animation.setDuration(200);
        animation.start();
    }

    /**
     * Allows an ObjectAnimator to set/get margins and dimensions of a view
     */
    private class LayoutProxy {
        private ViewGroup mView;

        public LayoutProxy(View view) {
            mView = (ViewGroup) view;
        }

        public int getWidth() {
            ViewGroup.LayoutParams lp = mView.getLayoutParams();
            return lp.width;
        }

        public void setWidth(int width) {
            ViewGroup.LayoutParams lp = mView.getLayoutParams();
            mView.setLayoutParams(new ViewGroup.LayoutParams(width, lp.height));
        }

        public int getHeight() {
            ViewGroup.LayoutParams lp = mView.getLayoutParams();
            return lp.height;
        }

        public void setHeight(int height) {
            ViewGroup.LayoutParams lp = mView.getLayoutParams();
            mView.setLayoutParams(new ViewGroup.LayoutParams(lp.width, height));
        }

        public int getLeftMargin() {
            ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mView.getLayoutParams();
            return lp.leftMargin;
        }

        public void setLeftMargin(int margin) {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            lp.setMargins(margin, lp.topMargin, lp.rightMargin, lp.bottomMargin);
            mView.requestLayout();
        }

        public int getTopMargin() {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            return lp.topMargin;
        }

        public void setTopMargin(int margin) {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            lp.setMargins(lp.leftMargin, margin, lp.rightMargin, lp.bottomMargin);
            mView.requestLayout();
        }

        public int getRightMargin() {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            return lp.rightMargin;
        }

        public void setRightMargin(int margin) {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            lp.setMargins(lp.leftMargin, lp.topMargin, margin, lp.bottomMargin);
            mView.requestLayout();
        }

        public int getBottomMargin() {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            return lp.bottomMargin;
        }

        public void setBottomMargin(int margin) {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, margin);
            mView.requestLayout();
        }
    }
}

还有错误:

FATAL EXCEPTION: main
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams
at com.test.data.ContactListCursorAdapter$handleClickListener$LayoutProxy.getLeftMargin(ContactListCursorAdapter.java:135)
at com.test.data.ContactListCursorAdapter$handleClickListener.onClick(ContactListCursorAdapter.java:94)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)

【问题讨论】:

  • 我也尝试在不使用 layoutProxy 的情况下设置宽度,之后投射仍然失败。
  • 你能告诉我们你导入了哪些 LayoutParams

标签: android-layout


【解决方案1】:

我也有类似的问题

一开始我用的是:

LayoutParams viewFlowLayout = new LayoutParams(originalWidth, newHeight);
viewFlow.setLayoutParams(flowViewLayout);

我的错误信息和你的一样:

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams

我不知道为什么,因为我没有使用 MarginLayoutParams。

那我试试改成这个

LayoutParams viewFlowLayout = viewFlow.getLayoutParams();
viewFlowLayout.height = newheight;
viewFlow.setLayoutParams(flowViewLayout);

然后错误消失了……


我猜您对上述代码有多个用例。 例如:改变这个

public void setHeight(int height) {
    ViewGroup.LayoutParams lp = mView.getLayoutParams();
    mView.setLayoutParams(new ViewGroup.LayoutParams(lp.width, height));
}

public void setHeight(int height) {
    ViewGroup.LayoutParams lp = mView.getLayoutParams();
    lp.height = height  //width will remain and height will be change to the newHeight
    mView.setLayoutParams(lp);
}

希望它对你和很多人有帮助......

【讨论】:

    【解决方案2】:

    我只是想知道您将 LayoutParams 转换为 MarginLayoutParams 的原因。有具体原因吗?因为我认为问题出在那儿。

    【讨论】:

    • LaoutParams 不保存 Margin 信息,只有 MarginLayoutParams。还有其他方法可以访问我错过的边距吗?
    • LayoutParams 确实保存边距信息。您可以像这样访问它: LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); params.leftMargin = //一些整数
    • @STT 这就是你可以设置边距的方式,而不是获取它们:)
    • -1 这应该是一条评论。请学习如何使用 StackOverflow。你真的认为这对社区有帮助吗?来吧...这是基本的。
    猜你喜欢
    • 2012-03-05
    • 2013-07-30
    • 2023-03-17
    • 2012-12-09
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多