1.布局样式
2.main.xml代码
<?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 android:id="@+id/flHomeFragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical"> </FrameLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/shortcut_icon" /> <LinearLayout android:layout_width="match_parent" android:layout_height="55dp" android:gravity="center" android:orientation="horizontal"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center"> <TextView android:id="@+id/llHomeATHome" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:drawableLeft="@drawable/phone_selector_one" android:drawablePadding="3dp" android:gravity="center_vertical" android:text="首页" android:textColor="@drawable/phone_color_bottom_tv" android:textSize="11sp" /> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center"> <TextView android:id="@+id/llHomeATNotice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:drawableLeft="@drawable/phone_selector_two" android:drawablePadding="3dp" android:gravity="center_vertical" android:text="通知" android:textColor="@drawable/phone_color_bottom_tv" android:textSize="11sp" /> <TextView android:id="@+id/tvNotice" android:layout_width="15dp" android:layout_height="15dp" android:layout_marginStart="-40dp" android:layout_marginTop="-5dp" android:layout_toEndOf="@+id/llHomeATNotice" android:background="@drawable/phone_login_button_bg_normal" android:gravity="center" android:text="1" android:textColor="@color/white" android:textSize="8sp" /> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center"> <TextView android:id="@+id/llHomeATMy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:drawableLeft="@drawable/phone_selector_four" android:drawablePadding="3dp" android:gravity="center_vertical" android:text="我的" android:textColor="@drawable/phone_color_bottom_tv" android:textSize="11sp" /> </RelativeLayout> </LinearLayout> </LinearLayout>
3.activity功能实现
public class MainActivity extends AppCompatActivity { MainActivityBinding binding; HomeFragment mHomeFragment = HomeFragment.newInstance(); NoticeFragment mNoticeFragment = NoticeFragment.newInstance(); MineFragment mMineFragment = MineFragment.newInstance(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = MainActivityBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); binding.llHomeATMy.setOnClickListener(mClickListener);//为第一个导航图片添加单机事件 binding.llHomeATHome.setOnClickListener(mClickListener);//为第二个导航图片添加单机事件 binding.llHomeATNotice.setOnClickListener(mClickListener);//为第三个导航图片添加单机事件 FragmentManager fm = getSupportFragmentManager();//获取Fragment的管理器 FragmentTransaction transaction = fm.beginTransaction();// 开启一个事务 transaction.add(R.id.flHomeFragment, mHomeFragment).commit();//设置初始的fragment binding.llHomeATHome.setSelected(true); } //创建单机事件监听器 View.OnClickListener mClickListener = new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = getSupportFragmentManager();//获取Fragment的管理器 FragmentTransaction transaction = fm.beginTransaction();// 开启一个事务 int id = v.getId();//通过获取点击的id判断点击了哪个张图片 if (id == R.id.llHomeATHome) { transaction.replace(R.id.flHomeFragment, mHomeFragment); //替换Fragment binding.llHomeATHome.setSelected(true); binding.llHomeATNotice.setSelected(false); binding.llHomeATMy.setSelected(false); } else if (id == R.id.llHomeATNotice) { transaction.replace(R.id.flHomeFragment, mNoticeFragment); //替换Fragment binding.llHomeATNotice.setSelected(true); binding.llHomeATHome.setSelected(false); binding.llHomeATMy.setSelected(false); } else if (id == R.id.llHomeATMy) { transaction.replace(R.id.flHomeFragment, mMineFragment); //替换Fragment binding.llHomeATMy.setSelected(true); binding.llHomeATNotice.setSelected(false); binding.llHomeATHome.setSelected(false); } transaction.commit(); //提交事务 } }; }