編輯:關於 Android 程式設計
Android 5.0 之后出现了 Toolbar,今天折腾了一下,做个记录方便以后查看,也给有需要的朋友做个参考。
下面将完成两方面的工作:
一、Toolbar 的基本使用,效果图如下:

二、自定义 Toolbar,效果图如下:

一、Toolbar 的基本使用
1、新建工程后,在 activity_main.xml 布局中添加如下代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" tools:context="com.gta.yanwen.hellotoolbar.MainActivity" xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools"> </LinearLayout>
这是主布局,根布局先不管,也可用 RelativeLayout,主要看 AppBarLayout 和 Toolbar 这两个 View。AppBarLayout 定义 Toolbar 所在的布局,设置了 android:theme 属性,这个属性用来设置 Toolbar 的主题,主要是文字和背景等一些属性的集合;若不设置 theme,也可以通过 ?attr/color 单独设置某个部位的属性。
AppBarLayout 布局里包含了 Toolbar 这个布局,来逐个分析它的属性:
android:background:设置 toolbar 的背景颜色
android:navigationIcon:设置 toolbar 最左边的按钮图标
app:logo:设置 Logo 图标
app:title:设置 Title 标题
app:subtitle:设置副标题 SubTitle
当然还有标题字体颜色、副标题字体颜色,读者可以自行尝试。注意:?attr/colorAccent 属性值在 style 文件中有定义。
2、主布局设置完后,再编写 MainActivity.java 文件,代码如下,慢慢分析:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.inflateMenu(R.menu.menu_main); toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { Toast.makeText(MainActivity.this, "action_menu", Toast.LENGTH_SHORT).show(); } return true; } }); } }
代码部分也很简单:获取到 toolbar 的 id,设置了 menu 选项菜单,并为菜单每一项设置点击监听事件。到此编译、运行即可出现图一效果。
二、自定义 Toolbar
思路:编写 CurToolbar 类继承 Toolbar,左边箭头和上面用法一样,右边部分加一个 TextView 和 ImageButton。
1、在 layout 下新建 tool_bar_layout.xml 布局文件,如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="https://schemas.android.com/apk/res/android"> <TextView android:background="@drawable/bg_toolbar_search_view" android:clickable="true" android:drawableLeft="@mipmap/search_icon" android:duplicateParentState="true" android:gravity="center_vertical" android:hint="输入需要搜索的商品名" android:id="@+id/toolbar_search_view" android:layout_centerVertical="true" android:layout_height="wrap_content" android:layout_marginRight="50dp" android:layout_width="match_parent" android:padding="5dp" android:singleLine="true"> </TextView> <ImageButton android:background="@android:color/transparent" android:id="@+id/toolbar_imgbtn" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_width="wrap_content"> </ImageButton></RelativeLayout>
在 RelativeLayout 中放置了两个控件,分别是 TextView 和 ImageButton。TextView 是点击搜索控件,其中加载了 bg_toolbar_search_view 样式(后面会贴出);布局最右边放置一个带加号图标的 ImageButton,这个布局就是自定义 Toolbar 的主要部分。
2、接着新建 CurToolbar 继承 Toolbar,代码如下:
public class CurToolBar extends Toolbar { private ImageView mImageView; private TextView mSearchEdit; private View view; public CurToolBar(Context context) { this(context, null); } public CurToolBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CurToolBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); view = LayoutInflater.from(getContext()).inflate(R.layout.tool_bar_layout, null); mImageView = (ImageView) view.findViewById(R.id.toolbar_imgbtn); mSearchEdit = (TextView) view.findViewById(R.id.toolbar_search_view); if (mImageView != null) { final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs, R.styleable.CurToolBar, defStyleAttr, 0); mImageView.setImageDrawable(a.getDrawable(R.styleable.CurToolBar_rightButtonIcon)); a.recycle(); } if (mSearchEdit != null) { final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs, R.styleable.CurToolBar, defStyleAttr, 0); boolean isVisible = a.getBoolean(R.styleable.CurToolBar_isShowSearchView, false); mSearchEdit.setVisibility(isVisible ? View.VISIBLE : View.GONE); a.recycle(); } LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER_HORIZONTAL); addView(view, lp); } }
通过 LayoutInflater 获取 tool_bar_layout 布局,随后获取里面的 TextView 和 ImageButton,调用 TintTypedArray.obtainStyledAttributes() 获取自定义的 CurToolbar 属性集合,再从中取出 rightButtonIcon 和 isShowSearchView 这两个自定义属性值,最后调用 addView() 把 tool_bar_layout 布局添加到 Toolbar 中。
3、在 values 下新建 attrs 文件,编写如下属性:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CurToolBar"> <attr name="rightButtonIcon" format="reference" /> <attr name="isShowSearchView" format="boolean" /> </declare-styleable> </resources>
rightButtonIcon 属性,值类型是 reference;isShowSearchView 属性,值类型是 boolean。
4、再编写 activity_main.xml 主布局,如下:
<com.gta.yanwen.hellotoolbar.curtoolbar android:background="?attr/colorAccent" android:id="@+id/toolbar" android:layout_height="wrap_content" android:layout_width="match_parent" android:minHeight="?attr/actionBarSize" app:isShowSearchView="true" app:navigationIcon="@mipmap/left_arrow_icon" app:rightButtonIcon="@mipmap/plus_icon"> </com.gta.yanwen.hellotoolbar.curtoolbar>
在 AppBarLayout 中加入 CurToolBar 自定义的布局,仔细看会发现加入了刚才自定义的 rightButtonIcon 和 isShowSearchIcon 属性,程序运行时 CurToolBar 类中会读到这两个属性的值并进行设置。
5、最后编写 MainActivity.java,如下:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.findViewById(R.id.toolbar_imgbtn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "ToolBar ImageButton", Toast.LENGTH_SHORT).show(); } }); toolbar.findViewById(R.id.toolbar_search_view).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, SearchCommodityActivity.class)); } }); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "onNavigationOnClick", Toast.LENGTH_SHORT).show(); } }); } }
这里主要是设置 CurToolbar 中控件的点击监听事件,在点击 SearchView 这个 TextView 时启动了 SearchCommodityActivity 搜索界面,贴出如下:
SearchCommodityActivity.class
public class SearchCommodityActivity extends Activity { private ImageButton back_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search_commondity_layout); back_btn = (ImageButton) findViewById(R.id.back_btn); back_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SearchCommodityActivity.this.finish(); } }); } }
search_commodity_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:background="#EDECEC" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" xmlns:android="https://schemas.android.com/apk/res/android"> <ImageButton android:background="@mipmap/left_arrow_icon" android:id="@+id/back_btn" android:layout_height="wrap_content" android:layout_width="wrap_content"> </ImageButton> <EditText android:background="@mipmap/bg_edit_ware" android:focusable="true" android:focusableInTouchMode="true" android:gravity="center_vertical" android:layout_height="match_parent" android:layout_marginRight="20dp" android:layout_width="match_parent" android:paddingLeft="15dp"> </EditText></LinearLayout>
6、编译、运行程序即可出现图二所示效果。
Android 高手進階教程(二十二):圖像特效處理大集合
大家好,這一節要和大家分享幾個 Android 圖像特效處理的小技巧,包括圓角、倒影、圖片縮放,以及 Drawable 與 Bitmap 之間的相互轉換等實用手法。 廢話
Android 入門:Toast 提示訊息的完整用法
Toast 幾乎是每個 Android 新手會遇到的第一個元件,也是最容易被濫用的元件。本文從最簡單的呼叫開始,把常見用法整理成一份可以隨時回頭查的筆記。 一、先看最直觀
Android 4.4 系統瀏覽器:Chromium 的頁面加載模塊與流程剖析
Android 4.4 起,WebView 的實作核心換成了 Chromium。理解它的加載流程,對排查 WebView 相關問題、評估效能瓶頸都有幫助。本文聚焦於 HT
深入解析 Android AIDL:輕鬆實現跨進程通信(IPC)
前言 在 Android 系統裡,每一個應用都運行在各自獨立的進程之中,擁有彼此隔離的內存空間。然而,現實開發中我們常常會遇到這樣的需求:讓自己的應用與其他應用交換數據、