Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 透過 BaseActivity 打造自訂標題欄

編輯:關於 Android 程式設計

在開發專案時,常常會遇到這樣的需求:標題欄的文字要置中,樣式、字體大小都得自訂;左側放返回鍵與關閉鍵,右側再擺一個提交鍵,而且這個提交鍵要能依情況顯示或隱藏。原生 Title 的文字預設靠左,若直接替 TitleBar 設定自訂 View,往往又會出現標題不置中、標題欄過高等惱人問題。

最終我們想要的樣式如下,右側的按鈕可以依需求顯示或隱藏:

權衡之後,乾脆自己寫一個 BaseActivity,讓所有頁面都繼承這個基底類別,再統一自訂標題欄樣式即可。以下說明這個介面是如何實作的。

首先定義一個 BaseActivity 類別:

 public class BaseActivity extends AppCompatActivity implements View.OnClickListener{
 private TextView mTitleTextView;//標題
 private TextView close_tv;//
 protected TextView commint_tv;
 @Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
 //將界面加入到棧中,方便管理
 MyApplication.getInstance().addActivity(this);
 initViews();
 }
 private void initViews() {
 super.setContentView(R.layout.activity_abstract_title);
 mTitleTextView = (TextView) findViewById(R.id.action_bar_title_tv);
 mContentLayout = (FrameLayout) findViewById(R.id.layout_content);
 close_tv = ((TextView) findViewById(R.id.action_bar_close_tv));
 ImageView back_ic = (ImageView) findViewById(R.id.action_bar_back_iv);
 commint_tv = (TextView) findViewById(R.id.action_bar_comint_tv);
 back_ic.setOnClickListener(this);
 mTitleTextView.setOnClickListener(this);
 }
 // 返回鍵返回事件
 @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
 if (KeyEvent.KEYCODE_BACK == keyCode) {
 onBackPressed();
 }
 return super.onKeyDown(keyCode, event);
 }
 public boolean onTouchEvent(MotionEvent event) {
 if(null != this.getCurrentFocus()){
 /**
 * 點擊空白位置 隱藏軟鍵盤
 */
 InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
 return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
 }
 return super .onTouchEvent(event);
 }
 /**
 * 顯示關閉按鈕
 */
 public void showCloseBT(){
 if (close_tv!=null){
 close_tv.setVisibility(View.VISIBLE);
 close_tv.setOnClickListener(this);
 }
 }
 /**
 * 顯示提交按鈕按鈕
 */
 public void showCommintBT(String s){
 if (commint_tv!=null){
 commint_tv.setVisibility(View.VISIBLE);
 commint_tv.setOnClickListener(this);
 commint_tv.setText(s);
 }
 }
 /**
 * 返回按鈕點擊後觸發
 */
 protected void onLeftBackward() {
 onBackPressed();
 }
 /**
 * 右邊提交按鈕點擊後觸發
 */
 protected void onRightForward() {
 }
 /**
 * 提交關閉按鈕點擊後觸發
 */
 protected void onLeftCloseword(){
 Intent intent = new Intent(this, MainActivity.class);
 intent.putExtra("tabpos", 2);
 startActivity(intent);
 finish();
 }
 //設置標題內容
 @Override public void setTitle(int titleId) {
 mTitleTextView.setText(titleId);
 }
 //設置標題內容
 @Override public void setTitle(CharSequence title) {
 mTitleTextView.setText(title);
 }
 //點擊標題時出發的事件操作
 public void onTitle() {
 }
 //取出FrameLayout並調用父類removeAllViews()方法
 @Override public void setContentView(int layoutResID) {
 mContentLayout.removeAllViews();
 View.inflate(this, layoutResID, mContentLayout);
 onContentChanged();
 }
 @Override public void setContentView(View view){
 mContentLayout.removeAllViews();
 mContentLayout.addView(view);
 onContentChanged();
 }
 @Override public void onClick(View v) {
 switch (v.getId()) {
 case R.id.action_bar_back_iv:
 onLeftBackward();
 break;
 case R.id.action_bar_comint_tv:
 onRightForward();
 break;
 case R.id.action_bar_close_tv:
 onLeftCloseword();
 case R.id.action_bar_title_tv:
 onTitle();
 default:
 break;
 }
 }
 } 

如此一來,其他 Activity 繼承 BaseActivity 時,只需決定要顯示哪些按鈕即可,標題欄各按鈕的點擊事件都已在基底類別中處理好,子類別只要按需覆寫
onLeftBackward()、onRightForward()、onTitle() 等方法,填入各自的邏輯即可。

接著是版面檔 activity_abstract_title.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <!-- Title -->
 <include layout="@layout/actionbar_layout" />
 <FrameLayout
 android:id="@+id/layout_content"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#fff" >
 </FrameLayout>
 </LinearLayout> 

以及 actionbar_layout.xml 檔:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:id="@+id/layout_titlebar"
 android:layout_width="match_parent"
 android:layout_height="40dp"
 android:background="#2B2B2B">
 <TextView
 android:id="@+id/action_bar_title_tv"
 android:layout_width="180dp"
 android:layout_height="match_parent"
 android:ellipsize="marquee"
 android:gravity="center_horizontal|center"
 android:lines="1"
 android:textColor="#fff"
 android:focusable="true"
 android:marqueeRepeatLimit="marquee_forever"
 android:layout_centerInParent="true"
 android:focusableInTouchMode="true"
 android:scrollHorizontally="true"
 android:textSize="18sp" />
 <ImageView
 android:contentDescription="@string/cancel"
 android:id="@+id/action_bar_back_iv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerVertical="true"
 android:padding="10dp"
 android:textSize="15sp"
 android:text
 android:textColor="#fff"
 android:src="@drawable/arrow_left" />
 <TextView
 android:layout_toEndOf="@id/action_bar_back_iv"
 android:text="@string/action_bar_close"
 android:id="@+id/action_bar_close_tv"
 android:textColor="#fff"
 android:visibility="invisible"
 android:textSize="15sp"
 android:text
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:padding="10dp"/>
 <TextView
 android:visibility="invisible"
 android:padding="10dp"
 android:layout_alignParentEnd="true"
 android:text="@string/action_bar_commint"
 android:id="@+id/action_bar_comint_tv"
 android:textSize="15sp"
 android:textColor="#fff"
 android:text
 android:layout_marginEnd="3dp"
 android:layout_width="wrap_content"
 android:layout_height="match_parent" />
 </RelativeLayout> 

最後是一個簡單的應用範例:

 public class DemoActivity extends MyBaseActivity {
 @Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setTitle("APPBaseActivity");//設置標題
 showCloseBT();//顯示關閉按鈕,默認時隱藏的
 }
 //如果返回按鈕有其他操作的話可以重寫
 @Override protected void onLeftBackward() {
 super.onLeftBackward();
 //裡面寫事件就可以
 }
 } 

透過這樣的封裝,標題欄的樣式與互動邏輯被集中管理,各頁面只需專注於自己的內容,開發效率與一致性都大幅提升。希望這份分享對大家有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved