寒假学干货之------ 初学者关于fragment_main(碎片的困扰)

我们在activity_main中编写的框架,会被fragment_main中的取代掉,是因为新版的ADT为了配合平板Android3.0开发

起作用的代码在MainActivity.java中

 1 package com.myprogram.text_two;
 2 
 3 import android.support.v7.app.ActionBarActivity;
 4 import android.support.v7.app.ActionBar;
 5 import android.support.v4.app.Fragment;
 6 import android.os.Bundle;
 7 import android.view.LayoutInflater;
 8 import android.view.Menu;
 9 import android.view.MenuItem;
10 import android.view.View;
11 import android.view.ViewGroup;
12 import android.os.Build;
13 
14 public class MainActivity extends ActionBarActivity {
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20 
21         if (savedInstanceState == null) { 22  getSupportFragmentManager().beginTransaction() 23                     .add(R.id.container, new PlaceholderFragment()) 24  .commit(); 25  } 26     }
27 
28 
29     @Override
30     public boolean onCreateOptionsMenu(Menu menu) {
31         
32         // Inflate the menu; this adds items to the action bar if it is present.
33         getMenuInflater().inflate(R.menu.main, menu);
34         return true;
35     }
36 
37     @Override
38     public boolean onOptionsItemSelected(MenuItem item) {
39         // Handle action bar item clicks here. The action bar will
40         // automatically handle clicks on the Home/Up button, so long
41         // as you specify a parent activity in AndroidManifest.xml.
42         int id = item.getItemId();
43         if (id == R.id.action_settings) {
44             return true;
45         }
46         return super.onOptionsItemSelected(item);
47     }
48 
49     /**
50      * A placeholder fragment containing a simple view.
51      */
52     public static class PlaceholderFragment extends Fragment {
53 
54         public PlaceholderFragment() {
55         }
56 
57         @Override
58         public View onCreateView(LayoutInflater inflater, ViewGroup container, 59  Bundle savedInstanceState) { 60             View rootView = inflater.inflate(R.layout.fragment_main, container, false); 61             return rootView; 62  } 63  } 64 
65 }

中浅蓝色背景的,屏蔽掉即可

你可能感兴趣的:(寒假学干货之------ 初学者关于fragment_main(碎片的困扰))