如何以编程方式调整自定义视图的大小?

问题描述:

我正在编码自RelativeLayout扩展的自定义视图,并且我想以编程方式调整它的大小,该怎么办?

I am coding a custom view, extended from RelativeLayout, and I want to resize it programmatically, How can I do?

自定义视图类类似于:

public ActiveSlideView(Context context, AttributeSet attr){
        super(context, attr);
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(inflater != null){       
            inflater.inflate(R.layout.active_slide, this);
        }

this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));

问题解决了!

注意:确保使用父版面的LayoutParams.我的是LinearLayout.LayoutParams

NOTE: Be sure to use the parent Layout's LayoutParams. Mine is LinearLayout.LayoutParams!