在Android中以编程方式更改Button的宽度

编程入门 行业动态 更新时间:2024-10-26 00:21:44
本文介绍了在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的宽度

本文发布于:2023-11-13 01:07:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582992.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:宽度   中以   方式   Android   Button

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!