【威哥說】若是任何一款產品的推廣,不僅是運營的主要工做,而是更加註重用戶體驗,及時網站內容不豐富,只要有一個功能顯得小有逼格,就會給人徹底不同的感受。下面你們就一塊兒看看,若是作一個有逼格的標題欄。 【正文】Toolbar這個控件是在 Android 5.0 推出的一個 Material Design 風格的導航控件 ,Google 推薦你們使用 Toolbar 來做爲Android客戶端的導航欄,以此來取代以前的 Actionbar 。與 Actionbar 相比,Toolbar 明顯要靈活的多。它不像 Actionbar 同樣,必定要固定在Activity的頂部,而是能夠放到界面的任意位置。除此以外,在設計 Toolbar 的時候,Google也留給了開發者不少可定製修改的餘地,這些可定製修改的屬性在API文檔中都有詳細介紹,如: 設置導航欄圖標; 設置App的logo; 支持設置標題和子標題; 支持添加一個或多個的自定義控件; 支持Action Menu; 下面咱們就開始使用ToolBar這個控件和以前的Fragment同樣,因爲是後期推出的,爲了兼容性(5.0以前),使用 android.support.v7.widget.Toolbar 進行開發。 首先是佈局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"java
xmlns:toolbar="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#0176da" toolbar:popupTheme="@style/Theme.ToolBar.Base"> <!--自定義控件--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="磨礪營教育" /> </android.support.v7.widget.Toolbar>
</LinearLayout>android
在toolsbar裏面放置任意佈局,顯示位置參照圖片 另外這裏要注意,若是要用到toolbar的屬性,必須導入xmlns:toolbar="http://schemas.android.com/apk/res-auto"命名,不能是android開頭,不然無效。
而後是代碼的實現:微信
public class ToolBarActivity extends AppCompatActivity {app
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
這裏設置notitle主題ide
supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_tool_bar);
這裏獲取toolbar後,對其進行一系列的設置,顏色,字體等等。佈局
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setNavigationIcon(R.mipmap.ic_home);//設置導航欄圖標 toolbar.setLogo(R.mipmap.ic_launcher);//設置app logo toolbar.setTitle("新浪");//設置主標題 toolbar.setTitleTextColor(Color.GREEN);//設置主標題顏色 toolbar.setTitleTextAppearance(this, R.style.Theme_ToolBar_Base_Title);//修改主標題的外觀,包括文字顏色,文字大小等 toolbar.setSubtitle("時事");//設置子標題 toolbar.setSubtitleTextColor(getResources().getColor(android.R.color.darker_gray));//設置子標題顏色 toolbar.setSubtitleTextAppearance(this, R.style.Theme_ToolBar_Base_Subtitle);//設置子標題的外觀,包括文字顏色,文字大小等
這裏是設置toolbar右側的菜單按鈕學習
toolbar.inflateMenu(R.menu.base_toolbar_menu);//設置右上角的填充菜單 toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { int menuItemId = item.getItemId(); if (menuItemId == R.id.action_search) { Toast.makeText(ToolBarActivity.this, 「搜索」, Toast.LENGTH_SHORT).show(); } else if (menuItemId == R.id.action_notification) { Toast.makeText(ToolBarActivity.this, 「通知」, Toast.LENGTH_SHORT).show(); } else if (menuItemId == R.id.action_item1) { Toast.makeText(ToolBarActivity.this, 「選項1」, Toast.LENGTH_SHORT).show(); } else if (menuItemId == R.id.action_item2) { Toast.makeText(ToolBarActivity.this, 「選項2」, Toast.LENGTH_SHORT).show(); }
注意這裏要return true字體
return true; } }); }
}網站
好,下面就能夠運行看看效果了,以下:
【圖一】 【圖二】
以上僅僅是toolbar簡單的使用,toolbar還有不少地方能夠學習,更多資料請參考Google文檔和API,有興趣的同窗研究下,製做出更好的md效果,也歡迎你們踊躍投稿,感謝你們關注。this
更多內容關注微信公衆號mjw-java或訪問www.moliying.com