当前位置:主页 > 移动开发 > Android代码 >

Android使用BottomTabBar实现底部导航页效果

时间:2020-12-31 14:34:50 | 栏目:Android代码 | 点击:

1. 导依赖

compile 'com.hjm:BottomTabBar:1.1.1'

2. 在所实现的XML中定义一下该控件

<com.hjm.bottomtabbar.BottomTabBar
  android:id="@+id/bottom_tab_bar"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >
</com.hjm.bottomtabbar.BottomTabBar>

3. 定义每个Fragment,这里列举一个

public class OneFragment extends Fragment{
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment1, container, false);
    return view;
  }
}

4. MainActivity中

//Fragment页面设置页
public class MainActivity extends AppCompatActivity {
  private BottomTabBar bottomTabBar;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bottomTabBar = findViewById(R.id.bottom_tab_bar);
    //初始化Fragment
    bottomTabBar.init(getSupportFragmentManager())
        .setImgSize(50, 50)   //图片大小
        .setFontSize(12)            //字体大小
        .setTabPadding(4, 6, 10)//选项卡的间距
        .setChangeColor(Color.RED, Color.BLUE)   //选项卡的选择颜色
        .addTabItem("首页", R.drawable.home_, ShouYe_Fragment.class)
        .addTabItem("分类", R.drawable.classify, FenLei_Fragment.class)
        .addTabItem("发现", R.drawable.cart, Cart_Fragment.class)
        .addTabItem("我的", R.drawable.mine, Mine_Fragment.class)
        .isShowDivider(true)  //是否包含分割线
        .setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() {
          @Override
          public void onTabChange(int position, String name) {
            Log.i("TGA", "位置:" + position + "   选项卡:" + name);
          }
        });
  }
}

效果图:

下面通过实例代码介绍下 Android_BottomTabBar

一:依赖 

compile 'com.hjm:BottomTabBar:1.1.1' 

二:布局

<com.hjm.bottomtabbar.BottomTabBar 
    android:id="@+id/bottomTabBar" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
  </com.hjm.bottomtabbar.BottomTabBar> 

三:代码

public class MainActivity extends AppCompatActivity { 
  @BindView(R.id.bottomtabbar) 
  BottomTabBar bottomtabbar; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ButterKnife.bind(this); 
    bottomtabbar.init(getSupportFragmentManager()) 
        .setImgSize(25, 25) 
        .setFontSize(8) 
        .setTabPadding(4, 6, 10) 
        .setChangeColor(Color.RED, Color.BLACK) 
        .addTabItem("精选", R.drawable.choiceness, ChoicenessFragment.class) 
        .addTabItem("专题", R.drawable.dissertation, DissertationFragment.class) 
        .addTabItem("发现", R.drawable.discover, DiscoverFragment.class) 
        .addTabItem("我的", R.drawable.my, MyFragment.class) 
        .isShowDivider(false) 
        .setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() { 
          @Override 
          public void onTabChange(int position, String name) { 
          } 
        }) 
        .setTabBarBackgroundResource(R.drawable.bottom_bg) 
        .setBackgroundResource(R.drawable.bg_blue); 
  } 
} 

总结

您可能感兴趣的文章:

相关文章