public class MainActivity extends AppCompatActivity { // 定义FragmentTabHost对象 private FragmentTabHost mTabHost; // 定义数组来存放Fragment界面 private Class<?> mFragmentArray[] = { RecordFragment.newInstance(0).getClass(), InteractFragment.newInstance(1).getClass(), RemindFragment.newInstance(2).getClass(), MyFragment.newInstance(3).getClass()}; //定义数组来存放按钮图片 private int mImageViewArray[] = { R.drawable.selector_tab_home, R.drawable.selector_tab_square, R.drawable.selector_tab_mine, R.drawable.selector_tab_my}; //Tab选项卡的文字 private String mTextViewArray[] = {"记录", "互动", "提醒", "我的"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // initVariables(); bindView(savedInstanceState); initData(); initExit(); } // @Override // protected void initVariables() { // // } // private DoubleClickExitDetector mClickExitDetector; //双击返回监听 // @Override // public void onBackPressed() { // if (mClickExitDetector.onClick()) { //退出程序 // System.exit(0); // // finish(); // } // } // @Override protected void bindView(Bundle savedInstanceState) { setContentView(R.layout.activity_main); mTabHost = (FragmentTabHost) findViewById(R.id.tab_host_app_main); mTabHost.setup(this, getSupportFragmentManager(), R.id.fl_app_main_content); //得到fragment个数 int count = mFragmentArray.length; for (int i = 0; i < count; i++) { //为每个Tab按钮设置图标、文字以及内容 TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextViewArray[i]).setIndicator(getTabItemView(i)); // 将Tab按钮添加进Tab选项卡中 mTabHost.addTab(tabSpec, mFragmentArray[i], null); //去分隔线 mTabHost.getTabWidget().setDividerDrawable(null); } } // @Override protected void initData() { } /** * 初始化变量 */ private void initVar() { } /** * 给Tab按钮设置图标和文字 */ private View getTabItemView(int index) { View view = getLayoutInflater().inflate(R.layout.tab_item_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.iv_tab_icon); imageView.setImageResource(mImageViewArray[index]); TextView textView = (TextView) view.findViewById(R.id.tv_tab_text); textView.setText(mTextViewArray[index]); return view; } /** * 初始化双击返回的类 */ private void initExit() { // mClickExitDetector = new DoubleClickExitDetector(this); }}activity_main
<?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"> <!--该FrameLayout布局用来显示fragment--> <FrameLayout android:id="@+id/fl_app_main_content" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"> </FrameLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/gray_line_bg"/> <!--这个FragmentTabHost用于显示下方的功能标签 --> <android.support.v4.app.FragmentTabHost android:id="@+id/tab_host_app_main" android:layout_width="match_parent" android:layout_height="56dp" android:background="#FFFFFF"/> </LinearLayout>
RecordFragment(其中一个)
public class RecordFragment extends Fragment { protected static String ARGUMENT_NUM = "num"; // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public RecordFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @return A new instance of fragment RecordFragment. */ // TODO: Rename and change types and number of parameters public static RecordFragment newInstance(int num) { RecordFragment f = new RecordFragment(); Bundle args = new Bundle(); args.putInt(ARGUMENT_NUM, num); f.setArguments(args); return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_record, container, false); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } // @Override // public void onAttach(Context context) { // super.onAttach(context); // if (context instanceof OnFragmentInteractionListener) { // mListener = (OnFragmentInteractionListener) context; // } else { // throw new RuntimeException(context.toString() // + " must implement OnFragmentInteractionListener"); // } // } @Override public void onDetach() { super.onDetach(); mListener = null; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p/> * See the Android Training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments</a> for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } }fragment_record
<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="yhb.com.test.RecordFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> </FrameLayout>
selector_tab_home按下选项卡变化效果<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/jilu_icon" android:state_selected="true"/> <item android:drawable="@drawable/jilu_icon2"/> </selector>
android选项卡
最新推荐文章于 2024-08-04 03:35:21 发布