Android創(chuàng)建外部lib庫(kù)及自定義View的圖文教程
前言
隨著插件化/組件化的快速發(fā)展,現(xiàn)在大部分的項(xiàng)目開發(fā)中都會(huì)提取公共的代碼制作成 Library module,根據(jù)具體的業(yè)務(wù)需求進(jìn)行拆分。小菜也學(xué)習(xí)一下如何拆分 lib 包,實(shí)際操作很簡(jiǎn)單,整理一下操作步驟。
拆分創(chuàng)建 Library
在當(dāng)前 Project 下,F(xiàn)ile -> New Module,選擇 Android Library,進(jìn)行下一步;
設(shè)置具體的 Library/Module/Package 等名稱,注意:Module 名稱與 Library 相匹配默認(rèn)為小寫,需要的話手動(dòng)調(diào)整,進(jìn)行下一步;
此時(shí)在當(dāng)前 Project 中就已經(jīng)創(chuàng)建好 Library;
在當(dāng)前 Project 的 settings.gradle 中就會(huì)自動(dòng)生成創(chuàng)建的 Module;
Tips: :myview 中的 : 代表的與 app 同級(jí)目錄下的 Module。
在當(dāng)前 app 的 build.gradle 中 dependencies{} 中添加 implementation project(':myview') 即可正常接入。
自定義 View
小菜在新建的 Library 中添加一個(gè)自定義按鈕,可以添加配置圖標(biāo)和文字以及背景樣式。因?yàn)橹皇菫榱藴y(cè)試 Library Module,所以功能很簡(jiǎn)單,實(shí)現(xiàn)方式也很簡(jiǎn)單,只是幾個(gè)基本控件的組合。小菜只是簡(jiǎn)單的整理一下。
1、新建一個(gè) MyView 繼承自 RelativeLayout,實(shí)現(xiàn)基本的構(gòu)造方法;
2、在構(gòu)造方法中實(shí)現(xiàn)對(duì)布局的添加,控件的綁定以及一些基本的 setXX 方法;
3、至此 MyView 就可以應(yīng)用,但所有但屬性都需要通過(guò) setXX 方法來(lái)設(shè)置;這當(dāng)然是不合理的,于是小菜新建一個(gè) attrs 文件,在資源文件中設(shè)置基本的樣式,并在 MyView 的 obtainAttributes 方法中逐一綁定即可;
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <declare-styleable name="my_view" tools:ignore="MissingDefaultResource"> <!-- 中間文字顏色 --> <attr name="tv_color" format="color" /> <!-- 中間文字顯隱性 --> <attr name="tv_show" format="boolean" /> <!-- 中間文字內(nèi)容 --> <attr name="tv_str" format="string" /> <!-- 中間文字大小 --> <attr name="tv_size" format="float" /> <!-- 右側(cè)文字顏色 --> <attr name="right_tv_color" format="color" /> <!-- 右側(cè)文字顯隱性 --> <attr name="right_tv_show" format="boolean" /> <!-- 右側(cè)文字內(nèi)容 --> <attr name="right_tv_str" format="string" /> <!-- 右側(cè)文字大小 --> <attr name="right_tv_size" format="float" /> <!-- 整體背景顏色 --> <attr name="bg_color" format="color" /> <!-- 整體邊框顏色 --> <attr name="strok_color" format="color" /> <!-- 整體邊框圓角 --> <attr name="bg_radius" format="float" /> <!-- 中間圖片顯隱性 --> <attr name="iv_show" format="boolean" /> <!-- 中間圖片資源 --> <attr name="iv_src" format="reference" /> </declare-styleable> </resources>
4、至此,MyView 自定義按鈕以及完成,在 app 中也是正常調(diào)用即可。
public class MyView extends RelativeLayout { private Context mContext; private RelativeLayout mRlay; private ImageView mIv; private TextView mTv, mRightTv; GradientDrawable drawable = new GradientDrawable(); int mTvColor, mRightTvColor, mRlayBgColor, mStrokeColor, mIvSrc; boolean isTvShow, isRightTvShow, isIvShow; float mTvSize, mRightTvSize, mRadiusSize; String mTvStr, mRightTvStr; public MyView(Context context) { super(context); mContext = context; initView(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; initView(); obtainAttributes(context,attrs); } private void initView() { LayoutInflater.from(mContext).inflate(R.layout.my_view_btn, this,true); mRlay = findViewById(R.id.my_view_rly); mIv = findViewById(R.id.my_view_iv); mTv = findViewById(R.id.my_view_tv); mRightTv = findViewById(R.id.my_view_rtv); } private void obtainAttributes(Context context, AttributeSet attrs) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.my_view); mTvColor = ta.getColor(R.styleable.my_view_tv_color, Color.BLACK); mTv.setTextColor(mTvColor); mRightTvColor = ta.getColor(R.styleable.my_view_right_tv_color, Color.BLACK); mRightTv.setTextColor(mRightTvColor); mRlayBgColor = ta.getColor(R.styleable.my_view_bg_color, Color.WHITE); mRlay.setBackgroundColor(mRlayBgColor); mStrokeColor = ta.getColor(R.styleable.my_view_strok_color, Color.BLACK); isIvShow = ta.getBoolean(R.styleable.my_view_iv_show, true); mIv.setVisibility(isIvShow?View.VISIBLE:View.GONE); isRightTvShow = ta.getBoolean(R.styleable.my_view_right_tv_show, true); mRightTv.setVisibility(isRightTvShow?View.VISIBLE:View.GONE); isTvShow = ta.getBoolean(R.styleable.my_view_tv_show, true); mTv.setVisibility(isTvShow?View.VISIBLE:View.GONE); mTvSize = ta.getFloat(R.styleable.my_view_tv_size, 16.0f); mTv.setTextSize(mTvSize); mRightTvSize = ta.getFloat(R.styleable.my_view_right_tv_size, 14.0f); mRightTv.setTextSize(mRightTvSize); mRadiusSize = ta.getFloat(R.styleable.my_view_bg_color, 80.0f); drawable = (GradientDrawable) getResources().getDrawable(R.drawable.user_login_corner_qq); drawable.setCornerRadius(mRadiusSize); drawable.setStroke(1, mStrokeColor); drawable.setColor(mRlayBgColor); mRlay.setBackground(drawable); mTvStr = ta.getString(R.styleable.my_view_tv_str); mTv.setText(mTvStr); mRightTvStr = ta.getString(R.styleable.my_view_right_tv_str); mRightTv.setText(mRightTvStr); mIvSrc = ta.getResourceId(R.styleable.my_view_iv_src, R.mipmap.user_login_icon_qq); mIv.setImageResource(mIvSrc); ta.recycle(); } public void setMyViewTv(String textStr) { mTv.setText(textStr); } public void setMyViewTvColor(int color) { mTv.setTextColor(color); } public void setMyViewTvSize(float size) { mTv.setTextSize(size); } public void isMyViewTvShow(boolean state) { mTv.setVisibility(state ? View.VISIBLE : View.GONE); } public void setMyViewIv(Drawable drawable) { mIv.setImageDrawable(drawable); } public void isMyViewIvShow(boolean state) { mIv.setVisibility(state ? View.VISIBLE : View.GONE); } public void isMyViewRightTvShow(boolean state) { mRightTv.setVisibility(state ? View.VISIBLE : View.GONE); } public void setMyViewRightTvText(String textStr) { mRightTv.setText(textStr); } public void setMyViewRightTvSize(float size) { mRightTv.setTextSize(size); } public void setMyViewRightTvColor(int color) { mRightTv.setTextColor(color); } public void setMyViewBgColor(int color) { drawable.setColor(color); mRlay.setBackground(drawable); } public void setMyViewBgRadius(float radius) { drawable.setCornerRadius(radius); mRlay.setBackground(drawable); } public void setMyViewBgStrokeColor(int color) { drawable.setStroke(1, color); mRlay.setBackground(drawable); } public void setMyViewBgDrawable(Drawable drawable) { mRlay.setBackground(drawable); } }
Tips: attrs.xml 中如果需要用到資源文件,可以使用 format="reference",代表某一個(gè)資源ID。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android事件分發(fā)機(jī)制深入刨析原理及源碼
Android?的事件分發(fā)機(jī)制大體可以分為三部分:事件生產(chǎn)、事件分發(fā)?、事件消費(fèi)。事件的生產(chǎn)是由用戶點(diǎn)擊屏幕產(chǎn)生,我們這次著重分析事件的分發(fā)和消費(fèi),因?yàn)槭录职l(fā)和處理聯(lián)系的過(guò)于緊密,這篇文章將把事件的分發(fā)和消費(fèi)放在一起分析2023-04-04android Matrix實(shí)現(xiàn)圖片隨意放大縮小或拖動(dòng)
這篇文章主要為大家詳細(xì)介紹了android Matrix實(shí)現(xiàn)圖片隨意放大縮小或拖動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android studio 混淆+打包+驗(yàn)證是否成功
本文主要介紹了Android studio 混淆+打包+驗(yàn)證是否成功的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03Android仿活動(dòng)時(shí)分秒倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android仿活動(dòng)時(shí)分秒倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02Android UI:ListView - SimpleAdapter實(shí)例詳解
這篇文章主要介紹了Android UI:ListView - SimpleAdapter實(shí)例詳解,SimpleAdapter是擴(kuò)展性最好的適配器,可以定義各種你想要的布局,而且使用很方便,需要的朋友可以參考下2016-11-11Android使用Service實(shí)現(xiàn)簡(jiǎn)單音樂(lè)播放實(shí)例
這篇文章主要為大家詳細(xì)介紹了Android使用Service實(shí)現(xiàn)簡(jiǎn)單音樂(lè)播放實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05