主界面的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/id_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/id_ly_bottombar" ></FrameLayout> <include android:id="@+id/id_ly_bottombar" android:layout_alignParentBottom="true" android:layout_height="55dp" android:layout_width="match_parent" layout="@layout/bottombar" /> </RelativeLayout>
-------------------------
底部tabbar文件bottombar:
<?xml version="1.0" encoding="utf-8"?> <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent"> <RadioButton android:id="@+id/id_firstbtn" android:background="#FFFF00" android:text="第一个页面" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:button="@android:color/transparent" /> <RadioButton android:background="#FFC0CB" android:id="@+id/id_secondbtn" android:text="第二个页面" android:layout_weight="1" android:gravity="center" android:button="@android:color/transparent" android:layout_width="wrap_content" android:layout_height="match_parent" /> </RadioGroup>
--------以上构成tabbar的UI------------
添加两个Fragment文件:firstfragment和secondfragment(第一个界面和第二个界面)
firstfragment的代码:
public class firstfragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_firstfragment, container, false); } }-------------------------------------------------
firstfragment的xml文件代码:
<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="com.example.ssiipp.study_2.firstfragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="使用第一个界面" /> </FrameLayout>---------------------------------------------------secondfragment的代码:
public class secondfragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_secondfragment, container, false); } }-----------------------------------
secondfragment的xml文件代码:
<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="com.example.ssiipp.study_2.secondfragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="使用第二个界面" /> </FrameLayout>-----------------------------------------------主界面代码:public class MainActivity extends ActionBarActivity implements View.OnClickListener{ firstfragment firstview; secondfragment secondview; RadioButton firstBtn; RadioButton secondBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); firstBtn=(RadioButton)findViewById(R.id.id_firstbtn); secondBtn=(RadioButton)findViewById(R.id.id_secondbtn); firstBtn.setOnClickListener(this); secondBtn.setOnClickListener(this); setOpen(); } public void onClick(View v){ Log.i("thouch","touch"); FragmentManager manager=getFragmentManager(); FragmentTransaction transaction=manager.beginTransaction(); switch (v.getId()){ case R.id.id_firstbtn: if (firstview==null) firstview=new firstfragment(); transaction.replace(R.id.id_content,firstview).commit(); break; case R.id.id_secondbtn: if (secondview==null) secondview=new secondfragment(); transaction.replace(R.id.id_content,secondview).commit(); break; } } private void setOpen(){ FragmentManager fragmentManager=getFragmentManager(); FragmentTransaction transaction=fragmentManager.beginTransaction(); firstview=new firstfragment(); transaction.replace(R.id.id_content,firstview); transaction.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.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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
--------------------------
参考帖子:http://blog.csdn.net/lmj623565791/article/details/37970961