Android screen Toggle view set different size or equal scale XML solution

Source: Internet
Author: User

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
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:layout_width="200DP"
  7. android:layout_height="150DP"
  8. android:layout_centerinparent="true"
  9. android:background="@android: Color/holo_red_light"
  10. android:gravity="center"
  11. android:text="horizontal screen" />
  12. </relativelayout>



Res/layout-port/activity_main.xml:

[HTML]View PlainCopyhttp://www.woaipu.com/shops/zuzhuan/61406
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:layout_width="133DP"
  7. android:layout_height="100DP"
  8. android:layout_centerinparent="true"
  9. android:background="@android: Color/holo_red_light"
  10. android:gravity="center"
  11. android:text="vertical screen" />
  12. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.