Android自定義View之組合控件實(shí)現(xiàn)類似電商app頂部欄
本文實(shí)例為大家分享了Android自定義View之組合控件,仿電商app頂部欄的相關(guān)代碼,供大家參考,具體內(nèi)容如下
效果圖:
分析:左右兩邊可以是TextView和Button,設(shè)置drawableTop即可,中間的看著像是EditText,但是用過淘寶天貓等類似app的話會(huì)發(fā)現(xiàn)點(diǎn)擊搜索不是在當(dāng)前Activit進(jìn)行搜索的,是跳轉(zhuǎn)到另外的頁(yè)面進(jìn)行的,所以用TextView然后設(shè)置背景即可. 實(shí)現(xiàn)流程
參數(shù)列表:
設(shè)置屬性文件:values下建立attrs.xml文件,添加需要自定義的屬性.
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TopBar"> <attr name="left_text" format="string" />// 左邊文字 <attr name="right_text" format="string" /> // 右邊文字 <attr name="center_text" format="string" />// 中間文字 <attr name="side_text_size" format="dimension" />//邊界按鈕大小 <attr name="center_text_size" format="dimension" />//中間文字大小 <attr name="text_color" format="color" />//文字顏色 <attr name="back_color" format="color" />//背景顏色 <attr name="left_icon" format="reference" />//左邊的icon <attr name="right_icon" format="reference" />//右邊的icon <attr name="center_icon" format="reference" />//中間的icon </declare-styleable> </resources>
代碼中獲取布局文件中設(shè)置的屬性:
TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.TopBar); mLeftText = array.getString(R.styleable.TopBar_left_text); mRightText = array.getString(R.styleable.TopBar_right_text); mCenterText = array.getString(R.styleable.TopBar_center_text); side_text_size = (int) array.getDimension(R.styleable.TopBar_side_text_size, 10); center_text_size = (int) array.getDimension(R.styleable.TopBar_center_text_size, 15); mLeft_icon = array.getDrawable(R.styleable.TopBar_left_icon); mRight_icon = array.getDrawable(R.styleable.TopBar_right_icon); mCenter_icon = array.getDrawable(R.styleable.TopBar_center_icon); text_color = array.getColor(R.styleable.TopBar_text_color, getResources().getColor(R.color.colorAccent)); back_color = array.getColor(R.styleable.TopBar_back_color, getResources().getColor(R.color.colorPrimary)); array.recycle();
設(shè)置背景顏色:
setBackgroundColor(back_color);
添加按鈕:
//設(shè)置內(nèi)容 mLeftButton = new Button(getContext());//創(chuàng)建按鈕 mLeftButton.setText(mLeftText);//設(shè)置文字 mLeftButton.setTextSize(side_text_size);//設(shè)置文字大小 mLeftButton.setTextColor(text_color);//設(shè)置文字顏色 mLeftButton.setBackgroundColor(Color.TRANSPARENT);//設(shè)置按鈕的背景為透明 LayoutParams leftParams = new LayoutParams(80, 150);//設(shè)置布局 mLeft_icon.setBounds(0, 0, 55, 55); //設(shè)置icon的大小 mLeftButton.setCompoundDrawables(null, mLeft_icon, null, null); //添加icon mLeftButton.setGravity(Gravity.CENTER);//設(shè)置置中 addView(mLeftButton, leftParams);//添加按鈕 //右按鈕類似,就不加注釋了 mRightButton = new Button(getContext()); mRightButton.setText(mRightText); mRightButton.setTextSize(side_text_size); mRightButton.setTextColor(text_color); mRightButton.setBackgroundColor(Color.TRANSPARENT); mRight_icon.setBounds(0, 0, 55, 55); LayoutParams rightParams = new LayoutParams(80, 150); mRightButton.setCompoundDrawables(null, mRight_icon, null, null); mRightButton.setGravity(Gravity.CENTER); addView(mRightButton, rightParams);
添加中間的TextView:(布局的排列是按添加順序的,所以中間TextVIew的添加應(yīng)該是在兩個(gè)按鈕中間的):
mCenterTextView = new TextView(getContext());//初始化TextView mCenterTextView.setText(mCenterText);//設(shè)置文字 mCenterTextView.setTextSize(center_text_size);//設(shè)置文字大小 mCenterTextView.setTextColor(text_color);//設(shè)置文字顏色 mCenterTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);//設(shè)置文字靠左 mCenter_icon.setBounds(0, 0, 50, 50);//設(shè)置icon大小 mCenterTextView.setCompoundDrawables(mCenter_icon, null, null, null);//添加icon LayoutParams params = new LayoutParams(0, 70);//創(chuàng)建布局屬性 mCenterTextView.setBackground(getResources().getDrawable(R.drawable.bg_search));//設(shè)置背景 params.weight = 1;//設(shè)置權(quán)重 params.gravity = Gravity.CENTER;//設(shè)置居中 params.setMargins(10, 0, 10, 0);//設(shè)置邊界 addView(mCenterTextView, params);//添加
處理高度的wrap_content屬性:
重寫onMeasure屬性,對(duì)wrap_content設(shè)置一個(gè)指定值
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int specWidth = MeasureSpec.getSize(widthMeasureSpec);//獲取寬度 int specHeight = MeasureSpec.getSize(heightMeasureSpec);//獲取高度 int heightMode = MeasureSpec.getMode(heightMeasureSpec);//獲取高度的測(cè)量模式 int height = 0;//初始化要設(shè)置的高度 if (heightMode == MeasureSpec.EXACTLY) {//如果是確定的值,包括match_parent height = specHeight; //最終的值即為測(cè)量值 } else { height = 120; //如果不是確定的值就設(shè)置為指定的高度 if (heightMode == MeasureSpec.AT_MOST) {//如果是wrap_content就取測(cè)量值和指定值得最小值作為最終的值 height = Math.min(specHeight, 120); } } setMeasuredDimension(specWidth, height);//設(shè)置寬高屬性 }
添加點(diǎn)擊事件:
需要自定義一個(gè)回調(diào)
public interface onClick { void onLeftButtonClick(); void onCenterButtonClick(); void onRightButtonClick(); }
創(chuàng)建一個(gè)回調(diào)并創(chuàng)建setX方法
private onClick onClick; public void setOnClick(TopBar.onClick onClick) { this.onClick = onClick; }
在添加按鈕的時(shí)候添加點(diǎn)擊事件
mLeftButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (onClick != null) { onClick.onLeftButtonClick(); } } }); mCenterTextView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (onClick != null) { onClick.onCenterButtonClick(); } } }); mRightButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (onClick != null) { onClick.onRightButtonClick(); } } });
至此自定義的組合控件就完成了,下面貼一下使用的方法:
布局文件:
<com.brioa.diyviewtest.view.TopBar xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/topBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" app:center_icon="@mipmap/ic_search" app:center_text="輸入關(guān)鍵字檢索" app:center_text_size="10sp" app:left_icon="@mipmap/ic_scan" app:left_text="掃一掃" app:right_icon="@mipmap/ic_msg" app:right_text="消息" app:side_text_size="6sp" app:text_color="#ffff"> </com.brioa.diyviewtest.view.TopBar>
代碼設(shè)置:
mTopBar = (TopBar) findViewById(R.id.topBar); mTopBar.setOnClick(new TopBar.onClick() { @Override public void onLeftButtonClick() { Toast.makeText(mContext, "LeftClick", Toast.LENGTH_SHORT).show(); } @Override public void onCenterButtonClick() { Toast.makeText(mContext, "CenterClick", Toast.LENGTH_SHORT).show(); } @Override public void onRightButtonClick() { Toast.makeText(mContext, "RightClick", Toast.LENGTH_SHORT).show(); } });
最終效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助,也希望大家多多支持腳本之家。
- Android自定義控件之自定義組合控件(三)
- Android自定義控件之組合控件學(xué)習(xí)筆記分享
- Android組合控件實(shí)現(xiàn)功能強(qiáng)大的自定義控件
- Android自定義組合控件之自定義下拉刷新和左滑刪除實(shí)例代碼
- 實(shí)例講解Android應(yīng)用中自定義組合控件的方法
- Android中View自定義組合控件的基本編寫方法
- 在Android開發(fā)中使用自定義組合控件的例子
- 探究Android中ListView復(fù)用導(dǎo)致布局錯(cuò)亂的解決方案
- Android自定義控件之繼承ViewGroup創(chuàng)建新容器
- Android自定義控件之創(chuàng)建可復(fù)用的組合控件
相關(guān)文章
Android實(shí)現(xiàn)登陸界面的記住密碼功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)登陸界面的記住密碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04android中打開相機(jī)、打開相冊(cè)進(jìn)行圖片的獲取示例
本篇文章主要介紹了android中打開相機(jī)、打開相冊(cè)進(jìn)行圖片的獲取示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01Android中使用二級(jí)緩存、異步加載批量加載圖片完整案例
這篇文章主要介紹了Android中使用二級(jí)緩存、異步加載批量加載圖片完整案例,本文講解了實(shí)現(xiàn)的過程以及核心代碼展示,并給出了完整項(xiàng)目源碼,需要的朋友可以參考下2015-06-06android實(shí)現(xiàn)簡(jiǎn)單拍照功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡(jiǎn)單拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android帶進(jìn)度條的文件上傳示例(使用AsyncTask異步任務(wù))
這篇文章主要介紹了Android帶進(jìn)度條的文件上傳示例(使用AsyncTask異步任務(wù)),使用起來(lái)比較方便,將幾個(gè)方法實(shí)現(xiàn)就行,感興趣的小伙伴們可以參考一下。2016-11-11Android Jetpack導(dǎo)航組件Navigation創(chuàng)建使用詳解
這篇文章主要為大家介紹了Android Jetpack導(dǎo)航組件Navigation創(chuàng)建及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11