How to reset or unset a view height or width which were set programmatically

倾然丶 夕夏残阳落幕 提交于 2019-12-24 01:27:16

问题


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?

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")

Is there anything to accomplish this?


回答1:


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;

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




回答2:


Thanks to nhouser9 idea and the documentation https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT I have managed to work a solution for this problem. The basic idea is the same, save the initial value so it can be restored after, however first you need to find out whether the view height or width has a fixed or dynamic value and which dynamic value it is, so for my case I have used the following process

int initial_height;
switch (view.getLayoutParams().height) {
    case LayoutParams.MATCH_PARENT:
        initial_height = LayoutParams.MATCH_PARENT;
        break;
    case LayoutParams.WRAP_CONTENT:
        initial_height = LayoutParams.WRAP_CONTENT;
        break;
    default:
        initial_height = view.getLayoutParams().height;
}

But I then understood that the dynamic sizes (match_parent, wrap_content) do have specific values, respectively -1 and -2, according to the documentation, meaning that nhouser9 suggestion worked directly since these dynamic sizes could be acquired directly from the view's dimensions (-1, -2 or positive if fixed size). This means that all that it takes now is to just save the initial size directly, like so

int initial_height = view.getLayoutParams().height;

And that's it. This answer was left here to help anyone looking for the same solution and I will be waiting for the other user to post his answer so that I can choose his as it was his help that got me to the right solution.



来源:https://stackoverflow.com/questions/42045976/how-to-reset-or-unset-a-view-height-or-width-which-were-set-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!