本文介绍了Action Bar、Action Bar Tabs、Action Mode、显示选项、Fragment添加到选项菜单、滚动时隐藏Action Bar、PreferenceActivity、SettingsActionProvider及ShareActionProvider组件的使用。
Action Bar Mechanics
演示了基本的Action Bar和怎样和标准的选项按钮交互。这个例子仅用于信息目的;对于使用Action Bar更常见的方法请查看ActionBarUsage示例。
1 | public class ActionBarMechanics extends AppCompatActivity { |
Action Bar Tabs
演示了Action Bar Tabs的使用和它们怎样和action bar的其它features交互。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66public void onAddTab(View v) {
final ActionBar bar = getSupportActionBar();
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 = getSupportActionBar();
if (bar.getTabCount() > 0) {
bar.removeTabAt(bar.getTabCount() - 1);
}
}
public void onToggleTabs(View v) {
final ActionBar bar = getSupportActionBar();
if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
} else {
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
}
public void onRemoveAllTabs(View v) {
getSupportActionBar().removeAllTabs();
}
/**
* A TabListener receives event callbacks from the action bar as tabs
* are deselected, selected, and reselected. A FragmentTransaction
* is provided to each of these callbacks; if any operations are added
* to it, it will be committed at the end of the full tab switch operation.
* This lets tab switches be atomic without the app needing to track
* the interactions between different tabs.
*
* NOTE: This is a very simple implementation that does not retain
* fragment state of the non-visible tabs across activity instances.
* Look at the FragmentTabs example for how to do a more complete
* implementation.
*/
private class TabListener implements ActionBar.TabListener {
private TabContentFragment mFragment;
public TabListener(TabContentFragment fragment) {
mFragment = fragment;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.fragment_content, mFragment, mFragment.getText());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show();
}
}
Action Bar Usage
演示了Action Bar的常见用法。默认的Honeycomb主题(Android 3.0)默认情况下包含action bar和一个菜单资源用于填充菜单数据。如果你想看看这些是怎样工作的,可以查看ActionBarMechanics类。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69public class ActionBarUsage extends AppCompatActivity {
TextView mSearchText;
int mSortMode = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSearchText = new TextView(this);
setContentView(mSearchText);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actions, menu);
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(menu.findItem(R.id.action_search));
searchView.setOnQueryTextListener(mOnQueryTextListener);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (mSortMode != -1) {
Drawable icon = menu.findItem(mSortMode).getIcon();
menu.findItem(R.id.action_sort).setIcon(icon);
}
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_sort_alpha:
case R.id.action_sort_size:
onSort(item);
break;
}
Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
private void onSort(MenuItem item) {
mSortMode = item.getItemId();
// Request a call to onPrepareOptionsMenu so we can change the sort icon
supportInvalidateOptionsMenu();
}
// The following callbacks are called for the SearchView.OnQueryChangeListener
// For more about using SearchView, see src/.../view/SearchView1.java and SearchView2.java
private final SearchView.OnQueryTextListener mOnQueryTextListener =
new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
newText = TextUtils.isEmpty(newText) ? "" : "Query so far: " + newText;
mSearchText.setText(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(ActionBarUsage.this,
"Searching for: " + query + "...", Toast.LENGTH_SHORT).show();
return true;
}
};
}
Action Mode
演示了action mode的常见用法。
菜单资源:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:title="@string/action_bar_search"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
<item android:id="@+id/action_add"
android:icon="@android:drawable/ic_menu_add"
android:title="@string/action_bar_add" />
<item android:id="@+id/action_edit"
android:icon="@android:drawable/ic_menu_edit"
android:title="@string/action_bar_edit"
app:showAsAction="always" />
<item android:id="@+id/action_share"
android:icon="@android:drawable/ic_menu_share"
android:title="@string/action_bar_share"
android:enabled="false"
app:showAsAction="ifRoom" />
<item android:id="@+id/action_sort"
android:icon="@android:drawable/ic_menu_sort_by_size"
android:title="@string/action_bar_sort"
app:showAsAction="ifRoom">
<menu>
<item android:id="@+id/action_sort_size"
android:icon="@android:drawable/ic_menu_sort_by_size"
android:title="@string/action_bar_sort_size" />
<item android:id="@+id/action_sort_alpha"
android:icon="@android:drawable/ic_menu_sort_alphabetically"
android:title="@string/action_bar_sort_alpha" />
</menu>
</item>
</menu>
相关代码
1 | private void startActionMode() { |
Display Options
演示了Action Bar各个显示选项flags可以怎样被结合和它们的效果。
1 | public class ActionBarDisplayOptions extends AppCompatActivity |
Fragment Menu
演示了Fragment怎样加入到选项菜单中。共有三个fragment,MenuFragment包含Menu 1A和Menu 1B,Menu2Fragment包含Menu 2,Menu3Fragment嵌套在Menu2Fragment中,包含MENU ITEM。
1 | // Update fragment visibility based on current check box state. |
Hide on Scroll
演示了Action Bar在内容滚动时隐藏的用法。
1 | @Override |
Preferences
演示了怎样使用我们自己定义的AppCompat。在使用的时候直接继承我们自定义的AppCompat类就可以了。
AppCompatPreferenceActivity实现和代理了使用AppCompat必要的调用。这个技术可以用于Activity类,不只是PreferenceActivity。
1 | public abstract class AppCompatPreferenceActivity extends PreferenceActivity { |
Settings Action Provider
演示了怎样实现一个ActionProvider在Action Bar上添加功能。尤其是这个示例创建了一个ActionProvider调起系统设置并且使用provider添加一个菜单item。
菜单资源:1
2
3
4
5
6
7
8
9
10
11
12<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_item_action_provider_action_bar"
android:title="@string/action_bar_settings"
app:showAsAction="ifRoom"
app:actionProviderClass="com.example.android.supportv7.app.ActionBarSettingsActionProviderActivity$SettingsActionProvider"/>
<item android:id="@+id/menu_item_action_provider_overflow"
android:title="@string/action_bar_settings"
app:showAsAction="never"
app:actionProviderClass="com.example.android.supportv7.app.ActionBarSettingsActionProviderActivity$SettingsActionProvider"/>
</menu>
SettingsActionProvider类代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37public static class SettingsActionProvider extends ActionProvider {
/** An intent for launching the system settings. */
private static final Intent sSettingsIntent = new Intent(Settings.ACTION_SETTINGS);
/**
* Creates a new instance.
*
* @param context Context for accessing resources.
*/
public SettingsActionProvider(Context context) {
super(context);
}
@Override
public View onCreateActionView() {
// Inflate the action view to be shown on the action bar.
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View view = layoutInflater.inflate(R.layout.action_bar_settings_action_provider, null);
ImageButton button = (ImageButton) view.findViewById(R.id.button);
// Attach a click listener for launching the system settings.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getContext().startActivity(sSettingsIntent);
}
});
return view;
}
@Override
public boolean onPerformDefaultAction() {
// This is called if the host menu item placed in the overflow menu of the
// action bar is clicked and the host activity did not handle the click.
getContext().startActivity(sSettingsIntent);
return true;
}
}
ShareActionProvider
演示了怎样在Action Bar上使用ShareActionProvider。
菜单资源:1
2
3
4
5
6
7
8
9<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_item_share_provider_action_bar"
android:title="@string/action_bar_share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
</menu>
ActionBarShareActionProvider类代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_share_action_provider, menu);
return true;
}
@Override
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
final ShareActionProvider sap = (ShareActionProvider) MenuItemCompat.getActionProvider(
menu.findItem(R.id.menu_item_share_provider_action_bar));
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello!");
sap.setShareIntent(shareIntent);
return true;
}
从Android SDK目录可找到源码,或从这里下载