第一篇
private LinearLayout generateHeadOfControl() {
	LinearLayout LayoutHead = createLayout(LinearLayout.HORIZONTAL);
    	Button DateButton = generateDateButton();
    	Button ItemButton = generateItemButton();

        DateButton.setLayoutParams(new LinearLayout.LayoutParams(100,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        ItemButton .setLayoutParams(new LinearLayout.LayoutParams(100,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        layoutHead.addView(DateButton);
        layoutHead.addView(ItemButton); 

        return layoutHead;
}
==========================
注意在LinearLayout里设置params用的是ViewGroup的LayoutParams,会出错,上面的代码OK了~
第二篇

textView.setLayoutParams(new TextSwitcher.LayoutParams( 
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

为什么要用TextSwitcher的LayoutParams呢。查一查API,可以看到这么一句话These supply parameters to the parent of this view specifying how it should be arranged。也就是说一定要用父控件的LayoutParams。如果父控件是LinearLayout,当然就必须写成LinearLayout.LayoutParams

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