我正在编写一个从 RelativeLayout 扩展的自定义视图,并且我想以编程方式调整它的大小,我该怎么做?
自定义视图类类似于:
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!
LayoutParams
LinearLayout.LayoutParams