关于java的android编程中setLayoutParams方法设置宽度的一个疑问
我在一个线性布局方法中添加了两个按钮,并在对两个按钮分别调用了setLayoutParams方法,但是屏幕结果是第一个按钮横向把父控件全占了,第二个按钮显示不了。想了解一...
我在一个线性布局方法中添加了两个按钮,并在对两个按钮分别调用了setLayoutParams方法,但是屏幕结果是第一个按钮横向把父控件全占了,第二个按钮显示不了。想了解一下这是什么状况?代码如下:
private LinearLayout generateHeadOfControl()
{
LinearLayout LayoutHead = createLayout(LinearLayout.HORIZONTAL);
Button DateButton = generateDateButton();
Button ItemButton = generateItemButton();
LayoutHead.addView(DateButton);
LayoutHead.addView(ItemButton);
DateButton.setLayoutParams(new LayoutParams(50,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));//这个按钮屏幕横向全占了,50这个数字怎么改结果都一样
ItemButton.setLayoutParams(new LayoutParams(50,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));//而这个按钮无法显示,同样是无论50怎么改结果一样是被无视
return LayoutHead;
}
先行致谢! 展开
private LinearLayout generateHeadOfControl()
{
LinearLayout LayoutHead = createLayout(LinearLayout.HORIZONTAL);
Button DateButton = generateDateButton();
Button ItemButton = generateItemButton();
LayoutHead.addView(DateButton);
LayoutHead.addView(ItemButton);
DateButton.setLayoutParams(new LayoutParams(50,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));//这个按钮屏幕横向全占了,50这个数字怎么改结果都一样
ItemButton.setLayoutParams(new LayoutParams(50,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));//而这个按钮无法显示,同样是无论50怎么改结果一样是被无视
return LayoutHead;
}
先行致谢! 展开
展开全部
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了~
还有,在setLayout之后再addView操作。
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了~
还有,在setLayout之后再addView操作。
展开全部
问题的关键在这个LayoutHead标签,LayoutHead标签还有很多属性你并没有设置,比如说子控件的排布规则什么的,加上
LayoutHead .setLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
LayoutHead .setOrientation(LinearLayout.VERTICAL)
试试吧,我现在的电脑上没环境不能帮你调试。
LayoutHead .setLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
LayoutHead .setOrientation(LinearLayout.VERTICAL)
试试吧,我现在的电脑上没环境不能帮你调试。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的错误错在,在LinearLayout里设置params用的是ViewGroup的LayoutParams
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;
}
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;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询