欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義TitleView標(biāo)題開發(fā)實(shí)例

 更新時(shí)間:2016年09月06日 17:27:04   作者:_堅(jiān)果  
這篇文章主要介紹了Android自定義TitleView標(biāo)題開發(fā)實(shí)例的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

Android開發(fā)過程中,經(jīng)常遇到一個(gè)項(xiàng)目需要重復(fù)的定義相同樣式的標(biāo)題欄,Android相繼推出了actionBar, toolBar, 相信有用到的朋友也會遇到一些不如意的時(shí)候,比如標(biāo)題欄居中時(shí),需要自定義xml文件給toolBar等,不了解actionBar,toolBar的可以去找相應(yīng)的文章了解,這里介紹自定義titleBar滿足國內(nèi)主題風(fēng)格樣式的情況。

為了提前看到效果,先上效果圖:

前期準(zhǔn)備

1.為標(biāo)題欄titleView預(yù)定義id,在values下的ids.xml中

<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="tv_title_name" type="id"/>
<item name="tv_left_text" type="id"/>
<item name="iv_left_image" type="id"/>
<item name="iv_right_image" type="id"/>
<item name="iv_right_image_two" type="id"/>
<item name="tv_right_text" type="id"/>
<item name="tv_right_text_two" type="id"/>
</resources>

這里可以看到定義了左側(cè)返回按鈕id,標(biāo)題id,后側(cè)按鈕id,左側(cè)分兩種情況:ImageView/TextView,右側(cè)可以同時(shí)存在兩個(gè)按鈕,圖片按鈕、文字按鈕組合。

2.自定義標(biāo)題欄屬性,在valuse下的attr.xml中

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 標(biāo)題屬性 -->
<declare-styleable name="TitleAttr">
<attr name="title_name" format="reference|string"/>
<attr name="title_text_color" format="reference|color"/>
<attr name="title_drawable_right" format="reference"/>
<attr name="title_drawable_left" format="reference"/>
<attr name="title_text_size" format="reference|dimension"/>
<attr name="left_text" format="reference|string"/>
<attr name="left_image" format="reference"/>
<attr name="small_text_size" format="reference|dimension"/>
<attr name="title_gravity">
<enum name="left" value="3"/>
<enum name="center" value="17"/>
<enum name="right" value="5"/>
</attr>
<attr name="right_image" format="reference"/>
<attr name="right_text" format="reference|string"/>
<attr name="right_image_two" format="reference"/>
<attr name="right_text_two" format="reference|string"/>
<attr name="title_height" format="dimension"/>
<attr name="right_text_drawable_right" format="reference"/>
<attr name="right_text_drawable_left" format="reference"/>
<attr name="right_text_two_drawable_right" format="reference"/>
<attr name="right_text_two_drawable_left" format="reference"/>
<attr name="left_text_drawable_right" format="reference"/>
<attr name="left_text_drawable_left" format="reference"/>
</declare-styleable>
</resources>

•編碼實(shí)現(xiàn)

• 有了前面的準(zhǔn)備后,現(xiàn)在就可以開始編碼實(shí)現(xiàn)了,這里先看看在xml中如何引入我們自定義的控件:

<com.jarek.library.TitleView
android:id="@+id/title_main"
android:layout_width="match_parent"
android:background="#0093fe"
title:title_name="標(biāo)題"
title:right_text="@string/more"
title:title_text_color="@android:color/white"
title:right_image_two="@mipmap/icon_crop_rotate"
title:title_text_size="20sp"
title:small_text_size="15sp"
title:left_text="返回"
title:left_text_drawable_left="@mipmap/back_normal"
title:right_text_drawable_right="@mipmap/bar_button_right"
android:layout_height="50dp"/>

這里的title標(biāo)簽就是我們自定義,首先創(chuàng)建一個(gè)類繼承自RelativeLayout,這里選擇RelativeLayout作為父類容器,目的在于RelativeLayout便于控制相對位置。

首先我們要獲得TypedArray對象,所有自定義屬性的值通過它來獲取:

TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.TitleAttr); 

得到了typedArray對象后,就可以開始添加按鈕到容器中了,首先看看左邊第一個(gè)返回按鈕如果添加上去,先看代碼:

