編輯:中階開發
今天一起來看 Android Action Bar 的示例程式碼,我們透過活動欄做一個簡單的選項菜單。下面這個例子會演示 ActionBar.NAVIGATION_MODE_STANDARD、ActionBar.NAVIGATION_MODE_TABS 等多種模式的效果。最後 Android123 仍要提醒:Action Bar 是 Android 3.0 Honeycomb 才開始具備的特性,舊版 SDK 與固件無法使用。
public class ActionBarDisplayOptions extends Activity implements View.OnClickListener, ActionBar.TabListener {
private View mCustomView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action_bar_display_options);
findViewById(R.id.toggle_home_as_up).setOnClickListener(this);
findViewById(R.id.toggle_show_home).setOnClickListener(this);
findViewById(R.id.toggle_use_logo).setOnClickListener(this);
findViewById(R.id.toggle_show_title).setOnClickListener(this);
findViewById(R.id.toggle_show_custom).setOnClickListener(this);
findViewById(R.id.toggle_navigation).setOnClickListener(this);
findViewById(R.id.cycle_custom_gravity).setOnClickListener(this);
mCustomView = getLayoutInflater().inflate(R.layout.action_bar_display_options_custom, null);
final ActionBar bar = getActionBar();
bar.setCustomView(mCustomView,
new ActionBar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this));
bar.addTab(bar.newTab().setText("Tab 2").setTabListener(this));
bar.addTab(bar.newTab().setText("Tab 3").setTabListener(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.display_options_actions, menu);
return true;
}
public void onClick(View v) {
final ActionBar bar = getActionBar();
int flags = 0;
switch (v.getId()) {
case R.id.toggle_home_as_up:
flags = ActionBar.DISPLAY_HOME_AS_UP;
break;
case R.id.toggle_show_home:
flags = ActionBar.DISPLAY_SHOW_HOME;
break;
case R.id.toggle_use_logo:
flags = ActionBar.DISPLAY_USE_LOGO;
break;
case R.id.toggle_show_title:
flags = ActionBar.DISPLAY_SHOW_TITLE;
break;
case R.id.toggle_show_custom:
flags = ActionBar.DISPLAY_SHOW_CUSTOM;
break;
case R.id.toggle_navigation:
bar.setNavigationMode(
bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_STANDARD
? ActionBar.NAVIGATION_MODE_TABS
: ActionBar.NAVIGATION_MODE_STANDARD);
return;
case R.id.cycle_custom_gravity:
ActionBar.LayoutParams lp = (ActionBar.LayoutParams) mCustomView.getLayoutParams();
int newGravity = 0;
switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT:
newGravity = Gravity.CENTER_HORIZONTAL;
break;
case Gravity.CENTER_HORIZONTAL:
newGravity = Gravity.RIGHT;
break;
case Gravity.RIGHT:
newGravity = Gravity.LEFT;
break;
}
lp.gravity = lp.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK | newGravity;
bar.setCustomView(mCustomView, lp);
return;
}
int change = bar.getDisplayOptions() ^ flags;
bar.setDisplayOptions(change, flags);
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
接下來是文中涉及的 3 個 XML 布局檔:
相關的 XML 布局 action_bar_display_options.XML 程式碼為
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button android:id="@+id/toggle_home_as_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toggle_home_as_up" />
<Button android:id="@+id/toggle_show_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toggle_show_home" />
<Button android:id="@+id/toggle_use_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toggle_use_logo" />
<Button android:id="@+id/toggle_show_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toggle_show_title" />
<Button android:id="@+id/toggle_show_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toggle_show_custom" />
<Button android:id="@+id/toggle_navigation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toggle_navigation" />
<Button android:id="@+id/cycle_custom_gravity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cycle_custom_gravity" />
</LinearLayout>
相關的自定義布局檔 action_bar_display_options_custom.XML 程式碼為
<?XML version="1.0" encoding="utf-8"?>
<Button XMLns:android="https://schemas.android.com/apk/res/android"
android:text="@string/display_options_custom_button" />
相關的 menu 布局 xml 檔 display_options_actions.XML 程式碼為
<?XML version="1.0" encoding="utf-8"?>
<menu XMLns:android="https://schemas.android.com/apk/res/android">
<item android:id="@+id/simple_item"
android:title="@string/display_options_menu_item" />
</menu>
上面的 string 元素大家可以根據自己的情況進行替換。
用 HTML5 地理定位 API 打造行動混搭程式
摘要:這是五部曲系列的第一篇。本文聚焦行動 Web 應用程式近年最受歡迎的新技術之一——地理定位。高階智慧型手機多半內建 GPS,本文示範如何透過標準 API 取得位置,
混合式應用程式模型打造 WebSphere Commerce 行動 App(下)
接續上篇,本文說明 iPhone 版本的四個進階能力,並完整介紹 Android 版本的實作方式。 可翻頁的產品詳情畫面 為了讓使用者能左右滑動瀏覽產品,我們自訂了一個
HTML 5 本地儲存實戰:讓行動 Web 應用擺脫 4KB 的枷鎖
HTML 5 最實用的新能力之一,就是把「用戶端儲存」這件事真正標準化。過去十幾年,Web 開發者只能把資料硬塞進 4KB 的 Cookie 裡;現在,透過一組再簡單不過
Android 本機資料儲存 API 入門:Preference、SQLite 與內部外部儲存
許多 Android 應用都需要保存資料,例如使用者偏好、設定值、好友清單或圖片。若能在裝置本機完成儲存與讀取,就能降低對網路的依賴,也能提升使用體驗。本文整理 Andr