我用的EasyNavigation库 https://github.com/forvv231/EasyNavigation.
- 根目录 build.gradle 的repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- 添加引入dependency
implementation 'com.github.Vincent7Wong:EasyNavigation:1.5.0'
implementation 'com.github.Vincent7Wong:EasyNavigation:2.0.5'
EasyNavigation的文档
navigationBar.defaultSetting()
.titleItems(tabText)
.normalIconItems(normalIcon)
.selectIconItems(selectIcon)
.fragmentList(fragments)
.fragmentManager(getSupportFragmentManager())
.iconSize(20)
.tabTextSize(10)
.tabTextTop(2)
.normalTextColor(Color.parseColor("#666666"))
.selectTextColor(Color.parseColor("#333333"))
.scaleType(ImageView.ScaleType.CENTER_INSIDE)
.navigationBackground(Color.parseColor("#80000000"))
.setOnTabClickListener(new EasyNavigationBar.OnTabClickListener() {
@Override
public boolean onTabSelectEvent(View view, int position) {
return false;
}
@Override
public boolean onTabReSelectEvent(View view, int position) {
return false;
}
})
.smoothScroll(false)
.canScroll(true)
.mode(EasyNavigationBar.NavigationMode.MODE_ADD)
.centerTextStr("发现")
.centerImageRes(R.mipmap.add_image)
.centerIconSize(36)
.centerLayoutHeight(100)
.navigationHeight(60)
.lineHeight(10)
.lineColor(Color.parseColor("#ff0000"))
.centerLayoutRule(EasyNavigationBar.RULE_BOTTOM)
.centerLayoutBottomMargin(10)
.hasPadding(true)
.hintPointLeft(-3)
.hintPointTop(-3)
.hintPointSize(6)
.msgPointLeft(-10)
.msgPointTop(-10)
.msgPointTextSize(9)
.msgPointSize(18)
.centerAlignBottom(true)
.centerTextTopMargin(50)
.centerTextSize(15)
.centerNormalTextColor(Color.parseColor("#ff0000"))
.centerSelectTextColor(Color.parseColor("#00ff00"))
.setMsgPointColor(Color.BLUE)
.setMsgPointMoreRadius(5)
.setMsgPointMoreWidth(50)
.setMsgPointMoreHeight(40)
.textSizeType(EasyNavigationBar.TextSizeType.TYPE_DP)
.setOnTabLoadListener(new EasyNavigationBar.OnTabLoadListener() {
@Override
public void onTabLoadCompleteEvent() {
navigationBar.setMsgPointCount(0, 7);
navigationBar.setMsgPointCount(1, 109);
navigationBar.setHintPoint(4, true);
}
})
.build();
做准备工作: 导入图片 https://bls.lanzoui.com/isPS4wqzp7a.到mipmap-xhdpi
activity_main.xml
<com.next.easynavigation.view.EasyNavigationBar
android:id="@+id/navigationBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:Easy_centerIconSize="50dp">
</com.next.easynavigation.view.EasyNavigationBar>
再创建四个窗口ABCD fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#20616161"
android:gravity="center"
android:text="A"
android:textColor="#ffffff"
android:textSize="100sp" />
</android.support.constraint.ConstraintLayout>
fragment_b.xml
<?xml ve<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#20616161"
android:gravity="center"
android:text="B"
android:textColor="#ffffff"
android:textSize="100sp" />
</android.support.constraint.ConstraintLayout>rsion="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#20616161" android:gravity="center" android:text="B" android:textColor="#ffffff" android:textSize="100sp" /></android.support.constraint.ConstraintLayout>
fragment_c.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#20616161"
android:gravity="center"
android:text="C"
android:textColor="#ffffff"
android:textSize="100sp" />
</android.support.constraint.ConstraintLayout>
fragment_d.xml
<?xml v<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#20616161"
android:gravity="center"
android:text="D"
android:textColor="#ffffff"
android:textSize="100sp" />
</android.support.constraint.ConstraintLayout>ersion="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#20616161" android:gravity="center" android:text="D" android:textColor="#ffffff" android:textSize="100sp" /></android.support.constraint.ConstraintLayout>
四个Fragment .AFragment.java
package com.mycompany.batDamo.UI;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mycompany.batDamo.R;
public class AFragment extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, null);
return view;
}
}
BFragment.java
package com.mycompany.batDamo.UI;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mycompany.batDamo.R;
public class BFragment extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container,false);
return view;
}
}
CFragment.java
package compackage com.mycompany.batDamo.UI;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.mycompany.batDamo.R;
public class CFragment extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_c, container,false);
return view;
}
public void showToast(String str) {
Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
}
}
.mycompany.batDamo.UI;
import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Toast;import com.mycompany.batDamo.R;
public class CFragment extends android.support.v4.app.Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_c, container,false); return view; }
DFragment.java
package com.mycompany.batDamo.UI;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.mycompany.batDamo.R;
public class DFragment extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_d, container,false);
return view;
}
public void showToast(String str) {
Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
}
}
最后MainActivity.java
package com.mycompany.batDamo;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import com.mycompany.batDamo.UI.AFragment;
import com.mycompany.batDamo.UI.BFragment;
import com.mycompany.batDamo.UI.CFragment;
import com.mycompany.batDamo.UI.DFragment;
import com.next.easynavigation.view.EasyNavigationBar;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
public class MainActivity extends AppCompatActivity
{
private EasyNavigationBar navigationBar;
private String[] tabText = {"首页", "发现", "消息", "我的"};
private int[] normalIcon = {R.mipmap.index, R.mipmap.find, R.mipmap.message, R.mipmap.me};
private int[] selectIcon = {R.mipmap.index1, R.mipmap.find1, R.mipmap.message1, R.mipmap.me1};
private List<Fragment> fragments = new ArrayList<>();
private Handler mHandler = new Handler();
private boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationBar = findViewById(R.id.navigationBar);
fragments.add(new AFragment());
fragments.add(new BFragment());
fragments.add(new CFragment());
fragments.add(new DFragment());
navigationBar.titleItems(tabText)
.normalIconItems(normalIcon)
.selectIconItems(selectIcon)
.fragmentList(fragments)
.centerImageRes(R.mipmap.add_image)
.centerTextStr("发布")
.anim(null)
.centerLayoutRule(EasyNavigationBar.RULE_BOTTOM)
.centerLayoutBottomMargin(0)
.centerAlignBottom(true)
.fragmentManager(getSupportFragmentManager())
.setOnTabClickListener(new EasyNavigationBar.OnTabClickListener() {
@Override
public boolean onTabSelectEvent(View view, int position) {
return false;
}
@Override
public boolean onTabReSelectEvent(View view, int position) {
return false;
}
})
.setOnCenterTabClickListener(new EasyNavigationBar.OnCenterTabSelectListener() {
@Override
public boolean onCenterTabSelectEvent(View view) {
mHandler.post(new Runnable() {
@Override
public void run() {
if (flag) {
navigationBar.getCenterImage().animate().rotation(45).setDuration(400);
} else {
navigationBar.getCenterImage().animate().rotation(0).setDuration(400);
}
flag = !flag;
}
});
return false;
}
})
.canScroll(true)
.mode(EasyNavigationBar.NavigationMode.MODE_ADD)
.build();
}
}
完整项目地址链接: https://bls.lanzoui.com/isPS4wqzp7a.
|