app的應用名被入口activity的標題名字覆蓋(ActionBar改為ToolBar)


今天遇到一個問題,百度“怎么讓app的應用名和入口activity的標題名字不一樣呢”
只找到一個方法,還沒用。。問題如下

 

 

我按這個老哥的方法試了沒用,后來請教了一位熱心的大佬。解決啦。

1.Androidmanifest.xml

 

 

 2.進入style.xml,把默認的DarkActionBar改為NoActionBar

 

3.把activity的label去掉,即在Manifest.xml中設置的頁面導航欄名稱去掉

4.在該頁面的activity.xml布局文件中,在根布局下面添加如下代碼:

方法一:直接用title

       缺點:(1)只能更改字體顏色,不能更改字體大小

                 (2)只能靠左,不能居中

   <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:title="小車登錄界面"
        android:background="@color/colorPrimary"
        app:titleTextColor="@android:color/white"></androidx.appcompat.widget.Toolbar>

或者

方法二:添加子類

        優點:子類也可以居中,一般做標題欄用

<androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="小車登錄界面"
            android:textSize="20dp"
            ></TextView>      
    </androidx.appcompat.widget.Toolbar>
 

ActionBar和Toolbar

詳見   https://blog.csdn.net/xingzhong128/article/details/79696467

ActionBar是Activity開發的標配,但是從5.0開始逐漸由Toolbar取代,主要因為Toolbar使用方式更加簡單也更容易定制。

ActionBar的內容組成如上圖所示,圖中的每個部分都有對應的設置方法。

 

 Android 5.0之后不再推薦使用ActionBar和它的導航功能,直接使用Toolbar來替代ActionBar是一種更好的開發方式。替換是需要在Activity的布局文件中添加Toolbar對象並把它的引用設置為ActionBar對象。

Toolbar基本使用

(1)簡介

Toolbar 是 Android 5.0 推出的一個 Material Design 風格的導航控件 ,用來取代之前的 Actionbar 。與 Actionbar 相比,Toolbar 明顯要靈活的多。它不像 Actionbar 一樣,一定要固定在Activity的頂部,而是可以放到界面的任意位置。

(2)修改主題

當我們新建一個工程時,我們發現默認是有導航欄的, 如圖:

圖片.png

那是因為默認app主題自帶了導航欄

圖片.png

圖片.png

我們發現,系統自帶的是一個actionBar,那么我們就換一個主題

圖片.png

三種主題任意選擇一個即可。

現在開始添加一個ToolBar。

(3)Toolbar導包選擇
圖片.png

如圖所示,Toolbar有兩種導包,建議使用v7的那個,因為第二種只支持Android5.0以上的手機。

(4)Toolbar基本代碼
<android.support.v7.widget.Toolbar android:id="@+id/toolbar_normal" android:layout_width="match_parent" android:layout_height="wrap_content" app:collapseIcon="@mipmap/ic_launcher" app:buttonGravity="bottom" app:navigationIcon="@mipmap/ic_launcher" app:popupTheme="@style/Animation.AppCompat.Dialog" app:logoDescription="asdasdasd" app:maxButtonHeight="50dp" app:navigationContentDescription="tttttt" app:title="Title" app:titleTextColor="#ffffff" android:background="@color/colorPrimaryDark" app:subtitle="SubTitle" app:subtitleTextColor="#ffffff" app:logo="@mipmap/ic_launcher">

</android.support.v7.widget.Toolbar>
(5)基本屬性
  • background 設置背景色
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_normal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark">
    
    </android.support.v7.widget.Toolbar>
    圖片.png

    目前發現,設置toolbar背景色之后,狀態欄也會隨之變化。

    • navigationIcon 添加返回按鈕
      <android.support.v7.widget.Toolbar
          android:id="@+id/toolbar_normal"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/colorPrimaryDark"
          app:navigationIcon="@mipmap/back">
      
      </android.support.v7.widget.Toolbar>

 
圖片.png
  • logo 添加logo
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_normal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    app:navigationIcon="@mipmap/back"
    app:logo="@mipmap/ic_launcher">

</android.support.v7.widget.Toolbar>

 

圖片.png
 
  • logo 添加logo
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_normal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    app:navigationIcon="@mipmap/back"
    app:logo="@mipmap/ic_launcher">

</android.support.v7.widget.Toolbar>
圖片.png
  • title和subtitle 添加標題和子標題

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_normal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    app:navigationIcon="@mipmap/back"
    app:logo="@mipmap/ic_launcher"
    app:title="ToolBar標題"
    app:subtitle="ToolBar子標題">

