android自带的有TabHost,但好像无法满足要求,

本文只记录使用 TabLayout + Fragment  和 android 自带的 BottomNavigationView + Fragment 来实现

由于测试的时候使用的是一个工程,所以看起来可能有点乱,但是里面的工程目录没有变化,变化的只是代码部分

需要新建的部分如下:

新建一个工程,如果是在做项目,不要在原项目中进行操作,以免损坏原项目,

通用部分,Fragment部分,(xml和对应的java代码),Androidstudio中会自带代码,不需要改动里面的代码

添加对应的包依赖

menu是 BottomNavigationView + Fragment需要的,如果工程中不使用自带的新建的话,可以不需要建立这一部分,

接下来先介绍 TabLayout + Fragment :

主页面的布局代码,

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schemas.android.com/apk/res-auto"
4 android:orientation="vertical"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent">
7
8 <FrameLayout
9 android:id="@+id/home_container"
10 android:layout_width="match_parent"
11 android:layout_height="0dp"
12 android:layout_weight="1"
13 >
14 </FrameLayout>
15
16 <View android:layout_width="match_parent"
17 android:layout_height="0.5dp"
18 android:alpha="0.6"
19 android:background="@android:color/darker_gray"
20 />
21 <android.support.design.widget.TabLayout
22 android:id="@+id/bottom_tab_layout"
23 android:layout_width="match_parent"
24 app:tabIndicatorHeight="0dp"
25 app:tabSelectedTextColor="@android:color/black"
26 app:tabTextColor="@android:color/darker_gray"
27 android:layout_height="48dp">
28 <!--app:menu="@menu/tab_menu" 如果需要使用 BottomNavigationView + Fragment 布局的话取消注释,更改id即可-->
29
30 </android.support.design.widget.TabLayout>
31
32 </LinearLayout>
 3 import android.content.Context;
4 import android.support.v4.app.Fragment;
5 import android.view.LayoutInflater;
6 import android.view.View;
7 import android.widget.ImageView;
8 import android.widget.TextView;
9
10 import com.example.tabhostcostom.AttentionFragment;
11 import com.example.tabhostcostom.DiscoveryFragment;
12 import com.example.tabhostcostom.HomeFragment;
13 import com.example.tabhostcostom.ProfileFragment;
14 import com.example.tabhostcostom.R;
15
16 public class DataGenerator {
17
18 public static final int []mTabRes = new int[]{R.mipmap.index_blue_icon, R.mipmap.index_message, R.mipmap.index_publish, R.mipmap.index_ying, R.mipmap.index_me};
19 //public static final int []mTabResPressed = new int[]{R.drawable.ic_tab_strip_icon_feed_selected,R.drawable.ic_tab_strip_icon_category_selected,R.drawable.ic_tab_strip_icon_pgc_selected,R.drawable.ic_tab_strip_icon_profile_selected};
20 public static final String []mTabTitle = new String[]{"首页","发现","", "关注","我的"};
21
22 public static Fragment[] getFragments(String from){
23 Fragment fragments[] = new Fragment[4];
24 fragments[0] = HomeFragment.newInstance(from, "a");
25 fragments[1] = DiscoveryFragment.newInstance(from, "b");
26 fragments[2] = AttentionFragment.newInstance(from, "c");
27 fragments[3] = ProfileFragment.newInstance(from, "d");
28 return fragments;
29 }
30
31 /**
32 * 获取Tab 显示的内容
33 * @param context
34 * @param position
35 * @return
36 */
37 public static View getTabView(Context context, int position){
38 View view = LayoutInflater.from(context).inflate(R.layout.home_tab_content,null);
39 ImageView tabIcon = view.findViewById(R.id.tab_content_image);
40 tabIcon.setImageResource(DataGenerator.mTabRes[position]);
41 TextView tabText = view.findViewById(R.id.tab_content_text);
42 tabText.setText(mTabTitle[position]);
43 return view;
44 }
45 }

以上是一个工具类将所有的数据全部封装在一块,

我也没搞明白为什么需要传两个参数,应该是可以改的吧,自己没有尝试,随便传一个字符串即可,

主函数中也就是主页面的代码

  1 import android.net.Uri;