int leftText = typeArray.getResourceId(R.styleable.TitleAttr_left_text, 0);
CharSequence charSequence = leftText > 0 ? typeArray.getResources().getText(leftText) : typeArray.getString(R.styleable.TitleAttr_left_text);

這里left_text就是自定義屬性,表明是左側(cè)TextView顯示的文字,文字可以是應(yīng)用資源文件里的預(yù)定義string,也可以是直接輸入文字,取到對應(yīng)的styleable后,首先判斷是否大于0,大于0表示是定義在string中的,通過typeArray.getResources().getText()獲取值,等于0就直接取值,表示可能是通過直接賦值的方式給值的。取到值后怎么賦值給TextView,這里需要首先給他寬高,這是所有控件都需要的

/**
* layout params of RelativeLayout
* @return LayoutParams
*/
private LayoutParams initLayoutParams () {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
} 

我們單獨(dú)寫一個(gè)方法,后續(xù)就可以直接通過

LayoutParams params = initLayoutParams(); 

來獲取預(yù)設(shè)寬高值了,這里可以看到都是高度填充父控件,寬度是自適應(yīng)。然后就是new一個(gè)TextView了:

/**
* create TextView
* @param context Context
* @param id the id of TextView
* @param charSequence text to show
* @param params relative params
* @return the TextView which is inited
*/
@NonNull
private TextView createTextView(Context context, int id, CharSequence charSequence, LayoutParams params) {
TextView textView = new TextView(context);
textView.setLayoutParams(params);
textView.setGravity(Gravity.CENTER);
textView.setId(id);
textView.setMinWidth((int)getPixelSizeByDp(minViewWidth));
textView.setText(charSequence);
return textView;
} 

這里可以看到我們傳入了預(yù)設(shè)的id值,需要顯示的內(nèi)容,以及上面給定的LayoutParams 。創(chuàng)建好TextView后還可以設(shè)置TextView的Drawable,通過自定義屬性left_text_drawable_right,left_text_drawable_left設(shè)置,這里是設(shè)置了左右,上下對應(yīng)的可以設(shè)置:

/**
* drawable of TextView
* @param typeArray TypedArray
* @param leftDrawableStyleable leftDrawableStyleable
* @param rightDrawableStyleable rightDrawableStyleable
* @param textView which TextView to set
*/
private void setTextViewDrawable(TypedArray typeArray, int leftDrawableStyleable, int rightDrawableStyleable, TextView textView) {
int leftDrawable = typeArray.getResourceId(leftDrawableStyleable, 0);
int rightDrawable = typeArray.getResourceId(rightDrawableStyleable, 0);
textView.setCompoundDrawablePadding((int)getPixelSizeByDp(drawablePadding));
textView.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, 0, rightDrawable, 0);
} 

這里也是抽取了一個(gè)方法出來,然后通過:

setTextViewDrawable(typeArray, R.styleable.TitleAttr_left_text_drawable_left, R.styleable.TitleAttr_left_text_drawable_right, mLeftBackTextTv); 

即可給指定的TextView設(shè)置drawable了。創(chuàng)建好TextView后,前面提到我們用的是相對布局,需要指定位置規(guī)則:

params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 

這里居左顯示。同時(shí)還可以設(shè)置字體大小,通過自定義屬性:small_text_size(兩側(cè)文字大小),title_text_size(標(biāo)題文字大小)來設(shè)置字體:

/**
* get the dimension pixel size from typeArray which is defined in attr
* @param typeArray TypedArray
* @param stylable stylable
* @param defaultSize defaultSize
* @return the dimension pixel size
*/
private float getDimensionPixelSize(TypedArray typeArray, int stylable, int defaultSize) {
int sizeStyleable = typeArray.getResourceId(stylable, 0);
return sizeStyleable > 0 ? typeArray.getResources().getDimensionPixelSize(sizeStyleable) : typeArray.getDimensionPixelSize(stylable, (int)getPiselSizeBySp(defaultSize));
} 

一樣,這里也是單獨(dú)寫一個(gè)方法來做,TypedArray的用法就不多講了,可以查看其它文章了解。然后通過如下設(shè)置字體大小:

float textSize = getDimensionPixelSize(typeArray, R.styleable.TitleAttr_small_text_size, defaultSmallTextSize);
mLeftBackTextTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); 

文字顏色,同樣的道理:

