編輯:關於 Android 程式設計
本文將分享如何用組合控件實作一個類似電商 App 的頂部搜尋欄,附上完整程式碼,供需要的朋友參考。
效果圖:

分析:左右兩邊可以是 TextView 或 Button,透過設置 drawableTop 即可;中間看起來像 EditText,但只要用過淘寶、天貓這類 App 就會發現,點擊搜尋並不是在當前 Activity 進行,而是跳轉到另一個頁面,因此這裡改用 TextView 並設置背景即可。
實作流程

參數列表:

設置屬性檔:在 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>
在程式碼中取得版面配置檔裡設置的屬性:
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();
設置背景顏色:
setBackgroundColor(back_color);
添加按鈕:
//設置內容 mLeftButton = new Button(getContext());//創建按鈕 mLeftButton.setText(mLeftText);//設置文字 mLeftButton.setTextSize(side_text_size);//設置文字大小 mLeftButton.setTextColor(text_color);//設置文字顏色 mLeftButton.setBackgroundColor(Color.TRANSPARENT);//設置按鈕的背景為透明 LayoutParams leftParams = new LayoutParams(80, 150);//設置布局 mLeft_icon.setBounds(0, 0, 55, 55); //設置icon的大小 mLeftButton.setCompoundDrawables(null, mLeft_icon, null, null); //添加icon mLeftButton.setGravity(Gravity.CENTER);//設置置中 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 應該加在兩個按鈕之間):
mCenterTextView = new TextView(getContext());//初始化TextView mCenterTextView.setText(mCenterText);//設置文字 mCenterTextView.setTextSize(center_text_size);//設置文字大小 mCenterTextView.setTextColor(text_color);//設置文字顏色 mCenterTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);//設置文字靠左 mCenter_icon.setBounds(0, 0, 50, 50);//設置icon大小 mCenterTextView.setCompoundDrawables(mCenter_icon, null, null, null);//添加icon LayoutParams params = new LayoutParams(0, 70);//創建布局屬性 mCenterTextView.setBackground(getResources().getDrawable(R.drawable.bg_search));//設置背景 params.weight = 1;//設置權重 params.gravity = Gravity.CENTER;//設置居中 params.setMargins(10, 0, 10, 0);//設置邊界 addView(mCenterTextView, params);//添加
處理高度的 wrap_content 屬性:
重寫 onMeasure,為 wrap_content 設置一個指定值。
@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);//獲取高度的測量模式 int height = 0;//初始化要設置的高度 if (heightMode == MeasureSpec.EXACTLY) {//如果是確定的值,包括match_parent height = specHeight; //最終的值即為測量值 } else { height = 120; //如果不是確定的值就設置為指定的高度 if (heightMode == MeasureSpec.AT_MOST) {//如果是wrap_content就取測量值和指定值得最小值作為最終的值 height = Math.min(specHeight, 120); } } setMeasuredDimension(specWidth, height);//設置寬高屬性 }
添加點擊事件:需要自訂一個回調。
public interface onClick { void onLeftButtonClick(); void onCenterButtonClick(); void onRightButtonClick(); }
建立一個回調並建立 setX 方法:
private onClick onClick; public void setOnClick(TopBar.onClick onClick) { this.onClick = onClick; }
在添加按鈕時加入點擊事件:
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="https://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="輸入關鍵字檢索" 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>
程式碼設置:
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(); } });
最終效果:

以上就是本文的全部內容,希望對大家學習 Android 程式開發有所幫助,也歡迎多多支持本站。
Android Activity切換與數據回傳完整解析
在Android開發中,Activity之間切換與資料傳遞是非常基礎但重要的能力。若要從主頁面開啟另一個頁面,並在對方完成操作後把結果帶回來,通常會使用Intent搭配s
Android SQLite 資料庫完整實例教學
Android 作為目前主流的行動作業系統,與 SQLite 佔用資源少的優勢十分契合,因此平台內建了一個嵌入式關聯式資料庫——SQLite。若需要在 Android 上
Android 應用訪問 Linux 驅動的第一步:編寫並測試一個字符設備驅動
一直想找機會親手試一次「用 Android 應用訪問 Linux 內核驅動」,卻總被各種事情耽擱。最近終於騰出時間,下定決心重新試一試。整個嘗試自然要從一個 Linux
Android Service 兩種啟動模式與音樂播放案例
Service 可以在背景執行不與使用者互動的工作,例如播放音樂、下載檔案或處理資料同步。它必須在 AndroidManifest.xml 中宣告,常見啟動方式有 sta