MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
private Button bt_news1, bt_news2, bt_news3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_news1 = (Button) findViewById(R.id.bt_news1);
bt_news2 = (Button) findViewById(R.id.bt_news2);
bt_news3 = (Button) findViewById(R.id.bt_news3);
bt_news1.setOnClickListener(this);
bt_news2.setOnClickListener(this);
bt_news3.setOnClickListener(this);
FragmentManager fm = getFragmentManager();
//FragmentManager fm = getSupportFragmentManager();//向下兼容
//Fragment需要使用android.support.v4.app.Fragment
FragmentTransaction ft = fm.beginTransaction(); //開啟事務(wù)
ft.replace(R.id.fl_container, new News1Fragment());
ft.commit();
}
@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction(); //開啟事務(wù)
switch (v.getId()) {
case R.id.bt_news1:
Toast.makeText(this, "點(diǎn)擊了社會(huì)新聞", 0).show();
ft.replace(R.id.fl_container, new News1Fragment());
break;
case R.id.bt_news2:
Toast.makeText(this, "點(diǎn)擊了娛樂新聞", 0).show();
ft.replace(R.id.fl_container, new News2Fragment());
break;
case R.id.bt_news3:
Toast.makeText(this, "點(diǎn)擊了國(guó)際新聞", 0).show();
ft.replace(R.id.fl_container, new News3Fragment());
break;
}
ft.commit();
}
}
News1Fragment.java
//1.不需要在清單文件中配置
//2.擁有與activities一模一樣的生命周期
public class News1Fragment extends Fragment {
//返回當(dāng)前fragment顯示的內(nèi)容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return View.inflate(getActivity(), R.layout.fragment01, null);
}
}
News2Fragment.java
//1.不需要在清單文件中配置
//2.擁有與activities一模一樣的生命周期
public class News1Fragment extends Fragment {
//返回當(dāng)前fragment顯示的內(nèi)容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return View.inflate(getActivity(), R.layout.fragment02, null);
}
}
News3Fragment.java
//1.不需要在清單文件中配置
//2.擁有與activities一模一樣的生命周期
public class News1Fragment extends Fragment {
//返回當(dāng)前fragment顯示的內(nèi)容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return View.inflate(getActivity(), R.layout.fragment01, null);
}
}
activity_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"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="@+id/bt_news1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="社會(huì)新聞" />
<Button
android:id="@+id/bt_news2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="娛樂新聞" />
<Button
android:id="@+id/bt_news3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="國(guó)際新聞" />
</LinearLayout>
</RelativeLayout>
fragment01.xml
<?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:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是社會(huì)新聞。"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隔壁鄰居的人咬了狗"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隔壁鄰居的人咬了黃狗"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隔壁鄰居的人咬了大白狗"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隔壁鄰居的人咬了黑狗"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
fragment02.xml
<?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:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是娛樂新聞。"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文章干了一個(gè)奇葩的事情"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="且行且珍惜"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="汪峰有新專輯"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="章子怡干了什么事情"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
fragment03.xml
<?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:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是國(guó)際新聞。"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="奧巴馬喜歡普京"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="奧巴馬覺得。。。"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xxxx"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="阿凡達(dá)發(fā)"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>