#最简单的底部标签页--仅用fragment:
图示:

activity_main.xml
<RelativeLayout
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/linearLayout"/>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
android:orientation="horizontal"
android:paddingBottom="4dp"
android:paddingTop="4dp">
<RelativeLayout
android:id="@+id/home_layout_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<TextView
android:id="@+id/home_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/comui_tab_home"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/home_image_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:text="首页"
android:textColor="#333333"
android:textSize="16sp"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/pond_layout_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"
>
<TextView
android:id="@+id/fish_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/comui_tab_pond"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/fish_image_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:text="鱼塘"
android:textColor="#333333"
android:textSize="16sp"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/message_layout_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<TextView
android:id="@+id/message_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/comui_tab_message"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/message_image_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:text="消息"
android:textColor="#333333"
android:textSize="16sp"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/mine_layout_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<TextView
android:id="@+id/mine_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/comui_tab_home"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/mine_image_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:text="我的"
android:textColor="#333333"
android:textSize="16sp"
/>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_above="@+id/linearLayout"
android:background="#333333"
/>
MainActivity.java
public class MainActivity extends BaseActivity implements View.OnClickListener {
//fragment
private FragmentManager fm;
private HomeFragment mHomeFragment;
private MessageFragment mMessageFragment;
private MineFragment mMineFragment;
private Fragment mCurrent;
//ui
private RelativeLayout mHomeLayout;
private RelativeLayout mPondLayout;
private RelativeLayout mMessageLayout;
private RelativeLayout mMineLayout;
private TextView mHomeView;
private TextView mPondView;
private TextView mMessageView;
private TextView mMineView;
@Override
protected BasePresenter setPresenter() {
return null;
}
@Override
protected int setContentViewId() {
return R.layout.activity_main;
}
@Override
protected void initView() {
mHomeLayout = (RelativeLayout) findViewById(R.id.home_layout_view);
mHomeLayout.setOnClickListener(this);
//供后期修改添加
mPondLayout = (RelativeLayout) findViewById(R.id.pond_layout_view);
mPondLayout.setOnClickListener(this);
mMessageLayout = (RelativeLayout) findViewById(R.id.message_layout_view);
mMessageLayout.setOnClickListener(this);
mMineLayout = (RelativeLayout) findViewById(R.id.mine_layout_view);
mMineLayout.setOnClickListener(this);
mHomeView = (TextView) findViewById(R.id.home_image_view);
mPondView = (TextView) findViewById(R.id.fish_image_view);
mMessageView = (TextView) findViewById(R.id.message_image_view);
mMineView = (TextView) findViewById(R.id.mine_image_view);
mHomeView.setBackgroundResource(R.drawable.comui_tab_home_selected);
}
//隐藏fragment,但是不销毁
private void hideFragment(Fragment fragment, FragmentTransaction ft) {
if (fragment != null) {
ft.hide(fragment);
}
}
@Override
protected void initData() {
//添加默认要显示的fragment
mHomeFragment = new HomeFragment();
fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.content_layout, mHomeFragment);
fragmentTransaction.commit();
}
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = fm.beginTransaction();
switch (v.getId()) {
case R.id.home_layout_view:
mHomeView.setBackgroundResource(R.drawable.comui_tab_home_selected);
mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
mMessageView.setBackgroundResource(R.drawable.comui_tab_message);
mMineView.setBackgroundResource(R.drawable.comui_tab_person);
hideFragment(mMessageFragment, fragmentTransaction);
hideFragment(mMineFragment, fragmentTransaction);
//显示HomeFragment
if (mHomeFragment == null) {
mHomeFragment = new HomeFragment();
fragmentTransaction.add(R.id.content_layout, mHomeFragment);
} else {
mCurrent = mHomeFragment;
fragmentTransaction.show(mHomeFragment);
}
break;
case R.id.message_layout_view:
mMessageView.setBackgroundResource(R.drawable.comui_tab_message_selected);
mHomeView.setBackgroundResource(R.drawable.comui_tab_home);
mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
mMineView.setBackgroundResource(R.drawable.comui_tab_person);
hideFragment(mHomeFragment, fragmentTransaction);
hideFragment(mMineFragment, fragmentTransaction);
//显示MessageFragment
if (mMessageFragment == null) {
mMessageFragment = new MessageFragment();
fragmentTransaction.add(R.id.content_layout, mMessageFragment);
} else {
mCurrent = mMessageFragment;
fragmentTransaction.show(mMessageFragment);
}
break;
case R.id.mine_layout_view:
mMineView.setBackgroundResource(R.drawable.comui_tab_person_selected);
mHomeView.setBackgroundResource(R.drawable.comui_tab_home);
mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
mMessageView.setBackgroundResource(R.drawable.comui_tab_message);
hideFragment(mMessageFragment, fragmentTransaction);
hideFragment(mHomeFragment, fragmentTransaction);
//显示MineFragment
if (mMineFragment == null) {
mMineFragment = new MineFragment();
fragmentTransaction.add(R.id.content_layout, mMineFragment);
} else {
mCurrent = mMineFragment;
fragmentTransaction.show(mMineFragment);
}
break;
}
fragmentTransaction.commit();
}
}