Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自訂控制項實作底部選單(上)

編輯:關於 Android 程式設計

今天我們要封裝一個底部選單欄,這是大多數應用都會用到的元件,因此把它自訂出來,方便日後專案重複使用。

這個控制項的實作將分為上下兩篇來介紹。先來認識選單欄的子控制項 MenuItemM,它有什麼用呢?我們看看一些主流 App 的選單樣式,例如:

以上三張圖片分別來自微信、今日頭條與去哪兒。接下來,我們將看看如何透過一個控制項來實現不同的效果。先來看我寫的一個 Demo:

可以看到,標題欄的消息控制項,以及底部的三個選單項,都是透過 MenuItemM 來實作的。

本篇主要是演示選單欄的子控制項,我們將在下一篇完成底部選單欄的整體封裝。這個控制項使用了上一篇教學中介紹的 ButtonExtendM,有興趣可以先參考相關文章。

接下來看看實作過程。

1. 定義屬性

 <declare-styleable name="MenuItemM">
 <attr name="backColor" />
 <attr name="textColor" />
 <attr name="textColorPress" />
 <attr name="iconDrawable" />
 <attr name="iconDrawablePress" />
 <attr name="text" />
 <attr name="textSize" />
 <attr name="unReadCount" format="integer" />
 <attr name="visibleMore">
 <enum name="visible" value="0x00000000" />
 <enum name="gone" value="0x00000008" />
 </attr>
 <attr name="visibleNew">
 <enum name="visible" value="0x00000000" />
 <enum name="gone" value="0x00000008" />
 </attr>
 </declare-styleable> 

這裡要特別留意 visibleMore 與 visibleNew 裡的兩個列舉值,它們與 View 原始碼中的 VISIBLE 和 GONE 保持一致。關於如何定義與使用屬性,可參考我之前的教學。

2. 版面檔 view_menu_item_m.xml

 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
 xmlns:landptf="https://schemas.android.com/apk/res-auto"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center">
 <com.landptf.view.ButtonExtendM
 android:id="@+id/bem_menu"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_marginRight="8dp"
 landptf:style="iconUp" />
 <ImageView
 android:id="@+id/iv_more"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="top|right"
 android:background="@drawable/icon_more"
 android:visibility="gone" />
 <ImageView
 android:id="@+id/iv_new"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="top|right"
 android:background="@drawable/icon_new"
 android:visibility="gone" />
 <com.landptf.view.ButtonM
 android:id="@+id/btm_unread_count"
 android:layout_width="20dp"
 android:layout_height="20dp"
 android:layout_gravity="top|right"
 android:textSize="12sp"
 android:visibility="gone"
 landptf:backColor="#ff0000"
 landptf:fillet="true"
 landptf:shape="oval"
 landptf:textColor="@android:color/white" />
 </FrameLayout> 

這裡使用了 FrameLayout,主要透過 ButtonExtendM 的上下結構控制項,再加上右上角的三種提示資訊:數量提示、more 提示與 new 提示。