</android.support.v7.widget.Toolbar>
圖片.png
  • titleTextColor和subtitleTextColor 修改標題和子標題的顏色
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_normal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:navigationIcon="@mipmap/back"
        app:logo="@mipmap/ic_launcher"
        app:title="ToolBar標題"
        app:subtitle="ToolBar子標題"
        app:titleTextColor="#ffffff"
        app:subtitleTextColor="#ffffff">
    
    </android.support.v7.widget.Toolbar>
    圖片.png
    • collapseIcon 設置折疊視圖的圖標
      <android.support.v7.widget.Toolbar
          android:id="@+id/toolbar_normal"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/colorPrimaryDark"
          app:navigationIcon="@mipmap/back"
          app:logo="@mipmap/ic_launcher"
          app:title="ToolBar標題"
          app:collapseIcon="@mipmap/back"
          app:subtitle="ToolBar子標題"
          app:titleTextColor="#ffffff"
          app:subtitleTextColor="#ffffff">
      圖片.png

      這個后面有詳細介紹。

      (6)添加菜單

      通過inflateMenu可以添加toolbar的菜單

          toolbar_normal.inflateMenu(R.menu.toolbar_menu);

      菜單布局是

      <menu
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">
      
          <item
              android:id="@+id/action_search"
              android:icon="@mipmap/back"
              android:title="按鈕1"
              app:showAsAction="ifRoom" />
      
          <item
              android:id="@+id/action_notification"
              android:icon="@mipmap/back"
              android:title="按鈕2"
              app:showAsAction="ifRoom" />
      
          <item
              android:id="@+id/action_item_one"
              android:title="按鈕3"
              app:showAsAction="never" />
      
          <item
              android:id="@+id/action_item_two"
              android:title="按鈕4"
              app:showAsAction="never" />
      </menu>

      效果如下:

      圖片.png
      圖片.png

      每個菜單都可以設置特有的屬性

      • orderInCategory
        設置菜單項的排列順序,必須設置大於等於0的整數值。數值小的排列在前,如果值相等,則按照xml中的順序展現。
      • title
        菜單項的標題。
      • icon
        菜單項的圖標。
      • showAsAction
        該屬性有五個值,可以混合使用。

      always:總是顯示在Toolbar上。
      ifRoom:如果Toolbar上還有空間,則顯示,否則會隱藏在溢出列表中。
      never:永遠不會顯示在Toolbar上,只會在溢出列表中出現。
      withText:文字和圖標一起顯示。
      collapseActionView:聲明了這個操作視窗應該被折疊到一個按鈕中,當用戶選擇這個按鈕時,這個操作視窗展開。一般要配合ifRoom一起使用才會有效。

      這里重點介紹一下collapseActionView屬性,利用collapseActionView屬性實現“點擊按鈕展開成搜索框”

      第一步:設置折疊視圖圖標


      圖片.png

      第二步:修改菜單屬性

      圖片.png

      這樣的話就可以實現下圖的效果

(7)setOnMenuItemClickListener

設置Item的點擊事件

    toolbar_normal.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()){
                case R.id.action_item_one:
                    break;
                case R.id.action_item_two:
                    break;
                case R.id.action_notification:
                    break;
                case R.id.action_search:
                    break;
            }
            return true;
        }
    });
(8)setNavigationOnClickListener

設置返回鍵點擊事件

    toolbar_normal.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
(9)SearchView

有關SearchView,前面已經介紹過怎么展示了,下面介紹怎么去設置監聽

    SearchView searchView = (SearchView) menuItem.getActionView();
    searchView.setQueryHint("搜索");
    
    //((ImageView)searchView.findViewById(R.id.search_button)).setImageResource(R.mipmap.shennvguo2);

    searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //當點擊搜索編輯框的時候執行,剛進入時默認點擊搜索編輯框
        }
    });

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            //點擊軟鍵盤搜索的時候執行
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            //搜索框文本發生改變的時候執行
            return false;
        }
    });
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            //當得到焦點和失去焦點的時候執行
        }
    });

    menuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            //展開時回調
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            //收起來時回調
            return true;
        }
    });
(10)Overflow

修改overflow的圖標

    //設置overflow圖標
   toolbar_normal.setOverflowIcon(ContextCompat.getDrawable(this,R.mipmap.overflow));

 

圖片.png
(11)overflow popup主題
圖片.png

溢出菜單就不介紹了,目前沒有什么好辦法可以自定義布局。

圖片.png
(12)添加子布局
圖片.png

Toolbar的父類是ViewGroup,我們常用的線性布局和相對布局的父類也是ViewGroup,所以Toolbar也同樣可以添加子布局,看以下代碼


<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_normal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    app:navigationIcon="@mipmap/back"
    app:popupTheme="@style/OverflowMenuStyle"
    app:collapseIcon="@mipmap/back">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorAccent"
        android:text="標題"
        android:layout_gravity="center"
        android:textSize="20sp"/>


</android.support.v7.widget.Toolbar>

我們添加一個標題,使它居中

 


ToolBar的使用來自一下,我覺的總結的太好了!!!
作者:NoBugException
鏈接:https://www.jianshu.com/p/7dd57a7c66f1
來源:簡書

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM