如何重置或取消以编程方式设置的视图高度或宽度 [英] How to reset or unset a view height or width which were set programmatically

查看:85
本文介绍了如何重置或取消以编程方式设置的视图高度或宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为此而苦苦挣扎,由于其通用关键字将结果引向无关的问题,因此我找不到直接答案.

I am struggling with this and I cannot find a direct answer to this question due to its generic keywords leading the results to unrelated questions.

如果我以编程方式设置视图的高度或宽度,例如:

If I set a view's height or width programmatically, for example:

((Button) findViewById(R.id.myButtonId)).getLayoutParams().height = 100;

如何取消/重置/还原此过程,以便视图的高度(或宽度)恢复到设置该大小之前的默认行为?

How can I unset/reset/revert this process so the view's height (or width) returns to its default behavior before that size was set?

我正在尝试找到与height: unsetheight: initial等效的Html CSS甚至是内联样式some_dom_element.removeAttribute("style")

I am trying to find something like the Html CSS equivalent of height: unset or height: initial or even the JavaScript way for inline styles some_dom_element.removeAttribute("style")

有什么要做的吗?

推荐答案

要解决此问题,应在修改高度之前保存高度.然后,当您要重置它时,可以恢复到原始高度.幸运的是,MATCH_PARENTWRAP_CONTENT的特殊情况都表示为整数常量,因此即使在这些情况下也可以使用(研究这一点时要贷给OP).

To solve this problem you should save the height before modifying it. Then when you want to reset it, you can revert to the original height. Luckily, the special cases of MATCH_PARENT and WRAP_CONTENT are represented as integer constants, so this works even for those cases (credit to OP for researching this point).

因此,解决方案是这样的:

So, the solution is something like this:

int initial_height = view.getLayoutParams().height;

然后,当您要将高度重新设置为其先前的值时,只需引用initial_height即可正确执行此操作.

And then when you want to set the height back to its previous value, just reference the initial_height to do so correctly.

这篇关于如何重置或取消以编程方式设置的视图高度或宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