package com.demofragment;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;android
@SuppressLint("NewApi")
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO 自動生成的方法存根
return inflater.inflate(R.layout.fragment1, null);
}
}app
package com.demofragment;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;ide
@SuppressLint("NewApi")
public class Fragment2 extends Fragment {xml
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO 自動生成的方法存根
return inflater.inflate(R.layout.fragment2, null);
}
}utf-8
<?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:background="#ff0000"
android:orientation="vertical" >
</LinearLayout>get
<?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:background="#0000ff"
android:orientation="vertical" >
</LinearLayout>it
<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:orientation="vertical"
tools:context=".MainActivity"
android:id="@+id/d">
</LinearLayout>io
package com.demofragment;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;class
public class MainActivity extends Activity {import
@SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int w=getWindowManager().getDefaultDisplay().getWidth(); int h=getWindowManager().getDefaultDisplay().getHeight(); Fragment1 fragment1=new Fragment1(); Fragment2 fragment2=new Fragment2(); FragmentManager fm=getFragmentManager(); FragmentTransaction ft=fm.beginTransaction(); if(w>h){ ft.replace(android.R.id.content, fragment1); }else{ ft.replace(android.R.id.content, fragment2); } ft.commit(); }}