3. MenuItemM.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.util.AttributeSet;
 import android.util.Log;
 import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
 import com.landptf.R;
 /**
 * Created by landptf on 2016/11/07.
 * 菜單按鈕,例如底部菜單的item或者消息控件
 */
 public class MenuItemM extends FrameLayout {
 private static final String TAG = MenuItemM.class.getSimpleName();
 /** 定義控件 */
 private ButtonExtendM bemMenu;
 private ImageView ivMore;
 private ImageView ivNew;
 private ButtonM btmUnReadCount;
 private OnClickListener onClickListener = null;
 public interface OnClickListener {
 void onClick(View v);
 }
 /** 設置View的Click事件
 * @param l */
 public void setOnClickListener(OnClickListener l) {
 this.onClickListener = l;
 //攔截ButtonExtendM控件的點擊事件,使其指向this.onclick
 bemMenu.setOnClickListener(new ButtonExtendM.OnClickListener() {
 @Override public void onClick(View v) {
 onClickListener.onClick(v);
 }
 });
 }
 public MenuItemM(Context context) {
 super(context);
 }
 public MenuItemM(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
 }
 public MenuItemM(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 init(context, attrs, defStyleAttr);
 }
 private void init(Context context, AttributeSet attrs, int defStyle) {
 //加載布局
 LayoutInflater.from(context).inflate(R.layout.view_menu_item_m, this, true);
 //初始化控件
 bemMenu = (ButtonExtendM) findViewById(R.id.bem_menu);
 ivMore = (ImageView) findViewById(R.id.iv_more);
 ivNew = (ImageView) findViewById(R.id.iv_new);
 btmUnReadCount = (ButtonM) findViewById(R.id.btm_unread_count);
 btmUnReadCount.setGravity(Gravity.CENTER);
 TypedArray a = getContext().obtainStyledAttributes(
 attrs, R.styleable.MenuItemM, defStyle, 0);
 if (a != null) {
 //設置背景色
 ColorStateList colorList = a.getColorStateList(R.styleable.MenuItemM_backColor);
 if (colorList != null) {
 int backColor = colorList.getColorForState(getDrawableState(), 0);
 if (backColor != 0) {
 setBackColor(backColor);
 }
 }
 //設置icon
 Drawable iconDrawable = a.getDrawable(R.styleable.MenuItemM_iconDrawable);
 if (iconDrawable != null) {
 setIconDrawable(iconDrawable);
 }
 //記錄View被按下時的icon的圖片
 Drawable iconDrawablePress = a.getDrawable(R.styleable.MenuItemM_iconDrawablePress);
 if (iconDrawablePress != null) {
 setIconDrawablePress(iconDrawablePress);
 }
 //設置文字的顏色
 ColorStateList textColorList = a.getColorStateList(R.styleable.MenuItemM_textColor);
 if (textColorList != null) {
 int textColor = textColorList.getColorForState(getDrawableState(), 0);
 if (textColor != 0) {
 setTextColor(textColor);
 }
 }
 //記錄View被按下時文字的顏色
 ColorStateList textColorPressList = a.getColorStateList(R.styleable.MenuItemM_textColorPress);
 if (textColorPressList != null) {
 int textColorPress = textColorPressList.getColorForState(getDrawableState(), 0);
 if (textColorPress != 0) {
 setTextColorPress(textColorPress);
 }
 }
 //設置顯示的文本內容
 String text = a.getString(R.styleable.MenuItemM_text);
 if (text != null) {
 setText(text);
 }
 //設置文本字體大小
 float textSize = a.getFloat(R.styleable.MenuItemM_textSize, 0);
 if (textSize != 0) {
 setTextSize(textSize);
 }
 //設置更多提示是否顯示
 int visibleMore = a.getInt(R.styleable.MenuItemM_visibleMore, -1);
 if (visibleMore != -1){
 setVisibilityMore(visibleMore);
 }
 //設置new提示是否顯示
 int visibleNew = a.getInt(R.styleable.MenuItemM_visibleNew, -1);
 if (visibleNew != -1){
 setVisibilityNew(visibleNew);
 }
 //設置消息未讀數量
 int unReadCount = a.getInt(R.styleable.MenuItemM_unReadCount, -1);
 if (unReadCount != -1){
 setUnReadCount(unReadCount);
 }
 a.recycle();
 }
 setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View v) {
 if (onClickListener != null) {
 onClickListener.onClick(v);
 }
 }
 });
 }
 /** 設置為被選中狀態
 * @param state in MotionEvent.ACTION_DOWN or MotionEvent.ACTION_UP */
 public void setPressState(int state){
 if (state != MotionEvent.ACTION_DOWN && state != MotionEvent.ACTION_UP){
 Log.w(TAG, "無效參數");
 return;
 }
 bemMenu.setPressState(state);
 }
 /** 設置View的背景色
 * @param backColor */
 public void setBackColor(int backColor) {
 bemMenu.setBackColor(backColor);
 }
 /** 設置icon的圖片
 * @param iconDrawable */
 public void setIconDrawable(Drawable iconDrawable) {
 bemMenu.setIconDrawable(iconDrawable);
 }
 /** 設置View被按下時的icon的圖片
 * @param iconDrawablePress */
 public void setIconDrawablePress(Drawable iconDrawablePress) {
 bemMenu.setIconDrawablePress(iconDrawablePress);
 }
 /** 設置文字的顏色
 * @param textColor */
 public void setTextColor(int textColor) {
 if (textColor == 0) return;
 bemMenu.setTextColor(textColor);
 }
 /** 設置View被按下時文字的顏色
 * @param textColorPress */
 public void setTextColorPress(int textColorPress) {
 if (textColorPress == 0) return;
 bemMenu.setTextColorPress(textColorPress);
 }
 /** 設置顯示的文本內容
 * @param text */
 public void setText(CharSequence text) {
 bemMenu.setText(text);
 }
 /** 獲取顯示的文本
 * @return */
 public String getText() {
 return bemMenu.getText();
 }
 /** 設置文本字體大小
 * @param size */
 public void setTextSize(float size) {
 bemMenu.setTextSize(size);
 }
 /** 設置更多提示是否顯示
 * 如果顯示則先重置new和未讀數量圖標
 * @param visibleMore */
 public void setVisibilityMore(int visibleMore) {
 if (visibleMore == VISIBLE) {
 resetTip();
 }
 ivMore.setVisibility(visibleMore);
 }
 /** 設置New提示是否顯示
 * 如果顯示則先重置更多和未讀數量圖標
 * @param visibleNew */
 public void setVisibilityNew(int visibleNew) {
 if (visibleNew == VISIBLE) {
 resetTip();
 }
 ivNew.setVisibility(visibleNew);
 }
 /** 設置未讀數量
 * 如果小於等於0,表示隱藏
 * 如果大於99,則將其隱藏,同時顯示更多的提示
 * 如果在0-99區間,則隱藏更多和new圖標
 * @param unReadCount */
 public void setUnReadCount(int unReadCount){
 if (unReadCount <= 0){
 btmUnReadCount.setVisibility(GONE);
 //如果先設置100(此時會顯示ivMore),再設置0,因此此處應將ivMore同時置為GONE
 if (ivMore.getVisibility() == VISIBLE){
 ivMore.setVisibility(GONE);
 }
 return;
 }
 if (unReadCount > 99){
 setVisibilityMore(VISIBLE);
 return;
 }
 resetTip();
 btmUnReadCount.setVisibility(VISIBLE);
 btmUnReadCount.setText(unReadCount + "");
 }
 /** 重置提示信息 */
 private void resetTip(){
 setVisibilityMore(GONE);
 setVisibilityNew(GONE);
 setUnReadCount(0);
 }
 } 

