手机横竖屏自动切换不同的View:

Landscape-Horizontal-横屏

Portrait-Vertical-竖屏

 1 package com.example.shad_fnst.myfragment;
 2 
 3 import android.app.Activity;
 4 import android.app.FragmentManager;
 5 import android.app.FragmentTransaction;
 6 import android.content.res.Configuration;
 7 import android.os.Bundle;
 8 import android.view.Menu;
 9 import android.view.MenuItem;
10 
11 
12 public class MainActivity extends Activity {
13 
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         //setContentView(R.layout.activity_main);
18         Configuration config = getResources().getConfiguration();
19         FragmentManager fragmentManager = getFragmentManager();
20         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
21         if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){
22             LM_Fragment lm_fragment = new LM_Fragment();
23             fragmentTransaction.replace(android.R.id.content, lm_fragment);
24         }
25         else{
26             PM_Fragment pm_fragment = new PM_Fragment();
27             fragmentTransaction.replace(android.R.id.content, pm_fragment);
28         }
29         fragmentTransaction.commit();
30     }
31 
32     @Override
33     public boolean onCreateOptionsMenu(Menu menu) {
34         // Inflate the menu; this adds items to the action bar if it is present.
35         getMenuInflater().inflate(R.menu.menu_main, menu);
36         return true;
37     }
38 
39     @Override
40     public boolean onOptionsItemSelected(MenuItem item) {
41         // Handle action bar item clicks here. The action bar will
42         // automatically handle clicks on the Home/Up button, so long
43         // as you specify a parent activity in AndroidManifest.xml.
44         int id = item.getItemId();
45 
46         //noinspection SimplifiableIfStatement
47         if (id == R.id.action_settings) {
48             return true;
49         }
50 
51         return super.onOptionsItemSelected(item);
52     }
53 }
MainActivity.java
 1 package com.example.shad_fnst.myfragment;
 2 
 3 import android.app.Fragment;
 4 import android.os.Bundle;
 5 import android.view.LayoutInflater;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 
 9 /**
10  * Created by shad-fnst on 2015/07/30.
11  */
12 public class LM_Fragment extends Fragment{
13     @Override
14     public View onCreateView(LayoutInflater inflater,
15                              ViewGroup container, Bundle savedInstanceState){
16         return inflater.inflate(R.layout.lm_fragment, container, false);
17     }
18 }
LM_Fragment.java
 1 package com.example.shad_fnst.myfragment;
 2 
 3 import android.app.Fragment;
 4 import android.os.Bundle;
 5 import android.view.LayoutInflater;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 
 9 /**
10  * Created by shad-fnst on 2015/07/30.
11  */
12 public class PM_Fragment extends Fragment{
13     @Override
14     public View onCreateView(LayoutInflater inflater,
15                              ViewGroup container, Bundle savedInstanceState){
16         return inflater.inflate(R.layout.pm_fragment, container, false);
17     }
18 }
PM_Fragment.java
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent" android:layout_height="match_parent"
 4     android:orientation="vertical"
 5     android:background="#7bae16" >
 6 
 7     <TextView
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:text="@string/landscape_message"
11         android:textColor="#000000"
12         android:textSize="20px" />
13 
14 </LinearLayout>
lm_fragment.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent" android:layout_height="match_parent"
 4     android:orientation="vertical"
 5     android:background="#666666" >
 6 
 7     <TextView
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:text="@string/portrait_message"
11         android:textColor="#000000"
12         android:textSize="20px" />
13 
14 </LinearLayout>
pm_fragment.xml
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 3     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
 4     android:paddingRight="@dimen/activity_horizontal_margin"
 5     android:paddingTop="@dimen/activity_vertical_margin"
 6     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
 7     android:orientation="horizontal" >
 8 
 9     <fragment
10         android:layout_width="0dp"
11         android:layout_height="match_parent"
12         android:name="com.example.shad_fnst.myfragment.LM_Fragment"
13         android:id="@+id/lm_fragmnet"
14         android:layout_weight="1"/>
15 
16     <fragment
17         android:layout_width="0dp"
18         android:layout_height="match_parent"
19         android:name="com.example.shad_fnst.myfragment.PM_Fragment"
20         android:id="@+id/pm_fragmnet"
21         android:layout_weight="2"/>
22 
23 </LinearLayout>
activity_main.xml

 

posted on 2015-07-31 14:21  hello_sandy  阅读(185)  评论(0)    收藏  举报