/**
* get textColor
* @param typeArray TypedArray
* @return textColor
*/
private int getTextColorFromAttr (TypedArray typeArray) {
int textColorStyleable = typeArray.getResourceId(R.styleable.TitleAttr_title_text_color, 0);
if (textColorStyleable > 0) {
return typeArray.getResources().getColor(textColorStyleable);
} else {
return typeArray.getColor(R.styleable.TitleAttr_title_text_color, textColor);
}
} 

然后調(diào)用方法設(shè)置顏色:

mLeftBackTextTv.setTextColor(getTextColorFromAttr(typeArray)); 

到這里為止,左側(cè)的第一個(gè)文字按鈕?;蛘呶淖謳D片的按鈕就創(chuàng)建好了,最后就差一步了:

mLeftBackTextTv.setTextColor(getTextColorFromAttr(typeArray)); 

其它按鈕,同樣的道理,可以依次添加到容器中,就不多講了,到此為止我們需要的titleView就創(chuàng)建好了,以后使用就可以直接調(diào)用了,不需要每個(gè)地方去重復(fù)的coding。

項(xiàng)目地址:github源碼下載

以上所述是小編給大家介紹的Android自定義TitleView標(biāo)題開發(fā)實(shí)例,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Android中判斷網(wǎng)絡(luò)是否連接實(shí)例詳解

    Android中判斷網(wǎng)絡(luò)是否連接實(shí)例詳解

    這篇文章主要介紹了Android中判斷網(wǎng)絡(luò)是否連接實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Android時(shí)間設(shè)置的3個(gè)小彩蛋分享

    Android時(shí)間設(shè)置的3個(gè)小彩蛋分享

    這篇文章主要給大家介紹了關(guān)于Android時(shí)間設(shè)置的3個(gè)小彩蛋,文中通過實(shí)例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-03-03
  • android viewpager實(shí)現(xiàn)豎直滑動效果

    android viewpager實(shí)現(xiàn)豎直滑動效果

    這篇文章主要為大家詳細(xì)介紹了android viewpager實(shí)現(xiàn)豎直滑動效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android實(shí)現(xiàn)時(shí)鐘特效

    Android實(shí)現(xiàn)時(shí)鐘特效

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)時(shí)鐘特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • Android三種雙屏異顯實(shí)現(xiàn)方法介紹

    Android三種雙屏異顯實(shí)現(xiàn)方法介紹

    現(xiàn)在越來越多的Android設(shè)備有多個(gè)屏幕,雙屏異顯應(yīng)用場景最多的應(yīng)該就是類似于收銀平臺那種設(shè)備,在主屏上店員能夠?qū)c(diǎn)商品進(jìn)行選擇錄入,副屏則是展示給我們的賬單詳情,但是它只通過了一個(gè)軟件系統(tǒng)就實(shí)現(xiàn)了雙屏異顯這個(gè)功能,而Presentation正是這其中的關(guān)鍵
    2023-01-01
  • Eclipse安裝ADT插件速度慢的解決方法

    Eclipse安裝ADT插件速度慢的解決方法

    這篇文章主要介紹了Eclipse安裝ADT插件速度慢的解決方法,涉及基于Apache的反向代理與hosts文件的設(shè)置方法,需要的朋友可以參考下
    2015-12-12
  • Android打開系統(tǒng)相機(jī)并拍照的2種顯示方法

    Android打開系統(tǒng)相機(jī)并拍照的2種顯示方法

    這篇文章主要介紹了Android打開系統(tǒng)相機(jī)并拍照的2種顯示方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Input系統(tǒng)之InputReader處理合成事件詳解

    Input系統(tǒng)之InputReader處理合成事件詳解

    這篇文章主要為大家介紹了Input系統(tǒng)之InputReader處理合成事件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android實(shí)現(xiàn)美團(tuán)外賣底部導(dǎo)航欄動畫

    Android實(shí)現(xiàn)美團(tuán)外賣底部導(dǎo)航欄動畫

    這篇文章主要介紹了Android實(shí)現(xiàn)美團(tuán)外賣底部導(dǎo)航欄動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android自定義view實(shí)現(xiàn)電影票在線選座功能

    Android自定義view實(shí)現(xiàn)電影票在線選座功能

    這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)選座功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11

最新評論