編輯:高級開發
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="http://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="http://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="http://schemas.android.com/apk/res/android">
< item android:id="@+id/simple_item"
android:title="@string/display_options_menu_item" />
< /menu>
上面的string後的元素大家可以根據自己的情況進行替換。
ActionBar相關的示例代碼第二部分分為兩種,作為android 3.0的重要特性我們直接看代碼:
一、使用菜單資源構造
public class ActionBarMechanics extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Normal item");
MenuItem actionItem = menu.add("Action Button");
actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
actionItem.setIcon(android.R.drawable.ic_menu_share);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
}
二、作為Tab切換Fragment
public class ActionBarTabs extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.action_bar_tabs);
}
public void onAddTab(VIEw v) {
final ActionBar bar = getActionBar();
接上頁
final int tabCount = bar.getTabCount();
final String text = "Tab " + tabCount;
bar.addTab(bar.newTab()
.setText(text)
.setTabListener(new TabListener(new TabContentFragment(text))));
}
public void onRemoveTab(VIEw v) {
final ActionBar bar = getActionBar();
bar.removeTabAt(bar.getTabCount() - 1);
}
public void onToggleTabs(VIEw v) {
final ActionBar bar = getActionBar();
if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
} else {
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
}
}
public void onRemoveAllTabs(VIEw v) {
getActionBar().removeAllTabs();
}
private class TabListener implements ActionBar.TabListener {
private TabContentFragment mFragment;
public TabListener(TabContentFragment fragment) {
mFragment = fragment;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.fragment_content, mFragment, mFragment.getText());
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show();
}
}
private class TabContentFragment extends Fragment {
private String mText;
public TabContentFragment(String text) {
mText = text;
}
public String getText() {
return mText;
接上頁
}
@Override
public View onCreateView(LayoutInflater inflater, VIEwGroup container,
Bundle savedInstanceState) {
View fragVIEw = inflater.inflate(R.layout.action_bar_tab_content, container, false);
TextView text = (TextView) fragView.findVIEwById(R.id.text);
text.setText(mText);
return fragVIEw;
}
}
}
涉及的布局文件action_bar_tabs.XML代碼為
< ?XML version="1.0" encoding="utf-8"?>
< LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orIEntation="vertical">
< FrameLayout android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
< LinearLayout android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orIEntation="vertical">
< Button android:id="@+id/btn_add_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_add_tab"
android:onClick="onAddTab" />
< Button android:id="@+id/btn_remove_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_remove_tab"
android:onClick="onRemoveTab" />
< Button android:id="@+id/btn_toggle_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_toggle_tabs"
android:onClick="onToggleTabs" />
< Button android:id="@+id/btn_remove_all_tabs"
android:layout_width="wrap_content"
接上頁
android:layout_height="wrap_content"
android:text="@string/btn_remove_all_tabs"
android:onClick="onRemoveAllTabs" />
< /LinearLayout>
< /LinearLayout>
涉及布局文件action_bar_tab_content.XML的代碼為
< ?XML version="1.0" encoding="utf-8"?>
< TextVIEw XMLns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />