最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年01月22日 14:20:04   作者:lixuce1234  
這篇文章主要介紹了Android中BaseActivity自定義標(biāo)題欄,非常實(shí)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

 

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

首先定義一個(gè)類(lèi)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)擊空白位置 隱藏軟鍵盤(pá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)用父類(lèi)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è)置,直接重寫(xiě)
onLeftBackward();onRightForward();onRightForward();onTitle();
然后對(duì)應(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è)簡(jiǎn)單的應(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í)隱藏的

  }


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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Flutter?Getx中的put和lazyPut函數(shù)使用案例解析

    Flutter?Getx中的put和lazyPut函數(shù)使用案例解析

    這篇文章主要為大家介紹了Flutter?Getx中的put和lazyPut函數(shù)使用案例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Android顯式Intent與隱式Intent的使用詳解

    Android顯式Intent與隱式Intent的使用詳解

    Intent的中文意思是“意圖,意向”, Intent對(duì)Android的核心和靈魂,是各組件之間的橋梁。四大組件分別為Activity 、Service、BroadcastReceiver、ContentProvider。而這四種組件是獨(dú)立的,它們之間可以互相調(diào)用,協(xié)調(diào)工作,最終組成一個(gè)真正的Android應(yīng)用
    2022-09-09
  • Android實(shí)現(xiàn)秒表功能

    Android實(shí)現(xiàn)秒表功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易秒表功能,具備啟停功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法

    Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法,涉及Android針對(duì)按鈕元素屬性的相關(guān)操作技巧,需要的朋友可以參考下
    2015-11-11
  • 詳解Android中Notification的使用方法

    詳解Android中Notification的使用方法

    這篇文章主要介紹了Android中Notification的使用方法,最典型的應(yīng)用就是未看短信和未接來(lái)電的顯示,還有QQ微信,想要深入了解Notification的朋友可以參考本文
    2015-12-12
  • Android Studio中快捷鍵實(shí)現(xiàn)try catch等功能包含代碼塊的實(shí)現(xiàn)方法

    Android Studio中快捷鍵實(shí)現(xiàn)try catch等功能包含代碼塊的實(shí)現(xiàn)方法

    這篇文章主要介紹了 Android Studio中快捷鍵實(shí)現(xiàn)try catch等功能包含代碼塊的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • Android SlidingDrawer 抽屜效果的實(shí)現(xiàn)

    Android SlidingDrawer 抽屜效果的實(shí)現(xiàn)

    本篇文章小編為大家介紹,Android SlidingDrawer 抽屜效果的實(shí)現(xiàn)。需要的朋友參考下
    2013-04-04
  • Android 隱藏及切換顯示鍵盤(pán)

    Android 隱藏及切換顯示鍵盤(pán)

    這篇文章主要介紹了Android 隱藏及切換顯示鍵盤(pán)的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Jetpack Compose按鈕組件使用實(shí)例詳細(xì)講解

    Jetpack Compose按鈕組件使用實(shí)例詳細(xì)講解

    這篇文章主要介紹了Jetpack Compose按鈕組件使用實(shí)例,按鈕組件Button是用戶和系統(tǒng)交互的重要組件之一,它按照Material Design風(fēng)格實(shí)現(xiàn),我們先看下Button的參數(shù)列表,通過(guò)參數(shù)列表了解下Button的整體功能
    2023-04-04
  • Android實(shí)現(xiàn)讀取掃碼槍內(nèi)容(條形碼)

    Android實(shí)現(xiàn)讀取掃碼槍內(nèi)容(條形碼)

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)讀取掃碼槍內(nèi)容、條形碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評(píng)論

石渠县| 民丰县| 图片| 湘西| 炉霍县| 商城县| 凭祥市| 高雄市| 讷河市| 镇江市| 巴里| 中江县| 巴林右旗| 岢岚县| 汉川市| 屏东市| 克什克腾旗| 临桂县| 合阳县| 河津市| 黑山县| 赣榆县| 建宁县| 长泰县| 商河县| 焦作市| 仙游县| 嵊州市| 赣榆县| 乌恰县| 甘孜县| 赤峰市| 新源县| 当涂县| 泰兴市| 左贡县| 隆安县| 格尔木市| 墨竹工卡县| 满城县| 普陀区|