简体   繁体   English

Android:以编程方式覆盖Button minheight

[英]Android : overriding Button minheight programmatically

Searching how to reduce the padding inside my buttons, I found out in https://stackoverflow.com/a/20323723/3888000 that I should set the min-height of my button to 0dp in the xml file. 搜索如何减少按钮内的填充,我在https://stackoverflow.com/a/20323723/3888000中发现我应该将我的按钮的最小高度设置为xml文件中的0dp。 And yes, it works for me. 是的,它对我有用。

Problem is : I want to create my button programmatically, and neither setMinimumHeight(1) nor setMinHeight(1) did work. 问题是:我想以编程方式创建我的按钮,setMinimumHeight(1)和setMinHeight(1)都不起作用。 How to do it ? 怎么做 ?

By the way, what is the difference between these two methods ? 顺便问一下,这两种方法有什么区别?

Thank you ! 谢谢 !

Just calling 只是打电话

btn.setMinHeight(0);

or 要么

btn.setMinimumHeight(0);

didn't work for me. 不适合我。

It worked when I called both methods, like that: 当我调用这两种方法时,它起作用了:

btn.setMinHeight(0);
btn.setMinimumHeight(0);

Checked and rechecked, it's definitely the case, as weird as it looks. 检查并重新检查,情况确实如此,看起来很奇怪。

Button was created programmatically, no XML, but it was a part of a custom numeric control that does have an XML description. Button是以编程方式创建的,没有XML,但它是具有XML描述的自定义数字控件的一部分。

EDIT: 编辑:

setMinHeight is defined by TextView, while setMinimumHeight is defined by View. setMinHeight由TextView定义,而setMinimumHeight由View定义。 According to the docs, the greater of the two values is used, so both must be set. 根据文档,使用两个值中的较大者,因此必须设置两者。 minHeight attribute in XML corresponds to the setMinimumHeight method. XML中的minHeight属性对应于setMinimumHeight方法。 Thanks to@David Liu for this clarification. 感谢@ David Liu的澄清。

I think you should try using setPadding(0,0,0,0); 我想你应该尝试使用setPadding(0,0,0,0); instead and see if that works for you. 相反,看看它是否适合你。

As far as minHeight vs minimumHeight goes, minHeight is the XML attribute name you can use in a layout file. 就minHeight和minimumHeight而言,minHeight是您可以在布局文件中使用的XML属性名称。 setMinimumHeight is the corresponding View API method to do the same thing. setMinimumHeight是相应的View API方法,可以做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式编写android:button - Writing android:button programmatically Android按钮样式编程 - Android Button Styling Programmatically 为什么minHeight属性在WebView Android中不起作用? - Why does minHeight attribute not work in WebView Android? 在Android中以编程方式单击切换按钮 - click toggle button programmatically in android 如何清除“找不到与android:minHeight =相匹配的资源?”的错误?attr / actionBarsize” - How to remove the Error of “no resource found that matches android:minHeight=”?attr/actionBarsize" 在Android上覆盖和控制“最近使用的应用程序”按钮时,onBackPressed()等效于什么? - What is the equivalent of onBackPressed() for overriding and taking control of the Recent Apps button on Android? 使用onkeydown方法或覆盖onbackbuttonpress时,Android后退按钮冻结 - Android back button freezes when using onkeydown method or overriding onbackbuttonpressed Android - 以编程方式添加按钮,设置 ID 崩溃 - Android - Programmatically adding button, setting ID crashes Android - 自定义按钮,具有形状可绘制和渐变编程 - Android - custom button with shape drawables and a gradient programmatically 如何以编程方式更改Android小部件按钮的样式? - How to change the style of an Android widget button programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM