編輯:關於 Android 程式設計
今天我們一起來深入解析 React Native 中的工具欄控件 ToolbarAndroid,並結合最佳實踐進行講解。
該 ToolbarAndroid 組件封裝了 Android 平台中的 Toolbar 組件(僅適用於 Android 平台)。一個 Toolbar 可以顯示 Logo 圖標、若干導航圖標(例如菜單按鈕)、一個標題與副標題,以及一系列功能列表。標題與副標題上下排列,Logo 與導航圖標顯示在左側,標題與副標題居中,功能列表則顯示在右側。
【注意】如果 Toolbar 只有一個子節點,該子節點會顯示在標題與功能列表中間。
特別聲明:儘管 Toolbar 的 Logo 圖標、導航圖標與功能列表圖標都支援加載遠程圖片(網絡圖片),但這種加載僅在開發(Dev)模式下支援。在發布(Release)模式下,你應該只使用應用內部的資源進行渲染,例如使用 require('./some_icon.png') 會自動幫你加載本地資源。因此,只要不直接使用 {uri:'https://...'} 的形式,一般就不會出問題。
先來看看官方提供的一個簡單示例:
render: function() {
return (
);
},
onActionSelected: function(position) {
if (position === 0) {
showSettings();
}
}
這段代碼添加了一個 ToolbarAndroid 組件,加入了 Logo 圖標、標題信息與功能列表,並在功能被點擊時響應相應方法。具體使用會在後文詳細講解。
3.1 繼承自 View 的相關屬性與樣式(如寬高、背景顏色、邊距等)。
3.2 actions:設置功能列表信息,傳入參數格式為 [{title: string, icon: optionalImageSource, show: enum('always','ifRoom','never'), showWithText: bool}],用於配置功能菜單中可用的各項功能,顯示在組件右側(以圖標或文字形式)。若界面上區域不足,則會加入隱藏菜單中彈出顯示。該屬性是一組對象數組,每個對象包含:
3.3 contentInsetEnd:設置 Toolbar 右側與屏幕右邊緣的間距。
3.4 contentInsetStart:設置 Toolbar 左側與屏幕左邊緣的間距。
3.5 logo:可選圖片資源,設置 Toolbar 的 Logo 圖標。
3.6 navIcon:可選圖片資源,設置導航圖標。
3.7 onActionSelected:功能被選中時的回調方法,僅傳入一個參數,即點擊功能在功能列表中的索引。
3.8 onIconClicked:圖標被選中時的回調方法。
3.9 overflowIcon:可選圖片資源,設置功能列表彈出菜單中的圖標。
3.10 rtl:設置 Toolbar 功能順序為從右到左(RTL)。為使其生效,需在 AndroidManifest.xml 的 application 節點中添加 android:supportsRtl="true",並在主 Activity 的 onCreate 方法中調用 setLayoutDirection(LayoutDirection.RTL)。
3.11 subtitle:設置 Toolbar 的副標題。
3.12 subtitleColor:設置副標題顏色。
3.13 title:設置 Toolbar 的標題。
3.14 titleColor:設置標題顏色。
4.1 簡單顯示 Toolbar 的標題、副標題、功能列表與導航圖標,代碼如下:
'use strict';
import React, { AppRegistry, Component, StyleSheet, View } from 'react-native';
var ToolbarAndroid = require('ToolbarAndroid');
class ToolBarAndroidDemo extends Component {
render() {
return (
);
}
}
var toolbarActions = [
{title: 'Create', icon: require('./ic_create_black_48dp.png'), show: 'always'},
{title: 'Filter'},
{title: 'Settings', icon: require('./ic_settings_black_48dp.png'), show: 'always'},
];
const styles = StyleSheet.create({
toolbar: { backgroundColor: '#e9eaed', height: 56 },
});
AppRegistry.registerComponent('ToolBarAndroidDemo', () => ToolBarAndroidDemo);
運行效果如下:

4.2 只設置標題與功能列表、無導航圖標的效果:
'use strict';
import React, { AppRegistry, Component, StyleSheet, View } from 'react-native';
var ToolbarAndroid = require('ToolbarAndroid');
class ToolBarAndroidDemo extends Component {
render() {
return (
);
}
}
var toolbarActions = [
{title: 'Create', icon: require('./ic_create_black_48dp.png'), show: 'always'},
{title: 'Filter'},
{title: 'Settings', icon: require('./ic_settings_black_48dp.png'), show: 'always'},
];
const styles = StyleSheet.create({
toolbar: { backgroundColor: '#e9eaed', height: 56 },
});
AppRegistry.registerComponent('ToolBarAndroidDemo', () => ToolBarAndroidDemo);
運行效果如下:

4.3 只存在導航圖標、Logo 圖標與功能列表的效果:
'use strict';
import React, { AppRegistry, Component, StyleSheet, View } from 'react-native';
var ToolbarAndroid = require('ToolbarAndroid');
class ToolBarAndroidDemo extends Component {
render() {
return (
);
}
}
var toolbarActions = [
{title: 'Create', icon: require('./ic_create_black_48dp.png'), show: 'always'},
{title: 'Filter'},
{title: 'Settings', icon: require('./ic_settings_black_48dp.png'), show: 'always'},
];
const styles = StyleSheet.create({
toolbar: { backgroundColor: '#e9eaed', height: 56 },
});
AppRegistry.registerComponent('ToolBarAndroidDemo', () => ToolBarAndroidDemo);
運行效果如下:

4.4 最後介紹一個知識點:ToolbarAndroid 組件還支援組件的嵌套。下面看一個 ToolbarAndroid 嵌套 SwitchAndroid 組件的例子:
'use strict';
import React, { AppRegistry, Component, StyleSheet, View } from 'react-native';
var ToolbarAndroid = require('ToolbarAndroid');
var SwitchAndroid = require('SwitchAndroid');
class ToolBarAndroidDemo extends Component {
render() {
return (
);
}
}
var toolbarActions = [
{title: 'Create', icon: require('./ic_create_black_48dp.png'), show: 'always'},
{title: 'Filter'},
{title: 'Settings', icon: require('./ic_settings_black_48dp.png'), show: 'always'},
];
const styles = StyleSheet.create({
toolbar: { backgroundColor: '#e9eaed', height: 56 },
});
AppRegistry.registerComponent('ToolBarAndroidDemo', () => ToolBarAndroidDemo);
運行效果如下:

Android 水波紋效果的實作重點
效果概述水波紋效果常用於互動回饋、狀態提示或視覺裝飾。透過自定義 View,可以讓圓形區域持續向外擴散,形成類似漣漪的動畫。搭配開始與停止按鈕,還能呈現不同狀態下的視覺變
Android 控制項封裝:打造可複用的自定義 Dialog 控制項
自定義 Dialog 幾乎每個專案都會用到,但每次重寫總讓人覺得有點繁瑣。與其反覆搬運,不如一次封裝好,以後直接複用。本文分享一個把 Dialog 封裝成獨立控制項的做法
Android Spinner 下拉選單控件的完整用法
一、效果預覽 二、建立版面檔(main.xml) 三、準備下拉選單的資料來源 List list = new ArrayList(); list.add(蘋果);
Android 仿淘寶商品詳情:拖動切換與標題欄漸變
效果目標 淘寶商品詳情頁有幾個很典型的互動:標題欄會隨滾動漸變,通知欄可透明處理,內容區可以繼續往下拖動並切換到詳情區。這種效果能讓商品資訊與詳細描述之間形成自然銜接。