編輯:關於 Android 開發
本文記錄如何封裝一個可複用的底部菜單切換框架,日後寫底部導航時直接作為依賴庫引入即可,免去重複勞力。
LinearLayout(也可依喜好繼承 RelativeLayout)。package com.weight.tabbottomlib.view;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* 底部切換框架
*/
public class MyBottomLayout extends LinearLayout {
static ArrayList<Item> itemData = new ArrayList<Item>();
static ArrayList<View> viewArryItem = new ArrayList<View>();
LayoutInflater inflater; // 布局管理器
View mainView; // 底部的容器
View viewItem;
int itemSize = 0;
public MyBottomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 為底部容器添加 view
*/
public void addBottomLayoutValue(ArrayList<Item> itemData) {
LayoutInflater inflater = LayoutInflater.from(getContext());
this.itemData = itemData;
this.itemSize = itemData.size();
for (int i = 0; i < itemData.size(); i++) {
View viewItem = inflater.inflate(R.layout.layout_tab_item, null);
viewItem.findViewById(R.id.tabImg).setBackgroundResource(
itemData.get(i).getDrawableNormalId());
TextView textView = (TextView) viewItem.findViewById(R.id.tabText);
textView.setText(itemData.get(i).getName());
textView.setTextColor(Color.WHITE);
this.addView(viewItem, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f));
viewItem.setId(i);
viewArryItem.add(viewItem);
viewItem.setOnClickListener(new lister());
}
changeDataState(); // 處理整體數據
}
/**
* 改變數據的狀態,不足 5 項時補齊
*/
private void changeDataState() {
if (itemSize != 5) {
for (int i = itemSize; i < 5; i++) {
itemData.add(new Item());
View viewItem = LayoutInflater.from(getContext())
.inflate(R.layout.layout_tab_item, null);
viewArryItem.add(viewItem);
}
}
}
public interface ICallbackLister {
public void click(int id);
}
ICallbackLister callbackLister = null;
public void setOnCallbackLister(ICallbackLister callbackLister) {
this.callbackLister = callbackLister;
}
// tab 點擊事件處理
private class lister implements OnClickListener {
@Override
public void onClick(View clickView) {
callbackLister.click(clickView.getId());
initBottom(clickView.getId());
}
}
public static void initBottom(int id) {
switch (id) {
case 0:
initData(itemData.get(0).getDrawableSelectId(), itemData.get(1).getDrawableNormalId(),
itemData.get(2).getDrawableNormalId(), itemData.get(3).getDrawableNormalId(),
itemData.get(4).getDrawableNormalId());
changeTextColor(
(TextView) viewArryItem.get(0).findViewById(R.id.tabText),
(TextView) viewArryItem.get(1).findViewById(R.id.tabText),
(TextView) viewArryItem.get(2).findViewById(R.id.tabText),
(TextView) viewArryItem.get(3).findViewById(R.id.tabText),
(TextView) viewArryItem.get(4).findViewById(R.id.tabText));
break;
// case 1~4 依此類推,切換對應的選中態圖示與文字顏色
}
}
public static void initData(int resIdOne, int resIdTwo, int resIdThree,
int resIdFour, int resIdFive) {
try {
viewArryItem.get(0).findViewById(R.id.tabImg).setBackgroundResource(resIdOne);
viewArryItem.get(1).findViewById(R.id.tabImg).setBackgroundResource(resIdTwo);
viewArryItem.get(2).findViewById(R.id.tabImg).setBackgroundResource(resIdThree);
viewArryItem.get(3).findViewById(R.id.tabImg).setBackgroundResource(resIdFour);
viewArryItem.get(4).findViewById(R.id.tabImg).setBackgroundResource(resIdFive);
} catch (Exception err) {
err.printStackTrace(); // 說明已無這麼多 item
}
}
public static void changeTextColor(TextView txtOne, TextView txtTwo,
TextView txtThree, TextView txtFour, TextView txtFive) {
txtOne.setTextColor(Color.MAGENTA); // 選中態
txtTwo.setTextColor(Color.BLUE);
txtThree.setTextColor(Color.BLUE);
txtFour.setTextColor(Color.BLUE);
txtFive.setTextColor(Color.BLUE);
}
}
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<ImageView
android:id="@+id/tabImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/tabText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tabImg"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:textSize="16dp" />
</RelativeLayout>
</RelativeLayout>
package com.weight.tabbottomlib.view;
public class Item {
private int drawableNormalId;
private int drawableSelectId;
private String name;
public Item(int drawableNormalId, int drawableSelectId, String name) {
super();
this.drawableNormalId = drawableNormalId;
this.drawableSelectId = drawableSelectId;
this.name = name;
}
public Item() {
super();
}
// getter / setter 省略
}
要作為庫使用,右鍵你的工程 → Build Path → Configure Build Path → Android,勾選 isLibrary 並點擊 OK 即可。具體使用方法將於後續文章說明。
EditText 密碼明文切換的兩種實現方式
EditText 密碼明文切換的兩種實現方式 一、效果預覽 二、布局文件 思路是在 EditText 右側疊一個眼睛圖標,點擊圖標切換輸入框的顯示模式。 <?x
RAC 集群集體宕機排查实录:真凶是存儲鏈路中斷
昨天有位朋友來問一個 RAC 問題。環境是 Oracle 11.2.0.4,兩節點集群,現象是 CRSD 進程掛掉後集群不會自動切換,而且這情況已經反覆出現好幾次了。 我
Android 自學筆記(Android Studio)4.4:AdapterView 家族與 Adapter 機制詳解
一、引言 AdapterView 是個抽象類別,它派生出來的眾多子類在用法上大同小異,差別主要在呈現形式,因此常被歸在同一個單元來學習。理解它的關鍵在於一個觀念:Adap
OLTP 應用下的 MySQL 高可用架構選型對比
OLTP 應用下的 MySQL 高可用架構選型 在把企業核心應用從商用資料庫遷移到開源資料庫、以本地磁碟取代共享儲存之前,必須先回答幾個關鍵問題,否則遷移很難真正落地: