In some applications, it involves switching between the portrait and the screen to switch the view to a different size scale size. To address this development scenario, there are a number of solutions, such as the ability to override the view to re-measure the size of the redraw view in either the Onmesure or the callback method of such view. You can also set the length-to-width ratio of the view in the Onconfigurationchanged, based on the current toggle situation.
Now, a relatively simple and flexible approach is given: by writing two sets of XML layouts, the size of different size scales is realized in different switching states. The key approach to this scenario is to place two layout in res, called Layout-land and Layout-port, respectively. Layout-land horizontal screen will be loaded, layout-port vertical screen when loaded. Just write two layout files with the same name, but put them in the Res/layout-land and Layout-port file directories respectively. In this way, the Android system will automatically load the layout according to the current screen.
Given an example, in this case there is only one activity_main.xml, which needs to load different layouts when switching between the different screens. Then write two different activity_main.xml, but with the same name, a layout file.
Res/layout-land/activity_main.xml:
[HTML]View PlainCopyhttp://www.woaipu.com/shops/zuzhuan/61406
- <? XML version= "1.0" encoding="Utf-8"?>
- <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="200DP"
- android:layout_height="150DP"
- android:layout_centerinparent="true"
- android:background="@android: Color/holo_red_light"
- android:gravity="center"
- android:text="horizontal screen" />
- </relativelayout>
Res/layout-port/activity_main.xml:
[HTML]View PlainCopyhttp://www.woaipu.com/shops/zuzhuan/61406
- <? XML version= "1.0" encoding="Utf-8"?>
- <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="133DP"
- android:layout_height="100DP"
- android:layout_centerinparent="true"
- android:background="@android: Color/holo_red_light"
- android:gravity="center"
- android:text="vertical screen" />
- </relativelayout>
code file Structure:
http://www.woaipu.com/shops/zuzhuan/61406
The result of the code's operation when the screen is switched:
Horizontal screen:
http://www.woaipu.com/shops/zuzhuan/61406
Android screen Toggle view set different size or equal scale XML solution