public class MainActivity extends AppCompatActivity { @Override PRotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}activity_main.xml<?xml version="1.0" encoding="utf-8"?><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="horizontal" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.zhiyuan3g.fragmenttest.MainActivity"> <fragment android:name="com.zhiyuan3g.fragmenttest.Fragment1" android:id="@+id/fragment1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <fragment android:name="com.zhiyuan3g.fragmenttest.Fragment2" android:layout_width="0dp" android:id="@+id/fragment2" android:layout_height="match_parent" android:layout_weight="1"/></LinearLayout>Fragment1.class(同Fragment2.class)public class Fragment1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View fragment1 = inflater.inflate(R.layout.fragment1, null); return fragment1; }}運行效果:

二、Fragment的動態創建的步驟:(前提:寫好自己的Fragment類,見上篇文章)1.創建Fragment的管理對象fragmentManager。FragmentManager fragmentManager = getFragmentManager();2.創建事務對象(Fragment事務對象不能抽取,因為每提交一次,就需要一個新的Fragment事務對象.(所有的事務都有這個特性))FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();3.動態創建FragmentfragmentTransaction.replace(android.R.id.content, new Fragment1());
4.提交事務對象fragmentTransaction.commit();
代碼如下:MainActivity.class/** * function:g根據橫豎屏切換不同的Fragment顯示。 */public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); int height = windowManager.getDefaultDisplay().getHeight(); int width = windowManager.getDefaultDisplay().getWidth(); //Fragment的動態創建步驟: //1.創建Fragment的管理對象fragmentManager FragmentManager fragmentManager = getFragmentManager(); //2.創建事務對象 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); if (height > width) { //3.動態創建Fragment fragmentTransaction.replace(android.R.id.content, new Fragment1()); } else { //3.動態創建Fragment fragmentTransaction.replace(android.R.id.content, new Fragment2()); } //4.提交事務對象 fragmentTransaction.commit(); }}activity_main.xml<?xml version="1.0" encoding="utf-8"?><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="horizontal" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.zhiyuan3g.fragmenttest.MainActivity"> </LinearLayout>Fragment1.class(同Fragment2.class)public class Fragment1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View fragment1 = inflater.inflate(R.layout.fragment1, null); return fragment1; }}fragment1.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是fragment1" android:textSize="25dp"/></LinearLayout>運行效果:
豎屏時顯示
橫屏時顯示
新聞熱點
疑難解答