开发者

LinearLayout (dynamic) and screen orientation problem

开发者 https://www.devze.com 2023-03-27 10:44 出处:网络
I have tried to create a custom NumberPicker which works in most of the cases pretty well. The only problem is the layout which is created dynamically. The layout consists of

I have tried to create a custom NumberPicker which works in most of the cases pretty well. The only problem is the layout which is created dynamically. The layout consists of

  • two buttons ("+" at the top and "-" at the bottom in portrait mode)
  • one edit

In the constructor ("NumberPicker extends LinearL开发者_如何学Pythonayout") I add the three components by using addView() to the layout.

In case of viewing this layout in landscape mode I would like the buttons "+" and "-" not to be at the top and the bottom, but on the right and on the left, because otherwise the size of the NumberPicker is too large.

By using the event onConfigurationChanged I check if the user has changed the screen orientation and set the orientation of the LinearLayout to horizontal and vertical. The problem is that the "+"- and "-"-Buttons are on wrong positions. It should look this way (e.g. in landscape):

"-" | edit | "+"

But it looks this way:

"+" | edit | "-"

How can I solve this problem? I would like to have the buttons in the right order. Could I solve the problem by using XMLs?

Thank you.


While there probably is a way to solve your problem without XMLs, I think using them is not a bad idea (using, for example, getLayoutInflater().inflate(), which returns a View that you can cast into a NumberPicker).

If you are handling configuration changes manually (using the configurationChanges="keyboardHidden|orientation" in your XML file), then you will need to re-retrieve the XML file using the above snippet. Otherwise, the activity will be destroyed on rotate and the proper resources will be retrieved automatically. If you place your portrait XML file in the layout folder, and the other in the layout-land, it should all work out.


you can have two different layout xmls, one for portrait and one for landscape. for the landscape one, you can then put the minus button before the plus button if you are using a horizontal linearlayout orientation

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号