Android自定義控件實(shí)現(xiàn)底部菜單(下)
在app中經(jīng)常會用到底部菜單的控件,每次都需要寫好多代碼,今天我們用到了前幾篇博客里的控件來進(jìn)一步封裝底部菜單。先看效果圖:

主要包括以下功能:
1 設(shè)置icon以及點(diǎn)擊之后的icon
2 設(shè)置文字
3 設(shè)置文字顏色以及點(diǎn)擊之后的文字顏色
4 設(shè)置未讀數(shù)量、更多以及new
我們先看如何使用,然后再看如何實(shí)現(xiàn)的
1 在布局文件中引用MenuM
<com.landptf.view.MenuM android:id="@+id/mm_bottom" android:layout_width="match_parent" android:layout_height="56dp" android:layout_alignParentBottom="true" landptf:backColor="@color/content" landptf:textColor="@color/text" landptf:textColorPress="@color/colorPrimary" landptf:count="4" />
這里主要說一下count屬性,表示菜單項的個數(shù)。
2 在Activity中初始化
final MenuM mmBottom = (MenuM) findViewById(R.id.mm_bottom);
mmBottom.setText(text);
mmBottom.setIconDrawable(iconDrawable);
mmBottom.setIconDrawablePress(iconDrawablePress);
//設(shè)置默認(rèn)選中第一項
mmBottom.setPressState(0, MotionEvent.ACTION_DOWN);
mmBottom.setOnItemClickListener(new MenuM.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Toast.makeText(MenuMTestActivity.this, mmBottom.getText(position), Toast.LENGTH_SHORT).show();
}
});
mmBottom.setUnReadCount(0, 100);
mmBottom.setUnReadCount(1, 15);
mmBottom.setVisibilityMore(2, View.VISIBLE);
mmBottom.setVisibilityNew(3, View.VISIBLE);
有以下幾個全局變量
text = new String[]{"首頁", "通訊錄", "發(fā)現(xiàn)", "我"};
//為了演示方便我只找了兩張icon,在實(shí)際開發(fā)中一般需要從網(wǎng)絡(luò)上下載,然后在設(shè)置
Drawable drawable = getResources().getDrawable(R.drawable.icon_home_page);
Drawable drawablePress = getResources().getDrawable(R.drawable.icon_home_page_press);
iconDrawable = new Drawable[]{drawable, drawable, drawable, drawable};
iconDrawablePress = new Drawable[]{drawablePress, drawablePress, drawablePress, drawablePress};
以上就是全部代碼是不是很方便呢?。?!
接下來我們來看下如何實(shí)現(xiàn)的
1 在style里定義了幾個屬性這里就不貼出來了,大家可以查看源碼,在本文的最后會給出全部源碼的下載地址
2 MenuM.java
package com.landptf.view;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.landptf.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by landptf on 2017/01/15.
* 菜單,可用于底部導(dǎo)航菜單,以及內(nèi)容區(qū)的菜單列表
*/
public class MenuM extends LinearLayout {
private static final String TAG = MenuM.class.getSimpleName();
private Context mContext;
private List<MenuItemM> menuList;
private List<RelativeLayout> rlList;
private OnItemClickListener mOnItemClickListener;
private int count = 0;
public MenuM(Context context) {
this(context, null, 0);
}
public MenuM(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MenuM(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
setOrientation(LinearLayout.HORIZONTAL);
TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.menuM, defStyle, 0);
if (a != null) {
//初始化菜單數(shù)量
count = a.getInteger(R.styleable.menuM_count, 0);
if (count > 0) {
initControl();
}
//設(shè)置背景色
ColorStateList colorList = a.getColorStateList(R.styleable.menuM_backColor);
if (colorList != null) {
int backColor = colorList.getColorForState(getDrawableState(), 0);
if (backColor != 0) {
setBackColor(backColor);
}
}
//設(shè)置文字的顏色
ColorStateList textColorList = a.getColorStateList(R.styleable.menuM_textColor);
if (textColorList != null) {
int textColor = textColorList.getColorForState(getDrawableState(), 0);
if (textColor != 0) {
setTextColor(textColor);
}
}
//記錄View被按下時文字的顏色
ColorStateList textColorPressList = a.getColorStateList(R.styleable.menuM_textColorPress);
if (textColorPressList != null) {
int textColorPress = textColorPressList.getColorForState(getDrawableState(), 0);
if (textColorPress != 0) {
setTextColorPress(textColorPress);
}
}
//設(shè)置文本字體大小
float textSize = a.getFloat(R.styleable.menuM_textSize, 0);
if (textSize != 0) {
setTextSize(textSize);
}
a.recycle();
}
}
/**
* 由于MenuItemM是有ButtonExtendM擴(kuò)展而來,為了適應(yīng)上下左右不同的樣式
* 需要在MenuItemM外層嵌套一層RelativeLayout,暫時沒有找到更好的替代方案
*/
private void initControl() {
rlList = new ArrayList<>(count);
menuList = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
RelativeLayout rlPanel = new RelativeLayout(mContext);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
lp.weight = 1;
rlPanel.setLayoutParams(lp);
final MenuItemM menuItem = new MenuItemM(mContext);
RelativeLayout.LayoutParams lpR = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lpR.addRule(RelativeLayout.CENTER_IN_PARENT);
menuItem.setLayoutParams(lpR);
menuItem.setOnClickListener(new MenuItemM.OnClickListener() {
@Override
public void onClick(View v) {
//此處需要根據(jù)view獲取position
MenuM.this.onClick(getPosition(menuItem));
}
});
rlPanel.addView(menuItem);
menuList.add(menuItem);
rlList.add(rlPanel);
addView(rlPanel);
}
}
/**
* 設(shè)置View的背景色
*
* @param backColor
*/
public void setBackColor(int backColor) {
if (backColor == 0) return;
if (!checkCount()) {
return;
}
for (RelativeLayout item : rlList) {
item.setBackgroundColor(backColor);
}
for (MenuItemM item : menuList) {
item.setBackColor(backColor);
}
}
/**
* 設(shè)置文字的顏色
*
* @param textColor
*/
public void setTextColor(int textColor) {
if (textColor == 0) return;
if (!checkCount()) {
return;
}
for (MenuItemM item : menuList) {
item.setTextColor(textColor);
}
}
/**
* 設(shè)置View被按下時文字的顏色
*
* @param textColorPress
*/
public void setTextColorPress(int textColorPress) {
if (textColorPress == 0) return;
if (!checkCount()) {
return;
}
for (MenuItemM item : menuList) {
item.setTextColorPress(textColorPress);
}
}
/**
* 設(shè)置icon的圖片
*
* @param iconDrawable
*/
public void setIconDrawable(Drawable[] iconDrawable) {
if (count != iconDrawable.length) {
Log.e(TAG, "the iconDrawable length do not equals count");
return;
}
for (int i = 0; i < count; i++) {
if (iconDrawable[i] != null) {
menuList.get(i).setIconDrawable(iconDrawable[i]);
}
}
}
/**
* 設(shè)置icon的圖片
*
* @param iconDrawable
*/
public void setIconDrawable(List<Drawable> iconDrawable) {
if (count != iconDrawable.size()) {
Log.e(TAG, "the iconDrawable length do not equals count");
return;
}
for (int i = 0; i < count; i++) {
if (iconDrawable.get(i) != null) {
menuList.get(i).setIconDrawable(iconDrawable.get(i));
}
}
}
/**
* 設(shè)置View被按下時的icon的圖片
*
* @param iconDrawablePress
*/
public void setIconDrawablePress(Drawable[] iconDrawablePress) {
if (count != iconDrawablePress.length) {
Log.e(TAG, "the iconDrawablePress length do not equals count");
return;
}
for (int i = 0; i < count; i++) {
if (iconDrawablePress[i] != null) {
menuList.get(i).setIconDrawablePress(iconDrawablePress[i]);
}
}
}
/**
* 設(shè)置View被按下時的icon的圖片
*
* @param iconDrawablePress
*/
public void setIconDrawablePress(List<Drawable> iconDrawablePress) {
if (count != iconDrawablePress.size()) {
Log.e(TAG, "the iconDrawablePress length do not equals count");
return;
}
for (int i = 0; i < count; i++) {
if (iconDrawablePress.get(i) != null) {
menuList.get(i).setIconDrawablePress(iconDrawablePress.get(i));
}
}
}
/**
* 設(shè)置顯示的文本內(nèi)容
*
* @param text
*/
public void setText(CharSequence[] text) {
for (int i = 0; i < count; i++) {
menuList.get(i).setText(text[i]);
}
}
/**
* 設(shè)置顯示的文本內(nèi)容
*
* @param text
*/
public void setText(List<CharSequence> text) {
if (count != text.size()) {
Log.e(TAG, "the text length do not equals count");
return;
}
for (int i = 0; i < count; i++) {
menuList.get(i).setText(text.get(i));
}
}
/**
* 獲取顯示的文本
*
* @return
*/
public String getText(int index) {
if (!checkIndex(index)) {
return "";
}
return menuList.get(index).getText();
}
/**
* 設(shè)置文本字體大小
*
* @param size
*/
public void setTextSize(float size) {
if (!checkCount()) {
return;
}
for (MenuItemM item : menuList) {
item.setTextSize(size);
}
}
/**
* 設(shè)置更多提示是否顯示
* 如果顯示則先重置new和未讀數(shù)量圖標(biāo)
*
* @param visibleMore
*/
public void setVisibilityMore(int index, int visibleMore) {
if (!checkIndex(index)) {
return;
}
menuList.get(index).setVisibilityMore(visibleMore);
}
/**
* 設(shè)置New提示是否顯示
* 如果顯示則先重置更多和未讀數(shù)量圖標(biāo)
*
* @param visibleNew
*/
public void setVisibilityNew(int index, int visibleNew) {
if (!checkIndex(index)) {
return;
}
menuList.get(index).setVisibilityNew(visibleNew);
}
/**
* 設(shè)置未讀數(shù)量
* 如果小于等于0,表示隱藏
* 如果大于99,則將其隱藏,同時顯示更多的提示
* 如果在0-99區(qū)間,則隱藏更多和new圖標(biāo)
*
* @param unReadCount
*/
public void setUnReadCount(int index, int unReadCount) {
if (!checkIndex(index)) {
return;
}
menuList.get(index).setUnReadCount(unReadCount);
}
/**
* 設(shè)置為被選中狀態(tài)
*
* @param index
* @param state in MotionEvent.ACTION_DOWN or MotionEvent.ACTION_UP
*/
public void setPressState(int index, int state) {
if (!checkIndex(index)) {
return;
}
menuList.get(index).setPressState(state);
}
/**
* 設(shè)置菜單點(diǎn)擊事件
*
* @param listener
*/
public void setOnItemClickListener(@Nullable OnItemClickListener listener) {
mOnItemClickListener = listener;
}
private void onClick(int position) {
for (int i = 0; i < count; i++) {
if (i == position) {
setPressState(i, MotionEvent.ACTION_DOWN);
} else {
setPressState(i, MotionEvent.ACTION_UP);
}
}
mOnItemClickListener.onItemClick(position);
}
/**
* 獲取點(diǎn)擊菜單項的位置
* @param item
* @return
*/
private int getPosition(MenuItemM item) {
for (int i = 0; i < count; i++) {
if (item == menuList.get(i)) {
return i;
}
}
return -1;
}
/**
* 檢查是否設(shè)置了Count參數(shù)
*
* @return
*/
private boolean checkCount() {
if (count == 0) {
Log.e(TAG, "You must set the count first");
return false;
}
return true;
}
/**
* 校驗輸入?yún)?shù)是否合法
*
* @param index
* @return
*/
private boolean checkIndex(int index) {
if (!checkCount()) {
return false;
}
if (index < 0 || index >= count) {
Log.e(TAG, "the index is wrong");
return false;
}
return true;
}
public interface OnItemClickListener {
void onItemClick(int position);
}
}
代碼比較簡單,相信大家看一遍都可以理解,這里面使用了MenuItemM自定義控件,有不了解的可以參考以前的博客http://www.dhdzp.com/article/103913.htm或者查看源碼。
全部代碼已托管到開源中國的碼云上,歡迎下載,地址:https://git.oschina.net/landptf/landptf.git
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用CoordinatorLayout實(shí)現(xiàn)底部彈出菜單
- Android底部菜單欄實(shí)現(xiàn)的實(shí)例代碼
- android SectorMenuView底部導(dǎo)航扇形菜單的實(shí)現(xiàn)代碼
- android實(shí)現(xiàn)上滑屏幕隱藏底部菜單欄的示例
- Android仿微信底部菜單欄效果
- Android仿網(wǎng)易嚴(yán)選底部彈出菜單效果
- Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法
- Android自定義控件實(shí)現(xiàn)底部菜單(上)
- Android如何實(shí)現(xiàn)底部菜單固定到底部
相關(guān)文章
Android?Material組件庫日期選擇和時間選擇器的使用方法
這篇文章主要介紹了Android?Material組件庫(日期選擇和時間選擇器)基本使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11
Android開發(fā)必備知識 為什么說Kotlin值得一試
為什么說值得一試,這篇文章主要為大家詳細(xì)介紹了Android開發(fā)必備知識,Kotlin的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Android Listview中顯示不同的視圖布局詳解及實(shí)例代碼
這篇文章主要介紹了Android Listview中顯示不同的視圖布局詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
android實(shí)現(xiàn)一鍵鎖屏和一鍵卸載的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于android如何實(shí)現(xiàn)一鍵鎖屏和一鍵卸載的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-05-05
解決webview調(diào)用goBack()返回上一頁自動刷新閃白的情況
本文主要介紹了解決webview調(diào)用goBack()返回上一頁自動刷新閃白的情況。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
Android USB轉(zhuǎn)串口通信開發(fā)實(shí)例詳解
這篇文章主要介紹了 Android USB轉(zhuǎn)串口通信開發(fā)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
flutter 屏幕尺寸適配和字體大小適配的實(shí)現(xiàn)
這篇文章主要介紹了flutter 屏幕尺寸適配和字體大小適配的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