2 import android.support.annotation.Nullable;
3 import android.support.design.widget.BottomNavigationView;
4 import android.support.annotation.NonNull;
5 import android.support.design.widget.TabLayout;
6 import android.support.v4.app.Fragment;
7 import android.support.v7.app.AppCompatActivity;
8 import android.os.Bundle;
9 import android.view.MenuItem;
10 import android.view.View;
11 import android.widget.ImageView;
12 import android.widget.TextView;
13 import android.widget.Toast;
14
15 import com.example.tabhostcostom.Utils.DataGenerator;
16
17 public class MainActivity extends AppCompatActivity implements HomeFragment.OnFragmentInteractionListener, DiscoveryFragment.OnFragmentInteractionListener ,
18 AttentionFragment.OnFragmentInteractionListener, ProfileFragment.OnFragmentInteractionListener
19 {
20
21 private BottomNavigationView mBottomNavigationView;
22 //private Fragment []mFragments;
23 private TabLayout mTabLayout;
24 private Fragment []mFragmensts;
25
26 @Override
27 protected void onCreate(@Nullable Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 setContentView(R.layout.activity_main);
30
31 mFragmensts = DataGenerator.getFragments("TabLayout Tab");
32
33 initView();
34 }
35
36 private void initView() {
37 mTabLayout = findViewById(R.id.bottom_tab_layout);
38
39 mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
40 @Override
41 public void onTabSelected(TabLayout.Tab tab) {
42 onTabItemSelected(tab.getPosition());
43 // Tab 选中之后,改变各个Tab的状态,如果点击下面的导航栏,需要改变颜色的话取消注释,同时取消掉工具类中的注释即可
44 /*
45 for (int i=0;i<mTabLayout.getTabCount();i++){
46 View view = mTabLayout.getTabAt(i).getCustomView();
47 ImageView icon = (ImageView) view.findViewById(R.id.tab_content_image);
48 TextView text = (TextView) view.findViewById(R.id.tab_content_text);
49
50 if(i == tab.getPosition()){ // 选中状态
51 icon.setImageResource(DataGenerator.mTabResPressed[i]);
52 text.setTextColor(getResources().getColor(android.R.color.black));
53 }else{// 未选中状态
54 icon.setImageResource(DataGenerator.mTabRes[i]);
55 text.setTextColor(getResources().getColor(android.R.color.darker_gray));
56 }
57 }*/
58
59
60 }
61
62 @Override
63 public void onTabUnselected(TabLayout.Tab tab) {
64
65 }
66
67 @Override
68 public void onTabReselected(TabLayout.Tab tab) {
69
70 }
71 });
72
73 for(int i=0;i<5;i++){
74 mTabLayout.addTab(mTabLayout.newTab().setCustomView(DataGenerator.getTabView(this,i)));
75 }
76
77 }
78
79 private void onTabItemSelected(int position){
80 Fragment fragment = null;
81 switch (position){
82 case 0:
83 fragment = mFragmensts[0];
84 break;
85 case 1:
86 fragment = mFragmensts[1];
87 break;
88 case 2:
89 fragment = mFragmensts[2];
90 break;
91 case 3:
92 fragment = mFragmensts[3];
93 break;
94 case 4:
95 fragment = mFragmensts[3];
96 break;
97 }
98 if(fragment!=null) {
99 getSupportFragmentManager().beginTransaction().replace(R.id.home_container,fragment).commit();
100 }
101 }
102
103 @Override
104 public void onFragmentInteraction(Uri uri) {
105 Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
106 }
107 }

这是 TabLayout + Fragment 全部代码

下面介绍 BottomNavigationView + Fragment 实现,带有动画效果,点击的那个会自动将其他的挤出去,所以建议使用上面的布局方式,当然,需要动画效果的布局也可以

需要 menu 菜单

 1 <?xml version="1.0" encoding="utf-8"?>
2 <menu xmlns:android="http://schemas.android.com/apk/res/android">
3 <item
4 android:id="@+id/tab_menu_home"
5 android:icon="@mipmap/index_blue_icon"
6 android:title="首页"
7 />
8 <item
9 android:id="@+id/tab_menu_discovery"
10 android:icon="@mipmap/index_message"
11 android:title="发现"
12 />
13 <item
14 android:id="@+id/tab_menu_attention1"
15 android:icon="@mipmap/index_publish"
16 android:title=""
17 />
18 <item
19 android:id="@+id/tab_menu_profile"
20 android:icon="@mipmap/index_ying"
21 android:title="Ying"
22 />
23 <item
24 android:id="@+id/tab_menu_attention"
25 android:icon="@mipmap/index_me"
26 android:title="我"
27 />
28
29 </menu>

主函数中代码:

package com.example.tabhostcostom;

import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; import com.example.tabhostcostom.Utils.DataGenerator; public class MainActivity extends AppCompatActivity implements HomeFragment.OnFragmentInteractionListener, DiscoveryFragment.OnFragmentInteractionListener ,
AttentionFragment.OnFragmentInteractionListener, ProfileFragment.OnFragmentInteractionListener
{ private BottomNavigationView mBottomNavigationView;
private Fragment []mFragments;
private TabLayout mTabLayout;
private Fragment []mFragmensts; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mFragments = DataGenerator.getFragments("TabLayout Tab"); initView();
} private void initView() {
mBottomNavigationView = findViewById(R.id.bottom_navigation_view);
//mBottomNavigationView.getMaxItemCount() mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
onTabItemSelected(item.getItemId());
return true;
}
}); // 由于第一次进来没有回调onNavigationItemSelected,因此需要手动调用一下切换状态的方法
onTabItemSelected(R.id.tab_menu_home);
} private void onTabItemSelected(int id){
Fragment fragment = null;
switch (id){
case R.id.tab_menu_home:
fragment = mFragments[0];
break;
case R.id.tab_menu_discovery:
fragment = mFragments[1];
break; case R.id.tab_menu_attention:
fragment = mFragments[2];
break;
case R.id.tab_menu_profile:
fragment = mFragments[3];
break;
}
if(fragment!=null) {
getSupportFragmentManager().beginTransaction().replace(R.id.home_container,fragment).commit();
}
} @Override
public void onFragmentInteraction(Uri uri) {
Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
}
}

