赞
踩
方式1:使用BottomNavigationView控件
xml代码:
- <!--底部导航栏-->
- <com.google.android.material.bottomnavigation.BottomNavigationView
- android:id="@+id/main_navigation_bar"
- android:layout_width="match_parent"
- android:layout_height="49dp"
- android:layout_alignParentBottom="true"
- app:itemIconTint="@drawable/navigation_bar_bg"
- app:itemTextColor="@drawable/navigation_bar_bg"
- android:background="@color/white"
- app:menu="@menu/my_navigation_items"/>
需要在menu菜单中配置每个item
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:id="@+id/home"
- android:icon="@mipmap/home_normal"
- android:title="@string/text_home" />
-
- <item
- android:id="@+id/selected"
- android:icon="@mipmap/select_normal"
- android:title="@string/text_selected" />
-
- <item
- android:id="@+id/red_packet"
- android:icon="@mipmap/red_packet_normal"
- android:title="@string/text_packet" />
-
- <item
- android:id="@+id/search"
- android:icon="@mipmap/search_normal"
- android:title="@string/text_search" />
- </menu>
java代码中获取对应的id即可处理点击事件:
- private void initListener() {
- mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
- @Override
- public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
- int itemId = item.getItemId();
- switch (itemId) {
- case R.id.home:
- switchFragment(mHomePageFragment);
- Logit.d(TAG, " cfx " + item.getTitle());
- break;
- case R.id.selected:
- switchFragment(mSelectedPageFragment);
- Logit.d(TAG, " cfx " + item.getTitle());
- break;
- case R.id.red_packet:
- switchFragment(mRedPacketPageFragment);
- Logit.d(TAG, " cfx " + item.getTitle());
- break;
- case R.id.search:
- switchFragment(mSearchPageFragment);
- Logit.d(TAG, " cfx " + item.getTitle());
- break;
- }
- return true;
- }
- });
- }
方式2:通过自定义RadioGroup实现
xml布局
- <!--RadioGroup继承子LinearLayout,需要设置orientation-->
- <RadioGroup
-
- android:id="@+id/test_navigation_bar"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:orientation="horizontal">
-
- <!--android:background="@null" 没有了默认点击的阴影效果-->
- <!--android:button="@null" 取消RadioButton前面的选择框-->
- <RadioButton
- android:id="@+id/test_home"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@null"
- android:button="@null"
- android:drawableTop="@drawable/test_home_selector"
- android:gravity="center"
- android:text="@string/text_home"
- android:textColor="@drawable/navigation_bar_bg"
- android:textSize="18sp" />
-
- <RadioButton
- android:id="@+id/test_selected"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:button="@null"
- android:drawableTop="@drawable/test_selected_selector"
- android:gravity="center"
- android:text="@string/text_selected"
- android:textColor="@drawable/navigation_bar_bg"
- android:textSize="18sp" />
-
- <RadioButton
- android:id="@+id/test_red_packet"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:button="@null"
- android:drawableTop="@drawable/test_packet_selector"
- android:gravity="center"
- android:text="@string/text_packet"
- android:textColor="@drawable/navigation_bar_bg"
- android:textSize="18sp" />
-
- <RadioButton
- android:id="@+id/test_search"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:button="@null"
- android:drawableTop="@drawable/test_search_selector"
- android:gravity="center"
- android:text="@string/text_search"
- android:textColor="@drawable/navigation_bar_bg"
- android:textSize="18sp" />
- </RadioGroup>
-
- <View
- android:layout_width="match_parent"
- android:layout_height="1dp"
- android:layout_above="@+id/test_navigation_bar"
- android:layout_marginBottom="20dp"
- android:background="@color/colorGrey" />
java代码找到相应的控件:
- public class TextNavigationBarActivity extends AppCompatActivity {
- @BindView(R.id.test_toast)
- public Button mTestToast;
- @BindView(R.id.test_navigation_bar)
- public RadioGroup mNavigationBar;
- private HomePageFragment mHomePageFragment;
- private SelectedPageFragment mSelectedPageFragment;
- private RedPacketPageFragment mRedPacketPageFragment;
- private SearchPageFragment mSearchPageFragment;
- private FragmentManager mFragmentManager;
-
- @Override
- protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_text_navigation_bar);
- ButterKnife.bind(this);
- initFragment();
- initListener();
- switchFragment(mHomePageFragment);
- }
-
- private void initFragment() {
- mHomePageFragment = new HomePageFragment();
- mSelectedPageFragment = new SelectedPageFragment();
- mRedPacketPageFragment = new RedPacketPageFragment();
- mSearchPageFragment = new SearchPageFragment();
- mFragmentManager = getSupportFragmentManager();
- }
-
- private void switchFragment(BaseFragment baseFragment) {
- FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
- fragmentTransaction.replace(R.id.test_page_container, baseFragment);
- fragmentTransaction.commit();
- }
-
- private void initListener() {
- mTestToast.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- // 多次点击会造成toast错乱,显示不准确
- // Toast.makeText(TextNavigationBarActivity.this, "dsad", Toast.LENGTH_SHORT).show();
-
- // 使用Toast工具类
- ToastUtils.showToast("测试Toast");
- }
- });
- mNavigationBar.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup radioGroup, int i) {
- switch (i) {
- case R.id.test_home:
- switchFragment(mHomePageFragment);
- break;
- case R.id.test_selected:
- switchFragment(mSelectedPageFragment);
- break;
- case R.id.test_red_packet:
- switchFragment(mRedPacketPageFragment);
- break;
- case R.id.test_search:
- switchFragment(mSearchPageFragment);
- break;
- }
- }
- });
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。