MyFragment

时间:2022-05-03 03:41:56

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

Landscape-Horizontal-横屏

Portrait-Vertical-竖屏

 package com.example.shad_fnst.myfragment;

 import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){
LM_Fragment lm_fragment = new LM_Fragment();
fragmentTransaction.replace(android.R.id.content, lm_fragment);
}
else{
PM_Fragment pm_fragment = new PM_Fragment();
fragmentTransaction.replace(android.R.id.content, pm_fragment);
}
fragmentTransaction.commit();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}

MainActivity.java

 package com.example.shad_fnst.myfragment;

 import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by shad-fnst on 2015/07/30.
*/
public class LM_Fragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.lm_fragment, container, false);
}
}

LM_Fragment.java

 package com.example.shad_fnst.myfragment;

 import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by shad-fnst on 2015/07/30.
*/
public class PM_Fragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.pm_fragment, container, false);
}
}

PM_Fragment.java

 <?xml version="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="vertical"
android:background="#7bae16" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/landscape_message"
android:textColor="#000000"
android:textSize="20px" /> </LinearLayout>

lm_fragment.xml

 <?xml version="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="vertical"
android:background="#666666" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/portrait_message"
android:textColor="#000000"
android:textSize="20px" /> </LinearLayout>

pm_fragment.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="horizontal" > <fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.shad_fnst.myfragment.LM_Fragment"
android:id="@+id/lm_fragmnet"
android:layout_weight="1"/> <fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.shad_fnst.myfragment.PM_Fragment"
android:id="@+id/pm_fragmnet"
android:layout_weight="2"/> </LinearLayout>

activity_main.xml

相关文章