程式碼看起來有點長,但邏輯其實很簡單。自訂控制項的過程大多類似,比較多的是對外提供的介面方法。要特別注意的是,使用時寬高要設為自適應(wrap_content),若指定了固定大小或 match_parent,子控制項將會偏向左上角,無法置中。

4. 最後簡單看一下如何使用

 <com.landptf.view.MenuItemM
 android:id="@+id/mim_home_page"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerVertical="true"
 android:layout_marginLeft="32dp"
 landptf:iconDrawable="@drawable/icon_home_page"
 landptf:iconDrawablePress="@drawable/icon_home_page_press"
 landptf:textColor="#696969"
 landptf:textColorPress="#303f9f"
 landptf:text="首頁" /> 

這裡主要使用了以下四個屬性,分別代表預設圖示、按下後顯示的圖示,以及文字顏色與按下後的文字顏色:

 landptf:iconDrawable="@drawable/icon_home_page"
 landptf:iconDrawablePress="@drawable/icon_home_page_press"
 landptf:textColor="#696969"
 landptf:textColorPress="#303f9f" 
 final MenuItemM mimHomePage = (MenuItemM) findViewById(R.id.mim_home_page);
 if (mimHomePage != null){
 //默認為選中狀態
 mimHomePage.setPressState(MotionEvent.ACTION_DOWN);
 mimHomePage.setVisibilityMore(View.VISIBLE);
 mimHomePage.setOnClickListener(new MenuItemM.OnClickListener() {
 @Override public void onClick(View v) {
 //按下後隱藏提示信息
 mimHomePage.setVisibilityMore(View.GONE);
 }
 });
 } 

好了,就介紹到這裡。更多的使用方法可以參考原始碼 MenuItemMTestActivity.java。全部程式碼已託管於開源中國的碼雲上,歡迎大家下載使用。

希望這份子控制項的教學能對大家有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved