編輯:進階開發
這次用一個具體例子把 ActionBar 跑起來。範例做出一個簡易的選項菜單,示範 NAVIGATION_MODE_STANDARD 與 NAVIGATION_MODE_TABS 的切換,以及各項 display option 的實際效果。
先提醒一件事:ActionBar 是 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) { }
}
<?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>
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="https://schemas.android.com/apk/res/android"
android:text="@string/display_options_custom_button" />
<?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 資源請依自己的專案命名替換。
Android 市場佔有率節節攀升,年末單月增幅突破五成
自問世以來,Google 的 Android 憑藉開放、可自由改寫的先天條件贏得不少好評,平台上可用軟體的數量與市佔率同步走揚,成長曲線相當醒目。 行動瀏覽器流量創新高
Android Content Provider:跨應用程式共享資料的完整實作
Content Provider(內容提供者)的職責,是把一個應用程式的資料開放給其他應用程式查詢,這些請求由 ContentResolver 類別的方法來處理。資料的存
Eclipse 開發 Android 入門(5):新建專案的六個欄位怎麼填
在 Eclipse 裡建立新的 Android 專案時,會遇到這幾個欄位: Project Name: BrewClock Build Target:
先睹為快:Android 3.0 細節曝光
國外網站 Phandroid 近日披露了 Android 3.0(Gingerbread)的一些細節。雖然系統仍在開發中,但從這些曝光的畫面已能看出方向:外觀更順滑,視覺