大致就这么多,因为是将两个布局杂糅在一块,如果有问题可以在博客后面进行留言,

android底部导航栏小结的更多相关文章

  1. Android底部导航栏——FrameLayout + RadioGroup

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6285881.html Android底部导航栏有多种实现方式,本文详细介绍FrameLayout ...

  2. Android底部导航栏创建——ViewPager + RadioGroup

    原创文章,引用请注明出处:http://www.cnblogs.com/baipengzhan/p/6270201.html Android底部导航栏有多种实现方式,本文详解其中的ViewPager ...

  3. Android底部导航栏

    Android底部导航栏 今天简单写了一个底部导航栏,封装了一个库,用法比较简单 效果图 Github地址:https://github.com/kongqw/KqwBottomNavigation ...

  4. Android底部导航栏(可滑动)----TabLayout+viewPager

    [TabLayout] ①TabLayout是选项卡,在屏幕空间有限的情况下,对不同的空间进行分组.属于android support design,更多的用于新闻上,如果放在底部也可做底部导航栏 ② ...

  5. Android 底部导航栏实现一 Fragment-replace

    [效果](这里下载的软件收费的试用有水印) [推荐]这里推荐一个图标网http://iconfont.cn/.以上图标来自此图标网 [项目结构] [步骤] ①创建布局文件,写底部导航栏 <?xm ...

  6. android底部导航栏实现

    第一种用radiobutton实现 https://wizardforcel.gitbooks.io/w3school-android/content/75.html 布局文件,使用radiogrou ...

  7. Android 底部导航栏的xml

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  8. Android 五种方式实现Android底部导航栏

    https://segmentfault.com/a/1190000007697941

  9. Android应用底部导航栏(选项卡)实例

    现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其 ...

随机推荐

  1. CF1463-C. Busy Robot

    题意: 你有一个机器人,这个机器人在一维坐标轴上移动.你可以给这个机器人下达指令,指令的形式为 \(t_i, x_i\) ,意味着机器人在第\(t_i\)秒的时候获得一条指令,此时这个机器人以\(1/ ...

  2. woj1013 Barcelet 字符串 woj1014 Doraemon's Flashlight 几何

    title: woj1013 Barcelet 字符串 date: 2020-03-18 18:00:00 categories: acm tags: [acm,字符串,woj] 字符串,字典序. 1 ...

  3. C++的memset

    1. 需要的头文件 C中为<memory.h> 或 <string.h> C++中为<cstring> void * memset ( void * ptr, in ...

  4. 秋招C++面试相关总结索引

    C++相关 C++ part1 C++ part2 C++ part3 C++ part4 C++ part5 C++ part6 C++ part6.5 C++ part7 C++ part8 C+ ...

  5. 调用其他文件__name__=='__main__'下代码

    如何调用其他文件__name__=='__main__'下代码 使用os.system()或者subprocess.run()执行该文件,用这种方法相当于直接创建了一个子进程,新调用的py不影响当前调 ...

  6. 多线程(二)多线程的基本原理+Synchronized

    由一个问题引发的思考 线程的合理使用能够提升程序的处理性能,主要有两个方面, 第一个是能够利用多核 cpu 以及超线程技术来实现线程的并行执行: 第二个是线程的异步化执行相比于同步执行来说,异步执行能 ...

  7. PEP 8 & Style Guide

    PEP 8 & Style Guide Style Guide for Python Code https://www.python.org/dev/peps/pep-0008/ PEP Py ...

  8. holy shit CSDN

    holy shit CSDN 垃圾 CSDN 到处都是垃圾文章, 无人子弟 到处都是垃圾广告,看的恶心 毫无底线,窃取别人的知识成果,毫无版权意识 垃圾爬虫,垃圾小号 ...等等 Google Sea ...

  9. 一文搞懂 js 中的各种 for 循环的不同之处

    一文搞懂 js 中的各种 for 循环的不同之处 See the Pen for...in vs for...of by xgqfrms (@xgqfrms) on CodePen. for &quo ...

  10. vue & watch props

    vue & watch props bug OK watch: { // props // chatObj: () => { // // bug // log(`this.chatObj ...