1、功能描述
你還在為類似設(shè)置頁面的樣式(圖+文字+圖)每次寫一大堆布局文件而煩惱嗎?快來看看SuperTextView吧,這里有你想要實(shí)現(xiàn)的任何樣式,而你要做的僅僅是引入幾行代碼。SuperTextView是一個(gè)功能強(qiáng)大的布局View,可以滿足日常大部分布局樣式,開發(fā)者可以自行組合屬性配置出屬于自己風(fēng)格的樣式!可能描述起來沒有概念,還是直接看效果圖吧!
2、 效果圖
3、如何使用
Android Studio導(dǎo)入方法,添加Gradle依賴
先在項(xiàng)目根目錄的 build.gradle 的 repositories 添加:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
然后在dependencies添加:
dependencies {
...
compile 'com.github.lygttpod:SuperTextView:2.0.1'
}
3.2、項(xiàng)目中如何使用
3.2.1、布局中如何使用(示例中只列出部分屬性,開發(fā)者可根據(jù)具體需求使用其他屬性)
<com.allen.supertextviewlibrary.SuperTextView
android:id="@+id/super_tv"
android:layout_width="match_parent"
android:layout_height="80dp"
stv:sLeftBottomTextColor2="@color/colorAccent"
stv:sLeftBottomTextString="招商銀行(8888)"
stv:sLeftBottomTextString2="限額說明>>"
stv:sLeftIconRes="@drawable/bank_zhao_shang"
stv:sLeftTopTextString="銀行卡支付"
stv:sRightCheckBoxRes="@drawable/circular_check_bg"
stv:sRightCheckBoxShow="true"
stv:sLineShow="bottom"
/>
注意:
1、上下的線可以通過 sLineShow 設(shè)置 有四種顯示方式 none,top,bottom,both
2、通過設(shè)置 sUseRipple=true 開啟水波效果
3.2.2、代碼中如何使用
/**
* 可以通過鏈?zhǔn)皆O(shè)置大部分常用的屬性值
*/
superTextView.setLeftIcon(drawable)
.setLeftString("")
.setLeftTVColor(0)
.setLeftTopString("")
.setLeftTopTVColor(0)
.setLeftBottomString("")
.setLeftBottomTVColor(0)
.setLeftBottomString2("")
.setLeftBottomTVColor2(0)
.setRightString("")
.setRightTVColor(0)
.setCbChecked(true)
.setCbBackground(drawable)
.setRightIcon(drawable)
.setRightString("")
.setRightTVColor(0)
.setLeftString("")
點(diǎn)擊事件(可根據(jù)需求選擇實(shí)現(xiàn)單個(gè)或者多個(gè)點(diǎn)擊事件,需要配合xml添加是否允許點(diǎn)擊的屬性,詳情見屬性參數(shù)的意義)
superTextView.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
@Override
public void onSuperTextViewClick() {
super.onSuperTextViewClick();
//do something
}
@Override
public void onLeftTopClick() {
super.onLeftTopClick();
//do something
}
@Override
public void onLeftBottomClick() {
super.onLeftBottomClick();
//do something
}
@Override
public void onLeftBottomClick2() {
super.onLeftBottomClick2();
//do something
}
});
注意:點(diǎn)擊事件需要配合屬性值使用
sLeftTopViewIsClickable= true
sLeftBottomViewIsClickable= true
sLeftBottomView2IsClickable= true
使用第三方庫(Picasso或者Glide)加載網(wǎng)絡(luò)圖片
Picasso.with(this)
.load(url)
.placeholder(R.drawable.head_default)
.into((ImageView) superTextView.getView(SuperTextView.leftImageViewId));
3.2.3、屬性說明(以下屬性全部可以通過xml文件配置和代碼進(jìn)行設(shè)置)
4、實(shí)現(xiàn)原理
4.1、需求分析
黑格爾曾說過:存在即合理。SuperTextView的出現(xiàn)應(yīng)該就是某種需求下的產(chǎn)物。
在開發(fā)項(xiàng)目的過程中你會發(fā)現(xiàn)有很多頁面的布局都是類似的,就比如說常見的設(shè)置頁面,基本上都是圖標(biāo)+文字+圖標(biāo)的格式,而且出現(xiàn)的頻率都很高,如果不做處理勢必會寫很多無用的代碼降低開發(fā)效率,正因?yàn)槿绱宋覀優(yōu)槭裁床话堰@一類view統(tǒng)一封裝起來吶(其實(shí)就是程序猿的懶惰,哈哈),有了這個(gè)想法就開始調(diào)研市場上主流應(yīng)用的顯示樣式,經(jīng)過調(diào)研總結(jié)出一套顯示方案涵蓋了市面上90%以上的布局樣式,目的是在以后的使用中快速高效的開發(fā)。
首先SuperTextView 是繼承自 RelativeLayout實(shí)現(xiàn),里邊所有布局的添加都是通過Java代碼實(shí)現(xiàn)的,事先定義好需要的參數(shù)名及類型
下邊只列出部分參數(shù)
private int backgroundColor;//背景顏色
private int leftTVColor;//左邊文字顏色
private int leftTopTVColor;//左上文字顏色
private int leftBottomTVColor;//左下文字顏色
private int leftBottomTVColor2;//左下第二個(gè)文字顏色
private int rightTVColor;//右邊文字顏色
private int centerTVColor;//中間文字顏色
private boolean isSingLines = true;//是否單行顯示 默認(rèn)單行
private int maxLines = 1;//最多幾行 默認(rèn)顯示一行
private int maxEms = 10;//最多幾個(gè)字 默認(rèn)顯示10個(gè)漢子
private static final int NONE = 0;
private static final int TOP = 1;
private static final int BOTTOM = 2;
private static final int BOTH = 3;
private static final int DEFAULT = BOTTOM;
然后就是在構(gòu)造方法里邊去實(shí)現(xiàn)各個(gè)方法
public SuperTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
getAttr(attrs);
initLayout();
}
我們知道系統(tǒng)控件可以從XML中拿到定義好的屬性值,那么我們其實(shí)也是可以的,首先在attr中定義好相關(guān)屬性名及字段類型然后通過AttributeSet去拿到相關(guān)屬性值在進(jìn)行設(shè)置
private void getAttr(AttributeSet attrs) {
//獲取我們在attr中定義的SuperTextView相關(guān)屬性
TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.SuperTextView);
//下邊展示的各個(gè)類型參數(shù)的獲取方法,drawable、string、boolean、int、px
leftIconRes = typedArray.getDrawable(R.styleable.SuperTextView_sLeftIconRes);
leftTextString = typedArray.getString(R.styleable.SuperTextView_sLeftTextString);
showCheckBox = typedArray.getBoolean(R.styleable.SuperTextView_sRightCheckBoxShow, false);
lineType = typedArray.getInt(R.styleable.SuperTextView_sLineShow, DEFAULT);
topLineMargin = typedArray.getDimensionPixelSize(R.styleable.SuperTextView_sTopLineMargin, defaultLinePadding);
//獲取完之后記得recycle()釋放掉資源
typedArray.recycle();
}
接下來就拿一個(gè)view為例進(jìn)行說明如何動態(tài)添加view及設(shè)置相關(guān)屬性
/**
* 初始化左邊文字
*/
private void initLeftText() {
leftTV = new TextView(mContext);
leftTextParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
leftTextParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
leftTextParams.addRule(RelativeLayout.RIGHT_OF, R.id.sLeftIconId);
setMargin(leftTextParams, leftTVMarginLeft, 0, dip2px(mContext, 10), 0);
leftTV.setId(R.id.sLeftTextId);
leftTV.setLayoutParams(leftTextParams);
leftTV.setText(leftTextString);
//設(shè)置Params的方法抽離出來便于以后統(tǒng)一調(diào)用
setTextViewParams(leftTV, isSingLines, maxLines, maxEms);
//設(shè)置字體顏色的方法抽離出來便于以后統(tǒng)一調(diào)用
setTextColor(leftTV, leftTVColor);
//設(shè)置字體大小的方法抽離出來便于以后統(tǒng)一調(diào)用
setTextSize(leftTV, leftTVSize);
//調(diào)用addView方法把我們動態(tài)創(chuàng)建的view添加到布局中
addView(leftTV);
}
初始化完view之后就是要暴露一些方法供外部人員使用了(部分代碼示例)
在這里我們返回SuperTextView對象是為了實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用
/**
* 獲取checkbox狀態(tài)
*
* @return 返回選擇框當(dāng)前選中狀態(tài)
*/
public boolean getCbisChecked() {
boolean isChecked = false;
if (rightCheckBox != null) {
isChecked = rightCheckBox.isChecked();
}
return isChecked;
}
/**
* 設(shè)置左邊文字的顏色
*
* @param textColor 文字顏色值
* @return 返回對象
*/
public SuperTextView setLeftTVColor(int textColor) {
leftTVColor = textColor;
if (leftTV == null) {
initLeftText();
} else {
leftTV.setTextColor(textColor);
}
return this;
}
最后就是一些回調(diào)方法的使用,以點(diǎn)擊事件為例
/**
* 點(diǎn)擊事件接口
* 這里沒有使用interface是因?yàn)橛行┓椒ㄎ覀儾皇且欢ㄒ獙?shí)現(xiàn)的
* 用到哪個(gè)方法再去重寫方法就可以了
*/
public static class OnSuperTextViewClickListener {
public void onSuperTextViewClick() {
}
public void onLeftTopClick() {
}
public void onLeftBottomClick() {
}
public void onLeftBottomClick2() {
}
}
相關(guān)的set方法拿到OnSuperTextViewClickListener對象
public SuperTextView setOnSuperTextViewClickListener(OnSuperTextViewClickListener listener) {
onSuperTextViewClickListener = listener;
return this;
}
初始化view的時(shí)候在各個(gè)view的點(diǎn)擊事件中添加相應(yīng)的回調(diào)方法就行了
if (mLeftTopViewIsClickable) {
leftTopTV.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (onSuperTextViewClickListener != null) {
onSuperTextViewClickListener.onLeftTopClick();
}
}
});
}
以上只是源碼中的部分方法拿出來供分析使用,看了之后是不是感覺實(shí)現(xiàn)其實(shí)很簡單,趕緊自己著手實(shí)現(xiàn)一個(gè)吧!