Android中BaseActivity自定義標(biāo)題欄

再做一個(gè)項(xiàng)目的時(shí)候,要求標(biāo)題欄的標(biāo)題再中間,樣式,字體大小都要自定義。左邊一個(gè)返回按鈕,一個(gè)關(guān)閉按鈕,右邊定義一個(gè)提交按鈕,有時(shí)候顯示有時(shí)候隱藏。因?yàn)樵膖itle標(biāo)題是再左邊的,然后去給Titlebar設(shè)置自定義View的時(shí)候,也會不盡人意,標(biāo)題不是再正中間的,標(biāo)題欄太高等問題。

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比新華網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式新華網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋新華地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。

我們要求的是這樣的,右邊的按鈕可以顯示或者隱藏。

Android中BaseActivity自定義標(biāo)題欄 

于是就決定自己寫一個(gè)BaseActivity,所有的都去繼承這個(gè)基類,然后自己去定義標(biāo)題欄的樣式就可以就可以了。
下面來講一下這個(gè)界面是怎么實(shí)現(xiàn)的:

首先定義一個(gè)類BaseActivity:

public class BaseActivity extends AppCompatActivity implements View.OnClickListener{

  private TextView mTitleTextView;//標(biāo)題
  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()){
      /**
       * 點(diǎn)擊空白位置 隱藏軟鍵盤
       */
      InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
      return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
    }
    return super .onTouchEvent(event);
  }

  /**
   * 顯示關(guān)閉按鈕
   */
  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);
    }
  }


  /**
   * 返回按鈕點(diǎn)擊后觸發(fā)
   */
  protected void onLeftBackward() {
    onBackPressed();
  }

  /**
   * 右邊提交按鈕點(diǎn)擊后觸發(fā)
   */
  protected void onRightForward() {

  }
  /**
   * 提交關(guān)閉按鈕點(diǎn)擊后觸發(fā)
   */
  protected void onLeftCloseword(){
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("tabpos", 2);
    startActivity(intent);
    finish();
  }
  //設(shè)置標(biāo)題內(nèi)容
  @Override
  public void setTitle(int titleId) {
    mTitleTextView.setText(titleId);
  }

  //設(shè)置標(biāo)題內(nèi)容
  @Override
  public void setTitle(CharSequence title) {
    mTitleTextView.setText(title);
  }

//點(diǎn)擊標(biāo)題時(shí)出發(fā)的事件操作
  public void onTitle() {

  }

  //取出FrameLayout并調(diào)用父類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的時(shí)候,只需要去設(shè)置是否顯示某個(gè)按鈕即可,標(biāo)題欄各個(gè)按鈕的點(diǎn)擊事件不需要去設(shè)置,直接重寫
onLeftBackward();onRightForward();onRightForward();onTitle();
然后對應(yīng)各自的方法就可以了。

下面給出布局文件activity_abstract_title.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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="http://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:textStyle="bold"
    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:textStyle="bold"
    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:textStyle="bold"
    android:layout_marginEnd="3dp"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />
</RelativeLayout>

下面是一個(gè)簡單的應(yīng)用:

public class DemoActivity extends MyBaseActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("APPBaseActivity");//設(shè)置標(biāo)題
    showCloseBT();//顯示關(guān)閉按鈕,默認(rèn)時(shí)隱藏的

  }


    //如果返回按鈕有其他操作的話可以重寫
  @Override
  protected void onLeftBackward() {
    super.onLeftBackward();
    //里面寫事件就可以
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站標(biāo)題:Android中BaseActivity自定義標(biāo)題欄
轉(zhuǎn)載注明:http://www.muchs.cn/article32/pdhpsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、網(wǎng)站營銷移動(dòng)網(wǎng)站建設(shè)標(biāo)簽優(yōu)化、網(wǎng)站收錄電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)