2014-03-25 94 views
0

繼官方Android「Hello World」教程之後,會生成兩個佈局文件:activity_main.xmlfragment_main.xml。同時,類MainActivity中生成嵌套類PlaceholderFragment。然後教程要求更改fragment_main.xml而不是activity_main.xml關於Android教程「hello world」的疑問

我的第一個問題: 爲什麼要改變fragment_main.xml而不是activity_main.xml

第二個問題: 如何使用類PlaceholderFragment?誰叫它?由於UI設計在fragment_main.xml中,因此MainActivity可以如何顯示UI?

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.myfirstapp.MainActivity" 
tools:ignore="MergeRootFrame" /> 

fragment_main.xml

<RelativeLayout 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: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.example.myfirstapp.MainActivity$PlaceholderFragment" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

</RelativeLayout> 

MainActivity.java

public class MainActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()) 
       .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.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(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     return rootView; 
    } 
} 
} 

感謝。 Deryk

+5

閱讀:http://developer.android.com/guide/components/fragments.html – Dyrborg

+0

雖然有一個「fragment_main.xml」和一個佔位符自動創建,我沒有使用片段。那個鏈接沒有提到我的問題。 – derek

+0

使用最新的SDK更新,片段活動是默認的空白項目默認爲現在的內容。這不是一個受歡迎的變化,請參閱http://code.google.com/p/android/issues/detail?id=67513 – NickT

回答

0

1 - 你不必把它命名爲fragment_main但它在這一行中使用同一個名字:

inflater.inflate(R.layout.fragment_main, container, false); 

2 - 它與這個代碼行可視化:

getSupportFragmentManager().beginTransaction() 
      .add(R.id.container, new PlaceholderFragment()) 
      .commit(); 

是的,你正在使用片段,所以你應該閱讀它。片段爲UI管理提供了很多權力。

+0

非常乾淨的解釋! 「PlaceholderFragment」被添加到容器中。只是我仍然有疑問:「PlaceholderFragment」類中的「onCreateView」何時何地執行? – derek

+0

它在活動的onCreate之後被調用。你可以在這裏看到完整的Fragment生命週期: http://developer.android.com/guide/components/fragments.html#Lifecycle – hmartinezd

+0

這實際上是令人困惑的部分。 「MainActivity」中的「oncreate」用於活動(從活動擴展而來),「PlaceholderFragment」中的「oncreateview」用於片段(從片段擴展)。他們如何混合在一起? 「PlaceholderFragment」中的「oncreate」在哪裏? – derek

0

這是因爲最近的一些更新並不都是流行的(IMO)。基本上你在這裏是一個主要的活動,它擴大了作爲一個容器的佈局。 PlaceHolderFragment只是一個膨脹實際「Hello World」視圖的內部類。

我猜這個改變背後的原因是因爲片段通常用在需要可重用代碼的地方 - 你基本上從容器(活動)抽象視圖(片段)。但這不是必需的。通過將上面顯示的TextView小部件複製粘貼到activity_main.xml中(然後刪除fragment_main.xml以及相應的支持代碼 - PlaceHolderFragment),可以達到相同的效果。