本文介绍了在Android中以编程方式更改Button的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以编程方式更改Button的width.我可以更改height,但宽度不变.以下是我的代码段
How to change the width of a Button programmatically. I am able to change the height but the width does not change. Following is my code snippet
private void createButton(final String label) { Button button = new Button(this); button.setText(label); button.setWidth(10); button.setHeight(100); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } }); mMenuContainer.addView(button, mMenuItemLayoutParamters); }但是Button的width占据了屏幕的width.
推荐答案button.setLayoutParams(new LinearLayout.LayoutParams(10, 100));
应该为您工作.
对此有一个很棒的示例在线:检查这个
There is one Awesome Example online for this : check this
您应该应用包含View的ViewGroup中的LayoutParams.也就是说,如果您的按钮位于RelativeLayout内,则应使用RelativeLayout.LayoutParams
you should apply LayoutParams of the ViewGroup that contains your View. That is, if your button is inside RelativeLayout, you should use RelativeLayout.LayoutParams
更多推荐
在Android中以编程方式更改Button的宽度
发布评论