Android_Fragment实现Tab
一.布局
二.代码
1.先写布局文件
主界面的布局文件 主要是分两部分:上面是一个实现Fragment中的内容,下面是RadioGroup
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/ll"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:background="#006644" >
- <RadioGroup
- android:id="@+id/rg"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <RadioButton
- android:id="@+id/rb_message"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:button="@null"
- android:checked="true"
- android:drawableTop="@android:drawable/btn_star"
- android:gravity="center"
- android:text="消息" />
- <RadioButton
- android:id="@+id/rb_contact"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:button="@null"
- android:drawableTop="@android:drawable/btn_star"
- android:gravity="center"
- android:text="联系人" />
- <RadioButton
- android:id="@+id/rb_dynamic"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:button="@null"
- android:drawableTop="@android:drawable/btn_star"
- android:gravity="center"
- android:text="动态" />
- <RadioButton
- android:id="@+id/rb_setting"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:button="@null"
- android:drawableTop="@android:drawable/btn_star"
- android:gravity="center"
- android:text="设置" />
- </RadioGroup>
- </LinearLayout>
- <FrameLayout
- android:id="@+id/fl_content"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_above="@id/ll" >
- </FrameLayout>
- </RelativeLayout>
四个Fragment中的布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:src="@android:drawable/btn_star" />
- <TextView
- android:id="@+id/tv_content"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:padding="10dp"
- android:textSize="20sp" />
- </LinearLayout>
- </RelativeLayout>
写四个Fragment来加载对应的布局
- package com.example.fragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- public class Fragment_Contact extends Fragment {
- private TextView tv_content;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_main, null);
- tv_content = (TextView) view.findViewById(R.id.tv_content);
- tv_content.setText("联系人");
- return view;
- }
- }
- package com.example.fragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- public class Fragment_Dynamic extends Fragment {
- private TextView tv_content;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_main, null);
- tv_content = (TextView) view.findViewById(R.id.tv_content);
- tv_content.setText("动态");
- return view;
- }
- }
- package com.example.fragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- public class Fragment_message extends Fragment {
- private TextView tv_content;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_main, null);
- tv_content = (TextView) view.findViewById(R.id.tv_content);
- tv_content.setText("消息");
- return view;
- }
- }
- package com.example.fragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- public class Fragment_Setting extends Fragment {
- private TextView tv_content;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_main, null);
- tv_content = (TextView) view.findViewById(R.id.tv_content);
- tv_content.setText("设置");
- return view;
- }
- }
主界面中实现Tab的切换
- package com.example.fragment;
- import android.app.Activity;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Window;
- import android.widget.FrameLayout;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- public class MainActivity extends Activity {
- protected static final String TAG = "MainActivity";
- private RadioGroup rg;
- private RadioButton rb_message;
- private RadioButton rb_contact;
- private RadioButton rb_dynamic;
- private RadioButton rb_setting;
- private FrameLayout fl_content;
- private Fragment_message fragment_message;
- private Fragment_Contact fragment_contact;
- private Fragment_Dynamic fragment_dynamic;
- private Fragment_Setting fragment_setting;
- private FragmentManager fragmentManager; // 管理Fragment
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- init();
- // 得到一个Fragment的管理者
- fragmentManager = getFragmentManager();
- // 开启一个Fragment事务
- FragmentTransaction transaction = fragmentManager.beginTransaction();
- fragment_message = new Fragment_message();
- transaction.add(R.id.fl_content, fragment_message);
- transaction.commit();
- Log.i(TAG, "消息的Fragment创建成功");
- // RadioGroup的点击事件
- rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- // transaction = fragmentManager.beginTransaction();
- // hideFragments(transaction);
- @Override
- public void onCheckedChanged(RadioGroup arg0, int arg1) {
- // 开启一个Fragment事务
- FragmentTransaction transaction = fragmentManager.beginTransaction();
- hideFragments(transaction);
- if (arg1 == rb_message.getId()) {
- if (fragment_message == null) {
- fragment_message = new Fragment_message();
- transaction.add(R.id.fl_content, fragment_message);
- } else {
- transaction.show(fragment_message);
- }
- Log.i(TAG, "选中了消息的按钮");
- } else if (arg1 == rb_contact.getId()) {
- if (fragment_contact == null) {
- fragment_contact = new Fragment_Contact();
- transaction.add(R.id.fl_content, fragment_contact);
- } else {
- transaction.show(fragment_contact);
- }
- Log.i(TAG, "选中了联系人的按钮");
- } else if (arg1 == rb_dynamic.getId()) {
- if (fragment_dynamic == null) {
- fragment_dynamic = new Fragment_Dynamic();
- transaction.add(R.id.fl_content, fragment_dynamic);
- } else {
- transaction.show(fragment_dynamic);
- }
- Log.i(TAG, "选中了动态的按钮");
- } else if (arg1 == rb_setting.getId()) {
- if (fragment_setting == null) {
- fragment_setting = new Fragment_Setting();
- transaction.add(R.id.fl_content, fragment_setting);
- } else {
- transaction.show(fragment_setting);
- }
- Log.i(TAG, "选中了设置的按钮");
- }
- transaction.commit();
- }
- });
- }
- /**
- * 控件的实例化
- */
- private void init() {
- rg = (RadioGroup) findViewById(R.id.rg);
- rb_message = (RadioButton) findViewById(R.id.rb_message);
- rb_contact = (RadioButton) findViewById(R.id.rb_contact);
- rb_dynamic = (RadioButton) findViewById(R.id.rb_dynamic);
- rb_setting = (RadioButton) findViewById(R.id.rb_setting);
- fl_content = (FrameLayout) findViewById(R.id.fl_content);
- }
- /**
- * 将所有的Fragment都置为隐藏状态。
- *
- * @param transaction
- * 用于对Fragment执行操作的事务
- */
- private void hideFragments(FragmentTransaction transaction) {
- if (fragment_message != null) {
- transaction.hide(fragment_message);
- }
- if (fragment_contact != null) {
- transaction.hide(fragment_contact);
- }
- if (fragment_dynamic != null) {
- transaction.hide(fragment_dynamic);
- }
- if (fragment_setting != null) {
- transaction.hide(fragment_setting);
- }
- }
- }

浙公网安备 33010602011771号