├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── ic_me_normal.png │ │ │ │ ├── ic_me_select.png │ │ │ │ ├── ic_home_normal.png │ │ │ │ ├── ic_home_selected.png │ │ │ │ ├── ic_managemoney_normal.png │ │ │ │ └── ic_managemoney_select.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ ├── fragment_my.xml │ │ │ │ └── activity_main.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── chensen │ │ │ ├── MyFragment.java │ │ │ └── MainActivity.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── chensen │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── chensen │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── NavMenuLayout-Library ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ └── attrs.xml │ │ │ ├── drawable-xhdpi │ │ │ │ ├── ic_home_normal.png │ │ │ │ └── ic_home_selected.png │ │ │ ├── drawable │ │ │ │ ├── point_red.xml │ │ │ │ └── bg_corner_red_5.xml │ │ │ └── layout │ │ │ │ └── item_menu.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── git │ │ │ └── navmenu │ │ │ ├── PixelUtil.java │ │ │ ├── MenuItem.java │ │ │ └── NavMenuLayout.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── git │ │ │ └── navmenulayout │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── git │ │ └── navmenulayout │ │ └── ExampleInstrumentedTest.java ├── build.gradle └── proguard-rules.pro ├── settings.gradle ├── Screenshots ├── pic1.png ├── pic2.png ├── pic3.png └── pic4.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── gradlew.bat ├── gradlew └── README.md /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /NavMenuLayout-Library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | include ':NavMenuLayout-Library' 3 | -------------------------------------------------------------------------------- /Screenshots/pic1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/Screenshots/pic1.png -------------------------------------------------------------------------------- /Screenshots/pic2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/Screenshots/pic2.png -------------------------------------------------------------------------------- /Screenshots/pic3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/Screenshots/pic3.png -------------------------------------------------------------------------------- /Screenshots/pic4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/Screenshots/pic4.png -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Android-NavMenu-master 3 | 4 | -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | navmenu 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_me_normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/app/src/main/res/mipmap-xhdpi/ic_me_normal.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_me_select.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/app/src/main/res/mipmap-xhdpi/ic_me_select.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_home_normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/app/src/main/res/mipmap-xhdpi/ic_home_normal.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_home_selected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/app/src/main/res/mipmap-xhdpi/ic_home_selected.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_managemoney_normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/app/src/main/res/mipmap-xhdpi/ic_managemoney_normal.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_managemoney_select.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/app/src/main/res/mipmap-xhdpi/ic_managemoney_select.png -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/main/res/drawable-xhdpi/ic_home_normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/NavMenuLayout-Library/src/main/res/drawable-xhdpi/ic_home_normal.png -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/main/res/drawable-xhdpi/ic_home_selected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smashinggit/Android-NavMenuLayout/HEAD/NavMenuLayout-Library/src/main/res/drawable-xhdpi/ic_home_selected.png -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Mar 20 19:53:14 CST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip 7 | -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/main/res/drawable/point_red.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 9 | -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/main/res/drawable/bg_corner_red_5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/chensen/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.chensen; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/test/java/com/git/navmenulayout/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.git.navmenulayout; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_my.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/chensen/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.chensen; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.chensen", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/androidTest/java/com/git/navmenulayout/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.git.navmenulayout; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.git.navmenulayout.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NavMenuLayout-Library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | 7 | defaultConfig { 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile fileTree(dir: 'libs', include: ['*.jar']) 26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 27 | exclude group: 'com.android.support', module: 'support-annotations' 28 | }) 29 | compile 'com.android.support:appcompat-v7:25.2.0' 30 | testCompile 'junit:junit:4.12' 31 | } 32 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.chensen" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.2.0' 28 | testCompile 'junit:junit:4.12' 29 | compile project(':NavMenuLayout-Library') 30 | } 31 | -------------------------------------------------------------------------------- /NavMenuLayout-Library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 16 | 17 | 23 | 24 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/chensen/MyFragment.java: -------------------------------------------------------------------------------- 1 | package com.chensen; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.TextView; 10 | 11 | /** 12 | * author: chensen 13 | * date: 2017年03月18日18:10 14 | * desc: 15 | */ 16 | 17 | public class MyFragment extends Fragment { 18 | 19 | int page; 20 | TextView textView; 21 | private String[] textRes = {"首页", "理财", "个人中心"}; 22 | 23 | @Nullable 24 | @Override 25 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 26 | View view = inflater.inflate(R.layout.fragment_my, container, false); 27 | textView = (TextView) view.findViewById(R.id.text); 28 | switch (page) { 29 | case 0: 30 | textView.setText(textRes[page]); 31 | break; 32 | case 1: 33 | textView.setText(textRes[page]); 34 | break; 35 | case 2: 36 | textView.setText(textRes[page]); 37 | break; 38 | } 39 | return view; 40 | } 41 | 42 | 43 | public static MyFragment getInstance(int page) { 44 | MyFragment myFragment = new MyFragment(); 45 | myFragment.page = page; 46 | return myFragment; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/main/java/com/git/navmenu/PixelUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Yahoo Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.git.navmenu; 18 | 19 | import android.content.Context; 20 | import android.util.DisplayMetrics; 21 | 22 | /** 23 | * Util class for converting between dp, px and other magical pixel units 24 | */ 25 | public class PixelUtil { 26 | 27 | private PixelUtil() { 28 | } 29 | 30 | public static int dpToPx(Context context, int dp) { 31 | int px = Math.round(dp * getPixelScaleFactor(context)); 32 | return px; 33 | } 34 | 35 | public static int pxToDp(Context context, int px) { 36 | int dp = Math.round(px / getPixelScaleFactor(context)); 37 | return dp; 38 | } 39 | 40 | private static float getPixelScaleFactor(Context context) { 41 | DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); 42 | return (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/main/res/layout/item_menu.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 20 | 21 | 34 | 35 | 47 | 48 | 58 | 59 | 60 | 61 | 69 | 70 | -------------------------------------------------------------------------------- /app/src/main/java/com/chensen/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.chensen; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.Fragment; 5 | import android.support.v4.app.FragmentManager; 6 | import android.support.v4.app.FragmentPagerAdapter; 7 | import android.support.v4.view.ViewPager; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.widget.Toast; 10 | 11 | import com.git.navmenu.NavMenuLayout; 12 | 13 | import java.util.ArrayList; 14 | 15 | 16 | public class MainActivity extends AppCompatActivity { 17 | ViewPager mViewPager; 18 | NavMenuLayout mNavMenuLayout; 19 | ArrayList listFragment; 20 | 21 | private int[] iconRes = {R.mipmap.ic_home_normal, R.mipmap.ic_managemoney_normal, R.mipmap.ic_me_normal}; 22 | private int[] iconResSelected = {R.mipmap.ic_home_selected, R.mipmap.ic_managemoney_select, R.mipmap.ic_me_select}; 23 | private String[] textRes = {"首页", "理财", "个人中心"}; 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_main); 29 | mNavMenuLayout = (NavMenuLayout) findViewById(R.id.nav_layout); 30 | mViewPager = (ViewPager) findViewById(R.id.viewpager); 31 | 32 | mNavMenuLayout.setIconRes(iconRes)//设置未选中图标 33 | .setIconResSelected(iconResSelected)//设置选中图标 34 | .setTextRes(textRes)//设置文字 35 | // .setIconSize(60, 60)//设置图标大小 36 | // .setIconSize(0, 60,60)//设置指定位置的图标 37 | // .setTextSize(20)//设置文字大小 38 | // .setTextSize(0, 20)//指定位置的文字大小 39 | // .setTextColor(Color.GRAY)//未选中状态下文字颜色 40 | // .setTextColorSelected(Color.RED)//选中状态下文字颜色 41 | // .setTextColor(0, Color.YELLOW)//设置指定位置下文字颜色 42 | // .setTextColorSelected(0, Color.BLUE)//设置指定位置下选中状态文字颜色 43 | // .setMarginTop(PixelUtil.dpToPx(MainActivity.this, 5))// 44 | // .setMarginTop(1, PixelUtil.dpToPx(MainActivity.this, 10)) 45 | .setMsg(0, "99+")//设置显示消息 46 | .setMsg(1, "NEW")//设置显示消息 47 | .showRedPoint(2)//设置显示红点 48 | .hideMsg(0)//隐藏消息 49 | .hideMsg(1)//隐藏消息 50 | .hideRedPoint(2)//隐藏红点 51 | .setSelected(0);//设置选中的位置 52 | 53 | //选中的点击事件 54 | mNavMenuLayout.setOnItemSelectedListener(new NavMenuLayout.OnItemSelectedListener() { 55 | @Override 56 | public void onItemSelected(int position) { 57 | mViewPager.setCurrentItem(position);//选中后切换viwepager 58 | Toast.makeText(MainActivity.this, "选中了-> " + textRes[position], Toast.LENGTH_SHORT).show(); 59 | } 60 | }); 61 | //已选中状态下的点击事件 62 | mNavMenuLayout.setOnItemReSelectedListener(new NavMenuLayout.OnItemReSelectedListener() { 63 | @Override 64 | public void onItemReSelected(int position) { 65 | Toast.makeText(MainActivity.this, "重复选中了-> " + textRes[position], Toast.LENGTH_SHORT).show(); 66 | } 67 | }); 68 | 69 | 70 | listFragment = new ArrayList<>(); 71 | listFragment.add(MyFragment.getInstance(0)); 72 | listFragment.add(MyFragment.getInstance(1)); 73 | listFragment.add(MyFragment.getInstance(2)); 74 | 75 | mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager())); 76 | mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 77 | @Override 78 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 79 | 80 | } 81 | 82 | @Override 83 | public void onPageSelected(int position) { 84 | mNavMenuLayout.setSelected(position); 85 | } 86 | 87 | @Override 88 | public void onPageScrollStateChanged(int state) { 89 | 90 | } 91 | }); 92 | } 93 | 94 | class MyAdapter extends FragmentPagerAdapter { 95 | 96 | public MyAdapter(FragmentManager fm) { 97 | super(fm); 98 | } 99 | 100 | @Override 101 | public Fragment getItem(int position) { 102 | return listFragment.get(position); 103 | } 104 | 105 | @Override 106 | public int getCount() { 107 | return listFragment.size(); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android-NavMenu-master 2 | 3 | ### 一个底部导航栏, 实现了显示未读消息数, 显示红点等效果的封装。 4 | 5 | # 添加依赖 6 | 7 | #### 1. 在项目根目录的 build.gradle 中添加 8 | 9 | allprojects { 10 | repositories { 11 | ... 12 | maven { url 'https://jitpack.io' } 13 | } 14 | } 15 | 16 | #### 2. 在项目的 build.gradle 中添加 17 | 18 | compile 'com.github.smashinggit:Android-NavMenuLayout:v1.0.0' 19 | 20 | # 如何使用 21 | 22 | 23 | ## 1. 普通用法 24 | 25 | #### 第一步, 在xml文件中添加控件 26 | 27 | 在xml中的跟布局添加 xmxmlns:app="http://schemas.android.com/apk/res-auto" 28 | 29 | 30 | 35 | 36 | 37 | 注: 38 | 39 |  app:menuCount="3" 属性是必须要添加的,代表底部菜单的数量,此数值必须要与 Activity 中设置的图片资源,文字资源的数量一致, 40 | 否则无法正常显示 41 |        42 |         43 | 44 | 45 | #### 第二步, 在Activity中设置数据 46 | 47 | private int[] iconRes = {R.mipmap.ic_home_normal, R.mipmap.ic_managemoney_normal, R.mipmap.ic_me_normal}; 48 | private int[] iconResSelected = {R.mipmap.ic_home_selected, R.mipmap.ic_managemoney_select, R.mipmap.ic_me_select}; 49 | private String[] textRes = {"首页", "理财", "个人中心"}; 50 | 51 | 52 | mNavMenuLayout.setIconRes(iconRes)//设置未选中图标 53 | .setIconResSelected(iconResSelected)//设置选中图标 54 | .setTextRes(textRes)//设置文字 55 | .setSelected(0);//设置选中的位置 56 | 57 | 注: 58 | 59 | setSelected(int position)方法一定要放在最后调用,因为在该方法中实现了刷新页面的功能,其他的几个方法都只是进行了赋值操作, 60 | 并没有刷新页面; 61 | 62 | #### 效果图 63 | 64 | 65 | ![](https://github.com/smashinggit/Android-NavMenuLayout/blob/master/Screenshots/pic1.png) 66 | 67 | 68 | 69 | ## 2. 带消息提示的用法 70 | 71 | #### 显示消息或红点提示 72 | 73 |    mNavMenuLayout.setIconRes(iconRes)//设置未选中图标 74 | .setIconResSelected(iconResSelected)//设置选中图标 75 | .setTextRes(textRes)//设置文字 76 | .setMsg(0, "99+")//设置显示消息 77 | .setMsg(1, "NEW")//设置显示消息 78 | .showRedPoint(2)//设置显示红点 79 | .setSelected(0);//设置选中的位置 80 | 81 | 注: 82 | 83 | setMsg(int position,String msg) 方法,第一个参数代表底部菜单的位置,第二个参数代表显示的内容 84 | showRedPoint(int position)方法,参数代表底部菜单的位置 85 | 86 | #### 取消消息或红点提示 87 | 88 | mNavMenuLayout .hideMsg(0)//隐藏消息 89 | .hideMsg(1)//隐藏消息 90 | .hideRedPoint(2)//隐藏红点 91 | 92 | 注: 93 | 94 | hideMsg(int position)参数代表底部菜单位置 95 | hideRedPoint(int position)同上 96 | 97 | #### 效果图 98 | 99 | ![](https://github.com/smashinggit/Android-NavMenuLayout/blob/master/Screenshots/pic2.png) 100 | 101 | ## 3. 点击事件 102 | 103 | //选中的点击事件 104 | mNavMenuLayout.setOnItemSelectedListener(new NavMenuLayout.OnItemSelectedListener() { 105 | @Override 106 | public void onItemSelected(int position) { 107 | mViewPager.setCurrentItem(position);//选中后切换viwepager 108 | Toast.makeText(MainActivity.this, "选中了-> " + textRes[position], Toast.LENGTH_SHORT).show(); 109 | } 110 | }); 111 | //已选中状态下的点击事件 112 | mNavMenuLayout.setOnItemReSelectedListener(new NavMenuLayout.OnItemReSelectedListener() { 113 | @Override 114 | public void onItemReSelected(int position) { 115 | Toast.makeText(MainActivity.this, "重复选中了-> " + textRes[position], Toast.LENGTH_SHORT).show(); 116 | } 117 | }); 118 | 119 | 注: 120 | 121 | 点击事件分为2种: 122 | setOnItemSelectedListener 代表的是当一个 item 由 未选中状态 变成 选中状态 时的回调 123 | setOnItemReSelectedListener 代表的是当一个 item 已经是选中状态 并且 又点击该 item 时的回调 124 | 125 | #### 效果图 126 | 127 | ![](https://github.com/smashinggit/Android-NavMenuLayout/blob/master/Screenshots/pic3.png) 128 | ![](https://github.com/smashinggit/Android-NavMenuLayout/blob/master/Screenshots/pic4.png) 129 | 130 | 131 | ## 4. 更多个性化定制 132 | 133 | mNavMenuLayout.setIconRes(iconRes)//设置未选中图标 134 | .setIconResSelected(iconResSelected)//设置选中图标 135 | .setTextRes(textRes)//设置文字 136 | .setIconSize(60, 60)//设置图标大小 137 | .setIconSize(0, 70,70) 138 | .setTextSize(20)//设置文字大小 139 | .setTextSize(0, 20) 140 | .setTextColor(Color.GRAY)//未选中状态下文字颜色 141 | .setTextColorSelected(Color.RED)//选中状态下文字颜色 142 | .setBackColor(Color.WHITE)//设置背景色 143 | .setBackColor(2,Color.RED) 144 | .setMarginTop(PixelUtil.dpToPx(MainActivity.this, 5))//文字和图标直接的距离,默认为5dp 145 | .setMarginTop(1, PixelUtil.dpToPx(MainActivity.this, 10)) 146 | .setSelected(0);//设置选中的位置 147 | 148 | 注: 149 | 150 | 以设置图标大小为例,有两个重载方法, 151 | .setIconSize(60, 60) 表示将图标宽高设为 60 * 60,此效果针对于底部所有的item,即底部所有图标大小都会是 60 * 60 152 | .setIconSize(0, 70,70) 第一个参数代表位置,表示针对将底部哪一个item的图标进行设置 153 | 154 | ## 报错处理 155 | 156 | 假如在使用过程中出现了无法显示或者是显示效果不正确等问题,请查看Log,里面会有报错提示。 157 | 158 | 例如: 159 | 如果没有在xml中声明  app:menuCount="" 属性 或者是 属性的值 小于1 ,就会在LogCat中看到这样的提示: 160 | 161 | 162 | 03-18 22:37:58.405 2518-2518/? E/NavMenuLayout: the menuCount mast greater than 0 163 | 03-18 22:37:58.405 2518-2518/? E/NavMenuLayout: the iconRes length is not equals count 164 | 03-18 22:37:58.405 2518-2518/? E/NavMenuLayout: the iconResSelected length is not equals count 165 | 03-18 22:37:58.405 2518-2518/? E/NavMenuLayout: the textRes length is not equals count 166 | 167 | ## 联系方式 168 | 169 | 如果在使用过程中有什么问题或者是意见,请联系我。谢谢大家! 170 | 171 | qq: 2670469507 172 | 邮箱:smashing_chen@163.com 173 | 174 | -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/main/java/com/git/navmenu/MenuItem.java: -------------------------------------------------------------------------------- 1 | package com.git.navmenu; 2 | 3 | import android.content.Context; 4 | import android.graphics.Color; 5 | import android.support.annotation.DrawableRes; 6 | import android.util.AttributeSet; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.widget.FrameLayout; 10 | import android.widget.ImageView; 11 | import android.widget.RelativeLayout; 12 | import android.widget.TextView; 13 | 14 | import com.git.navmenulayout.R; 15 | 16 | 17 | /** 18 | * author: chensen 19 | * date: 2017年03月17日10:19 20 | * desc: 21 | */ 22 | 23 | public class MenuItem extends FrameLayout { 24 | 25 | private int mIconWidth, mIconHeight;//图片大小 26 | private int mIconRes = 0;//图片资源 27 | private int mIconResSelected = 0;//选中图片资源 28 | 29 | private int mTextColor = Color.GRAY;//未选中的颜色(默认灰色) 30 | private int mTextColorSelected = Color.parseColor("#db4f55");//选中的颜色(默认红色) 31 | private int mTextSize = 12;//字体大小 32 | private String mText = "";//文字 33 | 34 | 35 | private View mRootView; 36 | private ImageView ivIcon;//图标 37 | private TextView tvText;//文字 38 | private TextView tvUnReadNum;//未读 39 | private TextView tvRedPoiont;//红点 40 | 41 | private boolean isSelected = false;//是否选中 42 | private int marginTop = 0;//文字和图标的距离 43 | 44 | 45 | public MenuItem(Context context) { 46 | this(context, null); 47 | } 48 | 49 | public MenuItem(Context context, AttributeSet attrs) { 50 | this(context, attrs, 0); 51 | } 52 | 53 | public MenuItem(Context context, AttributeSet attrs, int defStyleAttr) { 54 | super(context, attrs, defStyleAttr); 55 | intiView(context); 56 | } 57 | 58 | private void intiView(Context context) { 59 | marginTop = PixelUtil.dpToPx(context, 5); 60 | //加载布局 61 | mRootView = LayoutInflater.from(context).inflate(R.layout.item_menu, this, false); 62 | 63 | ivIcon = (ImageView) mRootView.findViewById(R.id.iv_icon); 64 | tvText = (TextView) mRootView.findViewById(R.id.tv_text); 65 | tvUnReadNum = (TextView) mRootView.findViewById(R.id.tv_unred_num); 66 | tvRedPoiont = (TextView) mRootView.findViewById(R.id.tv_point); 67 | addView(mRootView); 68 | } 69 | 70 | /** 71 | * 刷新状态 72 | */ 73 | private void refreshState() { 74 | if (isSelected) { 75 | if (mIconResSelected != 0 && mTextColorSelected != 0) { 76 | ivIcon.setImageResource(mIconResSelected); 77 | tvText.setTextColor(mTextColorSelected); 78 | } 79 | 80 | } else { 81 | if (mIconRes != 0 && mIconResSelected != 0) { 82 | ivIcon.setImageResource(mIconRes); 83 | tvText.setTextColor(mTextColor); 84 | } 85 | } 86 | if (mTextSize > 0) { 87 | tvText.setTextSize(mTextSize); 88 | } 89 | tvText.setText(mText); 90 | 91 | RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tvText.getLayoutParams(); 92 | layoutParams.topMargin = marginTop; 93 | tvText.setLayoutParams(layoutParams); 94 | } 95 | 96 | /** 97 | * 设置选中状态 98 | * 99 | * @param isSelected 100 | */ 101 | public void setSelected(boolean isSelected) { 102 | this.isSelected = isSelected; 103 | refreshState(); 104 | } 105 | 106 | 107 | /** 108 | * 设置图标 109 | * 110 | * @param res 111 | */ 112 | public void setIcon(@DrawableRes int res) { 113 | this.mIconRes = res; 114 | } 115 | 116 | /** 117 | * 设置选中图标 118 | * 119 | * @param res 120 | */ 121 | public void setIconSelected(@DrawableRes int res) { 122 | this.mIconResSelected = res; 123 | } 124 | 125 | /** 126 | * 设置图标大小 127 | * 128 | * @param width 129 | * @param height 130 | */ 131 | public void setIconSize(int width, int height) { 132 | this.mIconWidth = width; 133 | this.mIconHeight = height; 134 | LayoutParams params = (LayoutParams) ivIcon.getLayoutParams(); 135 | params.width = mIconWidth; 136 | params.height = mIconHeight; 137 | ivIcon.setLayoutParams(params); 138 | } 139 | 140 | 141 | /** 142 | * 设置文字 143 | * 144 | * @param text 145 | */ 146 | public void setText(String text) { 147 | this.mText = text; 148 | } 149 | 150 | /** 151 | * 设置文字大小 152 | * 153 | * @param size 154 | */ 155 | public void setTextSize(int size) { 156 | this.mTextSize = size; 157 | } 158 | 159 | /** 160 | * 文字颜色 161 | * 162 | * @param color 163 | */ 164 | 165 | public void setTextColor(int color) { 166 | this.mTextColor = color; 167 | } 168 | 169 | 170 | /** 171 | * 选中的文字颜色 172 | * 173 | * @param color 174 | */ 175 | public void setSelectedTextColor(int color) { 176 | this.mTextColorSelected = color; 177 | } 178 | 179 | 180 | /** 181 | * 显示提示 182 | * 183 | * @param msg 184 | */ 185 | public void setMsg(String msg) { 186 | tvUnReadNum.setVisibility(VISIBLE); 187 | tvUnReadNum.setText(msg); 188 | tvRedPoiont.setVisibility(GONE); 189 | } 190 | 191 | /** 192 | * 图标和文字之间的距离 193 | * 194 | * @param marginTop 195 | */ 196 | public void setMarginTop(int marginTop) { 197 | this.marginTop = marginTop; 198 | } 199 | 200 | 201 | /** 202 | * 隐藏提示 203 | */ 204 | public void hideMsg() { 205 | tvUnReadNum.setVisibility(GONE); 206 | } 207 | 208 | /** 209 | * 显示红点 210 | * 211 | * @param isShow 212 | */ 213 | public void showRedPoint(boolean isShow) { 214 | tvRedPoiont.setVisibility(VISIBLE); 215 | tvUnReadNum.setVisibility(GONE); 216 | } 217 | 218 | /** 219 | * 隐藏红点 220 | */ 221 | 222 | public void hideRedPoint() { 223 | tvRedPoiont.setVisibility(GONE); 224 | } 225 | 226 | /** 227 | * 隐藏所有提示 228 | */ 229 | public void hideAllTips() { 230 | tvRedPoiont.setVisibility(GONE); 231 | tvUnReadNum.setVisibility(GONE); 232 | } 233 | 234 | 235 | /** 236 | * 获取是否选中 237 | * 238 | * @return 239 | */ 240 | public boolean getIsSelected() { 241 | return isSelected; 242 | } 243 | 244 | 245 | } 246 | -------------------------------------------------------------------------------- /NavMenuLayout-Library/src/main/java/com/git/navmenu/NavMenuLayout.java: -------------------------------------------------------------------------------- 1 | package com.git.navmenu; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Color; 6 | import android.util.AttributeSet; 7 | import android.util.Log; 8 | import android.view.Gravity; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.LinearLayout; 12 | 13 | import com.git.navmenulayout.R; 14 | 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | 19 | /** 20 | * author: chensen 21 | * date: 2017年03月17日10:08 22 | * desc: 底部导航菜单 23 | */ 24 | 25 | public class NavMenuLayout extends LinearLayout { 26 | 27 | private static final String TAG = NavMenuLayout.class.getSimpleName(); 28 | private NavMenuLayout instance; 29 | 30 | private Context mContext; 31 | private List menuList; 32 | private OnItemSelectedListener mOnItemSelectedListener; 33 | private OnItemReSelectedListener mOnItemReSelectedListener; 34 | 35 | private int[] mListIconRes;//未选中图标 36 | private int[] mListIconResSelected;//选中图标 37 | private int mIconWidth, mIconHeight;//图标大小 38 | 39 | private String[] mListText;//的文字 40 | private int mTextSize;//文字大小 41 | private int mTextColor = Color.GRAY;//未选中的颜色(默认灰色) 42 | private int mTextColorSelected = Color.parseColor("#db4f55");//选中的颜色(默认红色) 43 | 44 | private int marginTop = 0;//文字和图标的距离 45 | 46 | private int mCount = 0;//菜单数量 47 | private int mCurrentPosition = 0;//选中的位置 48 | 49 | 50 | public NavMenuLayout(Context context) { 51 | this(context, null); 52 | } 53 | 54 | public NavMenuLayout(Context context, AttributeSet attrs) { 55 | this(context, attrs, 0); 56 | } 57 | 58 | public NavMenuLayout(Context context, AttributeSet attrs, int defStyleAttr) { 59 | super(context, attrs, defStyleAttr); 60 | mContext = context; 61 | marginTop = PixelUtil.dpToPx(context, 5); 62 | init(context, attrs, defStyleAttr); 63 | } 64 | 65 | private void init(Context context, AttributeSet attrs, int defStyleAttr) { 66 | instance = this; 67 | 68 | setOrientation(HORIZONTAL); 69 | TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.NavMenuLayout, defStyleAttr, 0); 70 | if (typedArray != null) { 71 | //菜单数量 72 | mCount = typedArray.getInt(R.styleable.NavMenuLayout_menuCount, 0); 73 | if (mCount > 0) { 74 | initMenu(); 75 | } else { 76 | Log.e(TAG, "the menuCount mast greater than 0"); 77 | } 78 | 79 | typedArray.recycle(); 80 | } 81 | } 82 | 83 | 84 | //初始化布局 85 | private void initMenu() { 86 | menuList = new ArrayList<>(mCount); 87 | for (int i = 0; i < mCount; i++) { 88 | final MenuItem menuItem = new MenuItem(mContext); 89 | LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1); 90 | params.gravity = Gravity.CENTER; 91 | menuItem.setLayoutParams(params); 92 | menuItem.setOnClickListener(new OnClickListener() { 93 | @Override 94 | public void onClick(View v) { 95 | int position = getSelectedPosition(menuItem); 96 | if (position == mCurrentPosition) { //已经是选中状态 97 | NavMenuLayout.this.OnItemReClick(position); 98 | } else { 99 | NavMenuLayout.this.OnItemClick(position); 100 | } 101 | } 102 | }); 103 | menuList.add(menuItem); 104 | addView(menuItem); 105 | } 106 | } 107 | 108 | 109 | public interface OnItemSelectedListener { 110 | void onItemSelected(int position); 111 | } 112 | 113 | public void setOnItemSelectedListener(OnItemSelectedListener listener) { 114 | this.mOnItemSelectedListener = listener; 115 | } 116 | 117 | public interface OnItemReSelectedListener { 118 | void onItemReSelected(int position); 119 | } 120 | 121 | public void setOnItemReSelectedListener(OnItemReSelectedListener listener) { 122 | this.mOnItemReSelectedListener = listener; 123 | } 124 | 125 | private void OnItemClick(int position) { 126 | for (int i = 0; i < mCount; i++) { 127 | if (i == position) { 128 | setSelected(position); 129 | } 130 | } 131 | if (mOnItemSelectedListener != null) { 132 | mOnItemSelectedListener.onItemSelected(position); 133 | } 134 | } 135 | 136 | private void OnItemReClick(int position) { 137 | if (mOnItemReSelectedListener != null) { 138 | mOnItemReSelectedListener.onItemReSelected(position); 139 | } 140 | } 141 | 142 | /** 143 | * 更改选中状态 144 | * 145 | * @param position 146 | */ 147 | public NavMenuLayout setSelected(int position) { 148 | if (position >= 0 && position < mCount) { 149 | mCurrentPosition = position; 150 | refreshState(); 151 | } else { 152 | Log.e(TAG, "setSelected(int position) ---> the position is not correct"); 153 | } 154 | 155 | return instance; 156 | } 157 | 158 | /** 159 | * 刷新状态 160 | */ 161 | public void refreshState() { 162 | for (int i = 0; i < mCount; i++) { 163 | MenuItem menuItem = menuList.get(i); 164 | if (i == mCurrentPosition) { 165 | menuItem.setSelected(true); 166 | } else { 167 | menuItem.setSelected(false); 168 | } 169 | } 170 | } 171 | 172 | /** 173 | * 设置未选中的图片 174 | * 175 | * @param listIconRes 176 | * @return 177 | */ 178 | public NavMenuLayout setIconRes(int[] listIconRes) { 179 | this.mListIconRes = listIconRes; 180 | if (mCount != mListIconRes.length) { 181 | Log.e(TAG, "the iconRes length is not equals count"); 182 | } else { 183 | for (int i = 0; i < mCount; i++) { 184 | if (mListIconRes[i] != 0) { 185 | menuList.get(i).setIcon(mListIconRes[i]); 186 | } 187 | } 188 | } 189 | return instance; 190 | } 191 | 192 | 193 | /** 194 | * 设置选中的图片 195 | * 196 | * @param listIconRes 197 | * @return 198 | */ 199 | public NavMenuLayout setIconResSelected(int[] listIconResSelected) { 200 | this.mListIconResSelected = listIconResSelected; 201 | if (mCount != mListIconResSelected.length) { 202 | Log.e(TAG, "the iconResSelected length is not equals count"); 203 | } else { 204 | for (int i = 0; i < mCount; i++) { 205 | if (mListIconResSelected[i] != 0) { 206 | menuList.get(i).setIconSelected(mListIconResSelected[i]); 207 | } 208 | } 209 | } 210 | return instance; 211 | } 212 | 213 | /** 214 | * 设置图片大小 215 | * 216 | * @param iconWidth 217 | * @param iconHeight 218 | * @return 219 | */ 220 | public NavMenuLayout setIconSize(int iconWidth, int iconHeight) { 221 | this.mIconWidth = iconWidth; 222 | this.mIconHeight = iconHeight; 223 | if (mCount != mListIconResSelected.length) { 224 | Log.e(TAG, "the iconResSelected length is not equals count"); 225 | } else { 226 | for (int i = 0; i < mCount; i++) { 227 | if (mListIconResSelected[i] != 0) { 228 | menuList.get(i).setIconSize(mIconWidth, mIconHeight); 229 | } 230 | } 231 | } 232 | return instance; 233 | } 234 | 235 | /** 236 | * 设置某一个图片大小 237 | * 238 | * @param iconWidth 239 | * @param iconHeight 240 | * @return 241 | */ 242 | public NavMenuLayout setIconSize(int position, int iconWidth, int iconHeight) { 243 | this.mIconWidth = iconWidth; 244 | this.mIconHeight = iconHeight; 245 | if (mCount != mListIconResSelected.length) { 246 | Log.e(TAG, "the iconResSelected length is not equals count"); 247 | } else { 248 | if (position >= 0 && position < mCount) { 249 | menuList.get(position).setIconSize(mIconWidth, mIconHeight); 250 | } else { 251 | Log.e(TAG, "setIconSize(int position, int iconWidth, int iconHeight) ---> the position is not correct"); 252 | } 253 | } 254 | 255 | return instance; 256 | } 257 | 258 | 259 | /** 260 | * 设置文字 261 | * 262 | * @param listText 263 | * @return 264 | */ 265 | public NavMenuLayout setTextRes(String[] listText) { 266 | this.mListText = listText; 267 | if (mCount != mListText.length) { 268 | Log.e(TAG, "the textRes length is not equals count"); 269 | } else { 270 | for (int i = 0; i < mCount; i++) { 271 | if (mListText[i] != null) { 272 | menuList.get(i).setText(mListText[i]); 273 | } 274 | } 275 | } 276 | return instance; 277 | } 278 | 279 | 280 | /** 281 | * 设置文字大小 282 | * 283 | * @param textSize 284 | */ 285 | public NavMenuLayout setTextSize(int textSize) { 286 | this.mTextSize = textSize; 287 | if (mCount != mListText.length) { 288 | Log.e(TAG, "the textRes length is not equals count"); 289 | } else { 290 | for (int i = 0; i < mCount; i++) { 291 | if (mListText[i] != null) { 292 | menuList.get(i).setTextSize(mTextSize); 293 | } 294 | } 295 | } 296 | 297 | return instance; 298 | } 299 | 300 | /** 301 | * 设置某一位置文字大小 302 | * 303 | * @param position 304 | * @param textSize 305 | * @return 306 | */ 307 | public NavMenuLayout setTextSize(int position, int textSize) { 308 | if (mCount != mListText.length) { 309 | Log.e(TAG, "the textRes length is not equals count"); 310 | } else { 311 | if (position >= 0 && position < mCount) { 312 | menuList.get(position).setTextSize(mTextSize); 313 | } else { 314 | Log.e("TAG", "setTextSize(int position, int textSize) ----> the position is not correct"); 315 | } 316 | 317 | } 318 | return instance; 319 | } 320 | 321 | 322 | /** 323 | * 文字颜色 324 | * 325 | * @param textColor 326 | */ 327 | public NavMenuLayout setTextColor(int textColor) { 328 | this.mTextColor = textColor; 329 | if (mCount != mListText.length) { 330 | Log.e(TAG, "the textRes length is not equals count"); 331 | } else { 332 | for (int i = 0; i < mCount; i++) { 333 | if (mListText[i] != null) { 334 | menuList.get(i).setTextColor(textColor); 335 | } 336 | } 337 | } 338 | 339 | return instance; 340 | } 341 | 342 | public NavMenuLayout setTextColor(int position, int textColor) { 343 | if (mCount != mListText.length) { 344 | Log.e(TAG, "the textRes length is not equals count"); 345 | } else { 346 | if (position >= 0 && position < mCount) { 347 | menuList.get(position).setTextColor(textColor); 348 | } else { 349 | Log.e(TAG, "setTextColor(int position, int textColor) ---> the position is not correct "); 350 | } 351 | } 352 | 353 | return instance; 354 | } 355 | 356 | 357 | /** 358 | * 选中的文字颜色 359 | * 360 | * @param textColorSelected 361 | */ 362 | public NavMenuLayout setTextColorSelected(int textColorSelected) { 363 | this.mTextColorSelected = textColorSelected; 364 | if (mCount != mListText.length) { 365 | Log.e(TAG, "the textRes length is not equals count"); 366 | } else { 367 | for (int i = 0; i < mCount; i++) { 368 | if (mListText[i] != null) { 369 | menuList.get(i).setSelectedTextColor(textColorSelected); 370 | } 371 | } 372 | } 373 | return instance; 374 | } 375 | 376 | public NavMenuLayout setTextColorSelected(int position, int textColorSelected) { 377 | if (mCount != mListText.length) { 378 | Log.e(TAG, "the textRes length is not equals count"); 379 | } else { 380 | if (position >= 0 && position < mCount) { 381 | menuList.get(position).setSelectedTextColor(textColorSelected); 382 | } else { 383 | Log.e(TAG, "setTextColorSelected(int position, int textColorSelected) ---> the position is not correct "); 384 | } 385 | } 386 | return instance; 387 | } 388 | 389 | /** 390 | * 图标和文字之间的距离 391 | * 392 | * @param marginTop 393 | */ 394 | public NavMenuLayout setMarginTop(int marginTop) { 395 | this.marginTop = marginTop; 396 | if (mCount != mListText.length) { 397 | Log.e(TAG, "the textRes length is not equals count"); 398 | } else { 399 | for (int i = 0; i < mCount; i++) { 400 | if (mListText[i] != null) { 401 | menuList.get(i).setMarginTop(marginTop); 402 | } 403 | } 404 | } 405 | return instance; 406 | } 407 | 408 | public NavMenuLayout setMarginTop(int position, int marginTop) { 409 | if (mCount != mListText.length) { 410 | Log.e(TAG, "the textRes length is not equals count"); 411 | } else { 412 | if (position >= 0 && position < mCount) { 413 | menuList.get(position).setMarginTop(marginTop); 414 | } else { 415 | Log.e(TAG, "setTextColorSelected(int position, int textColorSelected) ---> the position is not correct "); 416 | } 417 | } 418 | return instance; 419 | } 420 | 421 | 422 | /** 423 | * 背景颜色 424 | * 425 | * @param backColor 426 | */ 427 | public NavMenuLayout setBackColor(int backColor) { 428 | setBackgroundColor(backColor); 429 | return instance; 430 | } 431 | 432 | public NavMenuLayout setBackColor(int position, int backColor) { 433 | if (position >= 0 && position < mCount) { 434 | menuList.get(position).setBackgroundColor(backColor); 435 | } else { 436 | Log.e(TAG, "setBackColor(int position, int backColor) ---> the positon is not correct"); 437 | } 438 | return instance; 439 | } 440 | 441 | 442 | /** 443 | * 获取点击的位置 444 | */ 445 | public int getSelectedPosition(MenuItem item) { 446 | for (int i = 0; i < mCount; i++) { 447 | if (item == menuList.get(i)) { 448 | return i; 449 | } 450 | } 451 | return -1; 452 | } 453 | 454 | /** 455 | * 设置未读信息 456 | * 457 | * @param position 458 | * @param number 459 | * @return 460 | */ 461 | public NavMenuLayout setMsg(int position, String msg) { 462 | if (position >= 0 && position < mCount) { 463 | menuList.get(position).setMsg(msg); 464 | } else { 465 | Log.d(TAG, "setMsg(int position, String msg) ---> the position is not correct"); 466 | } 467 | return instance; 468 | 469 | } 470 | 471 | /** 472 | * 隐藏未读信息 473 | * 474 | * @param position 475 | * @return 476 | */ 477 | public NavMenuLayout hideMsg(int position) { 478 | if (position >= 0 && position < mCount) { 479 | menuList.get(position).hideMsg(); 480 | } else { 481 | Log.d(TAG, "hideMsg(int position) ---> the position is not correct"); 482 | } 483 | return instance; 484 | } 485 | 486 | 487 | /** 488 | * 显示红点 489 | * 490 | * @param position 491 | * @return 492 | */ 493 | public NavMenuLayout showRedPoint(int position) { 494 | if (position >= 0 && position < mCount) { 495 | menuList.get(position).showRedPoint(true); 496 | } else { 497 | Log.d(TAG, "showRedPoint(int position) ---> the position is not correct"); 498 | } 499 | return instance; 500 | 501 | } 502 | 503 | /** 504 | * 隐藏红点 505 | * 506 | * @param position 507 | * @return 508 | */ 509 | public NavMenuLayout hideRedPoint(int position) { 510 | if (position >= 0 && position < mCount) { 511 | menuList.get(position).hideRedPoint(); 512 | } else { 513 | Log.d(TAG, "hideRedPoint(int position) ---> the position is not correct"); 514 | } 515 | return instance; 516 | 517 | } 518 | 519 | 520 | } 521 | --------------------------------------------------------------------------------