main_activity 主布局文件
<?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"> <FrameLayout android:id="@+id/main_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/view_line" /> <View android:id="@+id/view_line" android:layout_width="match_parent" android:layout_height="1dp" android:layout_above="@+id/rl_bottom" android:background="#DCDBDB" /> <LinearLayout android:id="@+id/rl_bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#F2F2F2" android:orientation="horizontal" android:paddingBottom="5dp" android:paddingTop="5dp"> <TextView android:id="@+id/tv_main" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:drawablePadding="3dp" android:drawableTop="@drawable/tab_item_main_img_selector" android:focusable="true" android:gravity="center" android:text="商城" android:textColor="@drawable/tabitem_txt_sel" /> <TextView android:id="@+id/tv_dynamic" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:drawablePadding="3dp" android:drawableTop="@drawable/tab_item_dynamic_img_selector" android:focusable="true" android:gravity="center" android:text="分类" android:textColor="@drawable/tabitem_txt_sel" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <TextView android:id="@+id/tv_message" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:drawablePadding="3dp" android:drawableTop="@drawable/tab_item_message_img_selector" android:focusable="true" android:gravity="center" android:text="订单" android:textColor="@drawable/tabitem_txt_sel" /> <TextView android:id="@+id/tv_person" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:drawablePadding="3dp" android:drawableTop="@drawable/tab_item_person_img_selector" android:focusable="true" android:gravity="center" android:text="我的" android:textColor="@drawable/tabitem_txt_sel" /> </LinearLayout> <ImageView android:id="@+id/iv_make" android:layout_width="80dp" android:layout_height="80dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:paddingBottom="10dp" android:src="@mipmap/gouwu" /></RelativeLayout>
主代码:
package com.kczd.jinlan.activity; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.TextView; import com.kczd.jinlan.R; import com.kczd.jinlan.fragment.ClassificationFragment; import com.kczd.jinlan.fragment.MyFragment; import com.kczd.jinlan.fragment.OrderFragment; import com.kczd.jinlan.fragment.ShopPingFragment; /** * yang_g * 程序主入口 * Created by Administrator on 2017/5/15. */ public class MainActivity extends FragmentActivity { //要切换显示的四个Fragment private ShopPingFragment mainFragment; private ClassificationFragment dynamicFragment; private OrderFragment messageFragment; private MyFragment personFragment; private int currentId = R.id.tv_main;// 当前选中id,默认是主页 private TextView tvMain, tvDynamic, tvMessage, tvPerson;//底部四个TextView @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); tvMain = (TextView) findViewById(R.id.tv_main); tvMain.setSelected(true);//首页默认选中 tvDynamic = (TextView) findViewById(R.id.tv_dynamic); tvMessage = (TextView) findViewById(R.id.tv_message); tvPerson = (TextView) findViewById(R.id.tv_person); /** * 默认加载首页 */ mainFragment = new ShopPingFragment(); getSupportFragmentManager().beginTransaction().add(R.id.main_container, mainFragment).commit(); tvMain.setOnClickListener(tabClickListener); tvDynamic.setOnClickListener(tabClickListener); tvMessage.setOnClickListener(tabClickListener); tvPerson.setOnClickListener(tabClickListener); findViewById(R.id.iv_make).setOnClickListener(onClickListener); } private View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.iv_make: Intent intent = new Intent(MainActivity.this, ShoppingActivity.class); startActivity(intent); break; } } }; private View.OnClickListener tabClickListener = new View.OnClickListener() { @Override public void onClick(View v) { if (v.getId() != currentId) {//如果当前选中跟上次选中的一样,不需要处理 changeSelect(v.getId());//改变图标跟文字颜色的选中 changeFragment(v.getId());//fragment的切换 currentId = v.getId();//设置选中id } } }; /** * 改变fragment的显示 * * @param resId */ private void changeFragment(int resId) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//开启一个Fragment事务 hideFragments(transaction);//隐藏所有fragment if (resId == R.id.tv_main) {//商城 if (mainFragment == null) {//如果为空先添加进来.不为空直接显示 mainFragment = new ShopPingFragment(); transaction.add(R.id.main_container, mainFragment); } else { transaction.show(mainFragment); } } else if (resId == R.id.tv_dynamic) {//分类 if (dynamicFragment == null) { dynamicFragment = new ClassificationFragment(); transaction.add(R.id.main_container, dynamicFragment); } else { transaction.show(dynamicFragment); } } else if (resId == R.id.tv_message) {//订单 if (messageFragment == null) { messageFragment = new OrderFragment(); transaction.add(R.id.main_container, messageFragment); } else { transaction.show(messageFragment); } } else if (resId == R.id.tv_person) {//我的 if (personFragment == null) { personFragment = new MyFragment(); transaction.add(R.id.main_container, personFragment); } else { transaction.show(personFragment); } } transaction.commit();//一定要记得提交事务 } /** * 显示之前隐藏所有fragment * * @param transaction */ private void hideFragments(FragmentTransaction transaction) { if (mainFragment != null)//不为空才隐藏,如果不判断第一次会有空指针异常 transaction.hide(mainFragment); if (dynamicFragment != null) transaction.hide(dynamicFragment); if (messageFragment != null) transaction.hide(messageFragment); if (personFragment != null) transaction.hide(personFragment); } /** * 改变TextView选中颜色 * * @param resId */ private void changeSelect(int resId) { tvMain.setSelected(false); tvDynamic.setSelected(false); tvMessage.setSelected(false); tvPerson.setSelected(false); switch (resId) { case R.id.tv_main: tvMain.setSelected(true); break; case R.id.tv_dynamic: tvDynamic.setSelected(true); break; case R.id.tv_message: tvMessage.setSelected(true); break; case R.id.tv_person: tvPerson.setSelected(true); break; } } }tab_item_main_img_selector 文件:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Non focused states --> <item android:drawable="@mipmap/orderno" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/> <item android:drawable="@mipmap/order" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/> <!-- Focused states --> <item android:drawable="@mipmap/order" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/> <item android:drawable="@mipmap/order" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/> <!-- Pressed --> <item android:drawable="@mipmap/order" android:state_pressed="true" android:state_selected="true"/> <item android:drawable="@mipmap/order" android:state_pressed="true"/> </selector>tabitem_txt_sel 颜色选择器:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Non focused states --> <item android:state_focused="false" android:state_pressed="false" android:state_selected="false" android:color="@color/main_tab_item_text_normal"/> <item android:state_focused="false" android:state_pressed="false" android:state_selected="true" android:color="@color/main_tab_item_text_select"/> <!-- Focused states --> <item android:state_focused="true" android:state_pressed="false" android:state_selected="false" android:color="@color/main_tab_item_text_select"/> <item android:state_focused="true" android:state_pressed="false" android:state_selected="true" android:color="@color/main_tab_item_text_select"/> <!-- Pressed --> <item android:state_pressed="true" android:state_selected="true" android:color="@color/main_tab_item_text_select"/> <item android:state_pressed="true" android:color="@color/main_tab_item_text_select"/> </selector>