新聞中心
小編給大家分享一下Android創(chuàng)建自定義ActionBar的方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
當(dāng)多個(gè)界面都有很多相似部分時(shí),可以考慮創(chuàng)建一個(gè)功能較全的模板。而在需要時(shí),可以通過(guò)引用模板來(lái)實(shí)現(xiàn)自己想要實(shí)現(xiàn)的功能。比如適配器 Adapter,當(dāng)很多的適配器都差不多時(shí),就可以通過(guò)打造一個(gè)通用的適配器來(lái)實(shí)現(xiàn)。本例中主要是如何創(chuàng)建自定義的 ActionBar。
觀察上圖的,當(dāng)切換界面時(shí),每個(gè)界面的頂部最多只有兩個(gè)圖標(biāo),而且有4個(gè)界面具有類似特性。所以可以考慮通過(guò)自定義控件來(lái)創(chuàng)建UI模板。
由于是需要?jiǎng)?chuàng)建出具有重用功能的復(fù)合控件,所以通常需要繼承 ViewGroup ,在給它添加指定功能的控制。給其指定一些可配置的屬性,讓其具有更強(qiáng)的擴(kuò)展性。
本例可以簡(jiǎn)單的創(chuàng)建一個(gè) TopBar 來(lái)繼承 RelativeLayout,并在 values 文件下新建一個(gè) attrs.xml 布局文件,該文件用于定義 ActionBar 的屬性。
attrs.xml :
其中:
創(chuàng)建一個(gè)只有兩張圖片的布局文件,這樣做的好處是在自定義控件的類中可以減少代碼量,不必在該類中創(chuàng)建 ImageView ,也能更好的讓 xml 完成 UI 界面設(shè)置,而 Java 程序則專門(mén)負(fù)責(zé)業(yè)務(wù)邏輯。
topbar_layout.xml :
該布局只有兩個(gè)橫向的 ImageView 且都沒(méi)有指定 src 屬性。
創(chuàng)建一個(gè) TopBar 類用于繼承 RelativeLayout。
TopBar.java :
package com.crazy.gemi.ui.topbar; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; import com.crazy.gemi.R; public class TopBar extends RelativeLayout { private Drawable draw_left; private Drawable draw_right; public TopBar(Context context) { this(context, null); } public TopBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public TopBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs, defStyleAttr); } private void init(AttributeSet attrs, int defStyleAttr) { // 系統(tǒng)提供了 TypedArray 來(lái)獲取自定義的屬性集 TypedArray typedArray = null; try { typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TopBar,defStyleAttr,-1); draw_left = typedArray.getDrawable(R.styleable.TopBar_topbar_left_icon); draw_right = typedArray.getDrawable(R.styleable.TopBar_topbar_right_icon); } finally { // 獲取完所有的屬性值后要回收資源 typedArray.recycle(); } View view = View.inflate(getContext(), R.layout.topbar_layout, this); ImageView imgLeft = (ImageView)view.findViewById(R.id.topbar_left_img); ImageView imgRight = (ImageView)view.findViewById(R.id.topbar_right_img); imgLeft.setImageDrawable(draw_left); imgRight.setImageDrawable(draw_right); } }
其中需要注意的是:
1. 獲取完屬性值后,要記得回收資源。將其放入 finally 語(yǔ)句塊中,就一定能夠回收,不管前面是否出問(wèn)題等。
2. 先加載該布局文件:View view = View.inflate(getContext(), R.layout.topbar_layout, this); 其中的 this 為該 TopBar 對(duì)象的引用,將其添加到 RelativeLayout 中;給圖片賦值,如:imgLeft.setImageDrawable(draw_left);
由此可以看出避免了在該類中出現(xiàn) ImageView imgLeft = new ImageView(content); 的創(chuàng)建 ImageView 對(duì)象的代碼,也避免可為組件元素設(shè)置相應(yīng)的布局元素的問(wèn)題,如:
// 為組件設(shè)置相應(yīng)的布局元素(左邊) LayoutParams leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE); // 添加到 ViewGroup addView(imgLeft, leftParams); // 為組件設(shè)置相應(yīng)的布局元素(右邊) LayoutParams rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE); // 添加到 ViewGroup addView(imgRight,rightParams);
當(dāng)然該自定義的空間還不完善,可以在該類中添加接口,以方便點(diǎn)擊圖標(biāo)時(shí)有相應(yīng)的回調(diào)。這里也就沒(méi)有去創(chuàng)建該接口了。
接下來(lái)就是在需要的引用該模板:
先創(chuàng)建自己的名字空間:xmlns:custom="http://schemas.android.com/apk/res-auto" 其中 custom 為自定義的名字,res-auto 也可以改為該應(yīng)用的包名。下面簡(jiǎn)單創(chuàng)建一個(gè)布局,以此來(lái)演示對(duì)該 UI 模板的引用。
效果如下:
代碼如下:
其中用 custom:topbar_left_icon="" 來(lái)加載自己想要加載的圖片(左邊的圖標(biāo))。這樣就可以通過(guò)添加或者不添加 custom 屬性來(lái)實(shí)現(xiàn)完對(duì) UI 模板的引用。
看完了這篇文章,相信你對(duì)“Android創(chuàng)建自定義ActionBar的方法”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
標(biāo)題名稱:Android創(chuàng)建自定義ActionBar的方法-創(chuàng)新互聯(lián)
文章起源:http://fisionsoft.com.cn/article/ippco.html