1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_main" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context="com.example.zhoushasha.myapplication.MainActivity"> 9 10 <LinearLayout 11 android:id="@+id/show" 12 android:layout_width="match_parent" 13 android:layout_height="0dp" 14 android:orientation="vertical" 15 android:layout_weight="1"> 16 17 </LinearLayout> 18 <LinearLayout 19 android:layout_width="match_parent" 20 android:layout_height="0dp" 21 android:orientation="vertical" 22 android:layout_weight="1"> 23 <Button 24 android:id="@+id/next" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:layout_gravity="center" 28 android:text="no way" 29 android:onClick="onClick"/> 30 31 32 33 34 35 </LinearLayout> 36 37 </LinearLayout>
fragment界面的布局
<FrameLayout 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" tools:context="layout.yesFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="all roads to china" android:textSize="30dp"/> </FrameLayout>
部分Java实现代码
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_main); 5 FirstFragment firstFragment= new FirstFragment(); 6 7 FragmentManager manager = getFragmentManager(); 8 FragmentTransaction transaction = manager.beginTransaction(); 9 10 firstFragment = new FirstFragment(); 11 transaction.add(R.id.next, firstFragment); 12 transaction.commit(); 13 14 } 15 16 17 public void onClick(View view) { 18 if (view.getId() == R.id.next) { 19 tc = true; 20 if (qh) { 21 FragmentManager manager = getFragmentManager(); 22 FragmentTransaction transaction = manager.beginTransaction(); 23 if (secondFragment == null) { 24 secondFragment secondFragment= new secondFragment(); 25 transaction.replace(R.id.next, secondFragment); 26 transaction.commit(); 27 qh = false; 28 } else { 29 transaction.replace(R.id.next, secondFragment); 30 transaction.commit(); 31 qh = false; 32 } 33 } else { 34 Toast.makeText(this, "This is second fragment", Toast.LENGTH_SHORT).show(); 35 } 36 37 } 38 } 39 40 public boolean onKeyDown(int keyCode, KeyEvent event) { 41 42 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && tc) { 43 44 FragmentManager manager = getFragmentManager(); 45 FragmentTransaction transaction = manager.beginTransaction(); 46 FirstFragment firstFragment=new FirstFragment(); 47 qh = true; 48 tc = false; 49 transaction.replace(R.id.next, firstFragment); 50 transaction.commit(); 51 return false; 52 } else { 53 finish(); 54 } 55 return super.onKeyDown(keyCode, event); 56 57 58 } 59 60 }