Android-layout-screen processing

Source: Internet
Author: User

The screen processing is the development of the application is a basic point of comparison, almost all will be used. Online there is a lot of the screen to switch between the article, but turned over N page found unexpectedly exclusively is reproduced, so do not want to waste time to this above, or oneself according to their own needs and experience summed up it, also convenient for later review

The difference and use of Layout-land and Layout-prot

By default, there is only one layout folder in the Android project that is created, although it can be toggled in a horizontal screen, but some layouts have a flat screen that is especially ugly after

Horizontal screen After the display is not complete, and sometimes look at the more tangled. So you need to reload the new layout file in the horizontal screen.

The workaround is to delete the layout directory first, because it may conflict with the following. Then create a new two folder, one Layout-land, and the other is Layout-prot.

Layout-land: Store A horizontal screen layout file, such as main. XML. The layout name is the same as Layout-prot.

Layout-prot: Store portrait layout file with the same name as Layout-land

The rest of the matter can be processed by the mobile phone, the phone will automatically handle the layout of the screen when the switch between (if your phone supports the screen, and: Settings-display-automatically rotate the screen)

Let's take a look at the two layout files, the activity can be

Layout-land/main.xml

<?XMLVersion= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:layout_height=" match_parent "android:orientation=" ho         Rizontal "> <linearlayout android:orientation=" vertical "android:layout_width=" fill_parent "         android:layout_height= "wrap_content" android:layout_gravity= "center" android:paddingleft= "20dip" android:paddingright= "20dip" > <textview android:text= "cnblogs-Flower Lang" Android : layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "ce         Nter "android:layout_marginbottom=" 20dip "android:textsize=" 24.5sp "/>             <tablelayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content"                 android:layout_gravity= "Center" android:stretchcolumns= "*" > <TableRow> <buttOn android:text= "Eat"/> <button android:                    text= "Sleeping"/> </TableRow> <TableRow> <button android:text= "Travel"/> <button android:text= "Tomb Raider"/> </TableRow> </TableLayout> </LinearLayout> </l Inearlayout>

Layout-prot/main.xml

<?XMLVersion= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android "android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:background=" #3500 FFFF "android:padding=" 30dip "android:orientation=" horizontal "> <linearlayout android:orientatio n= "vertical" android:layout_height= "wrap_content" android:layout_width= "Fill_parent" android:layou t_gravity= "center" > <textview android:text= "cnblogs-flower Lang" Android:layout_widt             H= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center"             Android:layout_marginbottom= "25dip" android:textsize= "24.5sp"/> <button             Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "Eat" /> <button android:layout_width= "fill_parent" android:layout_height= "Wrap_conte NT "Android:text= "Sleeping"/> <button android:layout_width= "Fill_parent" Android:layout_h eight= "Wrap_content" android:text= "Tour"/> <button android:layout_width= " Fill_parent "android:layout_height=" wrap_content "android:text=" tomb Grave "/> </lin Earlayout> </LinearLayout>

Here is a picture of the truth, the layout when the horizontal screen.

Note: The command-type CTRL+F12 of the toggle simulator

One thing to note here is the life cycle problem of the screen switching.

In the case above, when the screen is switched, the activity is destroyed and then created again.

When you do not add any settings, its life cycle is this:

Oncreate-onstart-onresume Start Run time

Onpause-onstop-ondestroy-oncreate-onstart-onresume, it's always the same when you switch between screens.

When in Androidmanifest. The activity tags in the xml include:

Android:configchanges= "Orientation|keyboardhidden"

or android:configchanges= "orientation" after

The screen switch does not need to be re-loaded, that is, you do not have to destroy the activity and then re-create the activity.

Another way of looking at the life cycle of the screen is different from the Onconfigurechanges method, and the life cycle of it is a little differently from mine.

Reference: http://hi.baidu.com/%C2%E4%C2%E4%E7%F7%E7%F7%B0%A1/blog/item/dcc19ce5a9c274cc2e2e2178.html

Second, how to limit the horizontal screen or vertical screen?

Some people hate to play on the phone when the screen to switch back and forth, some applications also limit the application to use only horizontal screen or only the vertical screen, even if the phone set the "Auto-toggle screen." For example, "Fruit Ninja" is not vertical screen (except for the two-person mode)

Workaround: Just take the androidmanifest. add: android:screenorientation= "Landscape" in the Activity tab of the XML

android:screenorientation= "Landscape" means that the horizontal screen or android:screenorientation= "protrait" represents the vertical screen

In this way, the application you set can only be a horizontal or vertical screen.

