如何设置我的视图的LayoutParams宽度? [英] How Do I Set My View LayoutParams Width Smaller?

查看:246
本文介绍了如何设置我的视图的LayoutParams宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我借用 Seth的方法发现的此处动画我viewGroups。它的伟大工程,但在错误的方向 - 这是我第一次使用/扩展动画类和我无法弄清楚如何降低我的看法 - 这其扩展的大小。

下面是类和我如何使用它。任何帮助将大大AP preciated。

 公共类ContainerAnim扩展动画{
    INT targetWidth;
    查看视图。
    布尔开通;    公共ContainerAnim(视图V,诠释targetWidth,布尔开){
        this.view = V;
        this.targetWidth = targetWidth;
        this.opened =开通;
    }    @覆盖
    保护无效applyTransformation(浮动interpolatedTime,变换T){
        INT newWidth;
        如果(打开){
            newWidth =(INT)(targetWidth * interpolatedTime);
        }其他{
            newWidth =(INT)(targetWidth *(1 - interpolatedTime));
        }
        view.getLayoutParams()宽= newWidth。
        view.requestLayout();
    }    @覆盖
    公共无效初始化(INT宽度,高度INT,INT上级宽度,
            INT上级高度){
        super.initialize(宽度,高度,上级宽度,上级高度);
    }    @覆盖
    公共布尔willChangeBounds(){
        返回true;
    }
}

用法:

  ...情况下R.id.menu_shrink:
            FragmentTransaction英尺= getFragmentManager()调用BeginTransaction()。
            的FrameLayout帧=(的FrameLayout)findViewById(R.id.listFragment_container);            ContainerAnim设置=新ContainerAnim(帧,100,真正的);
            set.setDuration(200);
            LayoutAnimationController C =新LayoutAnimationController(套,
                    0.25f);
            frame.setLayoutAnimation(C);
            frame.startLayoutAnimation();            ft.commit();
...


解决方案

我想你混淆了第三个参数(布尔)的使用。你把它叫做打开,并把它传递真实的,因为你要缩水。然而,这是使用在我原来的code相反!在我的code,布尔是真实的,如果有观点的关闭,但我想它为增长

和D =一个布尔值,用于指定方向(真=扩张,假=崩溃)。

如果你交换的条件在动画类,如果(!开){},它应该工作。或者,通过它,而不是虚假的真实。或者,它传递真实的,但改变变量设置为关闭,而不是打开。

-Seth

Hi I am borrowing Seth's method found here to animate my viewGroups. It works great but in the wrong direction - This is my first time using/extending the Animation class and I cant figure how to decrease the size of my view -This expands it.

Below is the class and how I use it. Any help will be greatly appreciated.

public class ContainerAnim extends Animation {
    int targetWidth;
    View view;
    boolean opened;

    public ContainerAnim(View v, int targetWidth, boolean opened) {
        this.view = v;
        this.targetWidth = targetWidth;
        this.opened = opened;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        int newWidth;
        if (opened) {
            newWidth = (int) (targetWidth * interpolatedTime);
        } else {
            newWidth = (int) (targetWidth * (1 - interpolatedTime));
        }
        view.getLayoutParams().width = newWidth;
        view.requestLayout();
    }

    @Override
    public void initialize(int width, int height, int parentWidth,
            int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

Usage:

... case R.id.menu_shrink:
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            FrameLayout frame = (FrameLayout) findViewById(R.id.listFragment_container);

            ContainerAnim set = new ContainerAnim(frame, 100, true);
            set.setDuration(200);
            LayoutAnimationController c = new LayoutAnimationController(set,
                    0.25f);
            frame.setLayoutAnimation(c);
            frame.startLayoutAnimation();

            ft.commit();
...

解决方案

I think you've mixed up the usage of that third parameter (the boolean). You call it "opened", and pass it true, because you want to shrink it. However, this is the opposite of the usage in my original code! In my code, the boolean was true if the view was closed but I wanted it to grow.

"and d = a boolean which specifies the direction (true = expanding, false = collapsing)."

If you swap the condition in the animation class to if(!opened){}, it should work. Or pass it false instead of true. Or pass it true, but change the variable to "closed" instead of "opened".

-Seth

这篇关于如何设置我的视图的LayoutParams宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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