編輯:關於 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();
//裡面寫事件就可以
}
}
透過這樣的封裝,標題欄的樣式與互動邏輯被集中管理,各頁面只需專注於自己的內容,開發效率與一致性都大幅提升。希望這份分享對大家有所幫助。
Android 分享微信好友與閃退問題的解決方案
一、申請微信 APPID 要讓 App 具備分享到微信的能力,第一步就是到微信開放平台申請一個 APPID。申請時需要填寫應用簽名與應用包名,這裡有兩個關鍵細節:包名必須
Android 自定義 View 實戰:打造水波紋進度球
在日常 Android 開發中,自定義控件的出鏡率其實相當高。設計與產品為了讓畫面更精緻、互動更有意思,往往會畫出各種炫目的效果圖,而最終把它落地成可運行代碼的,永遠是我
Android 權限機制解析:如何宣告與使用自訂權限
Android 權限機制用於保護敏感資源與功能。若應用要撥打電話、讀取聯絡人、存取相機或網路,通常需要在 AndroidManifest 中宣告對應權限。 系統權限 例
Android 網路編程:UDP 通信模型實作範例
什麼是 Android UDP? UDP 是 User Datagram Protocol 的簡稱,中文名為使用者資料包協定,是 OSI 參考模型中一種無連線的傳輸層協定