Third, the question about activity reload when the screen is switched on or off (onconfigurationchanged () method)

For example, in the above example, the activity each time the screen switch is re-loaded, but for example, we play the game when the screen switch, we can not play again, so need to have a solution to the screen switch between the time to save the current state, without re-loaded method

Solution: You can use the Onconfigurationchanged method, which allows users to switch between the screen and the user without having to re-execute the OnCreate method.

Reminder: The use of this method of the scene, such as playing audio and video conversion screen, if it is set up two layout, then the screen to correspond to different layout, also means to implement the OnCreate method, so the layout is the best one (do not know right, ask high finger point)

1. Need to add in the corresponding activity tag in Androidmanifest.xml

Android:configchanges= "Orientation|keyboardhidden"

This statement means: The change of configuration information when the screen switch or the physical keyboard is closed

2, need in androidmanifest. adding permissions in XML

<uses-permission android:name= "Andorid.permission.CHANGE_CONFIGURATION"/>

3, the need to switch between the screen and the same activity to cover the Onconfigurationchanged method, as follows

    @Override public    void onconfigurationchanged (Configuration newconfig) {        //TODO auto-generated method stub         super.onconfigurationchanged (newconfig);        if (This.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape)        {            Toast.maketext (Getapplicationcontext (), "Switch to horizontal screen", Toast.length_short). Show ();        else if (this.getresources (). GetConfiguration (). Orientation = = configuration.orientation_portrait)        {            Toast.maketext (Getapplicationcontext (), "Switch to vertical screen", toast.length_short). Show ();        }    }

What needs to be said here is that the IF statement in the code determines whether the current device is a horizontal or vertical screen, and then has its corresponding action. Before the screen switch to set a different layout, although the ability to display a different layout, but this method is meaningless, because the screen to switch to a different layout we can use the first method above, and this is better just a layout bar, and then the inside of the screen when the other operation, Prevents re-loading

Let's look at an example below, an example of an RTSP stream playing

Package Com.loulijun.demo07;import Android.app.activity;import Android.content.res.configuration;import Android.net.uri;import Android.os.bundle;import Android.view.window;import Android.view.windowmanager;import Android.widget.toast;import Android.widget.videoview;public class Demo07activity extends Activity {private Videoview V    Ideo Private String Rtspurl = "rtsp://218.205.231.149:554/Mobile/1/2cbe124b67c85a59/48f313651199829e.sdp?id=guest&t=1305313158&en=f2ed024c7963e179f65c65689fdd9887    &rs=wap ";        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); Video = (videoview) Findviewbyid (r.id. play);        Video.setvideouri (Uri.parse (Rtspurl));        Video.requestfocus ();    Video.start ();         } @Override public void onconfigurationchanged (Configuration newconfig) {//TODO auto-generated method stub        Super.onconfigurationchanged (Newconfig); if (This.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape) {TOAST.M        Aketext (Getapplicationcontext (), "Switch to horizontal screen", Toast.length_short). Show (); }else if (this.getresources (). GetConfiguration (). Orientation = = configuration.orientation_portrait) {Toa        St.maketext (Getapplicationcontext (), "Switch to vertical screen", toast.length_short). Show (); }    }        }

Main.xml

 <? xml  version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk /res/android        "Android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "> <videoview android:id= "@+id/play  "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:layout_ gravity= "center"/></LINEARLAYOUT>    

Here need to say videoview full screen display problem, horizontal screen after the right always empty a black area, even through the way of WindowManager can not solve, simply can only set the layout for the center display, at least look good

So just add a style to the manifest file: Android:theme= "@android:style/theme.notitlebar.fullscreen"

This way when playing the video can not be full screen, I hope that Daniel can come up with their own solutions
Androidmanifest. XML

<?XMLVersion= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/android "package=" com.loulijun.demo07 "android:versioncode=" 1 "android:versionname=" 1.0 "> &LT;USES-SD K android:minsdkversion= "8"/> <application android:icon= "@drawable/ic_launcher" Android:label= "@string/app_name" > <activity android:label= "@string/app_name" android:configchanges= "Orientation|keyboardhidden" Android:theme= "@android: style/theme.notitlebar.fullscreen "Android:name=".  Demo07activity "> <intent-filter > <action android:name=" Android.intent.action.MAIN "         /> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> <uses-permission android:name= "Andorid.permission.CHANGE_CONFI Guration "/> <uses-permission android:name=" Android.permission.ACCESS_WIFI_STATE "/> <uses-permission an Droid:name= "Android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name= " Android.permission.INTERNET "/> </manifest>

Let's see how it works.

Android-layout-screen processing

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.