下面列出了怎么用android.app.ActionBar的API类实例代码及写法,或者点击链接到github查看源代码。
@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);
// Configure several action bar elements that will be toggled by display options.
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 void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// setup action bar for tabs
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
Tab tab = actionBar
.newTab()
.setText("First tab")
.setTabListener(
new MyTabListener<DetailFragment>(this, "artist",
DetailFragment.class));
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText("Second Tab")
.setTabListener(
new MyTabListener<ImageFragment>(this, "album",
ImageFragment.class));
actionBar.addTab(tab);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getActionBar().setListNavigationCallbacks(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Arrays.asList("Todo", "Completed")), new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (itemPosition == 0) {
displayMode = DisplayMode.TODO;
} else {
displayMode = DisplayMode.COMPLETED;
}
refreshUI();
return true;
}
});
listView = (ListView)findViewById(R.id.todo_tasks_list);
refreshUI();
}
public QrReaderView(Context context, PluginRegistry.Registrar registrar, int id, Map<String, Object> params){
this.mContext = context;
this.mParams = params;
this.mRegistrar = registrar;
// 创建视图
int width = (int) mParams.get("width");
int height = (int) mParams.get("height");
_view = new QRCodeReaderView(mContext);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(width, height);
_view.setLayoutParams(layoutParams);
_view.setOnQRCodeReadListener(this);
_view.setQRDecodingEnabled(true);
_view.forceAutoFocus();
int interval = mParams.containsKey(EXTRA_FOCUS_INTERVAL) ? (int) mParams.get(EXTRA_FOCUS_INTERVAL) : 2000;
_view.setAutofocusInterval(interval);
_view.setTorchEnabled((boolean)mParams.get(EXTRA_TORCH_ENABLED));
// 操作监听
mMethodChannel = new MethodChannel(registrar.messenger(), "me.hetian.flutter_qr_reader.reader_view_" + id);
mMethodChannel.setMethodCallHandler(this);
}
@SuppressLint("NewApi")
private static void setActionBar(final ActionBar actionBar,
final com.actionbarsherlock.app.ActionBar sherlockActionBar,
final int iconResc) {
if (actionBar == null || sherlockActionBar == null) return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
actionBar.setHomeAsUpIndicator(iconResc);
sherlockActionBar.setDisplayHomeAsUpEnabled(true);
sherlockActionBar.setHomeButtonEnabled(true);
} else {
sherlockActionBar.setLogo(iconResc);
sherlockActionBar.setDisplayUseLogoEnabled(true);
sherlockActionBar.setDisplayHomeAsUpEnabled(true);
sherlockActionBar.setHomeButtonEnabled(true);
}
}
@Override
public Varargs invoke(Varargs args) {
ActionBar actionBar = LuaViewUtil.getActionBar(getGlobals());
if (actionBar != null) {
CharSequence title = actionBar.getTitle();
if (!TextUtils.isEmpty(title)) {
return valueOf(title.toString());
} else {
View view = actionBar.getCustomView();
if (view != null) {
Object tag = view.getTag(Constants.RES_LV_TAG);
return tag instanceof LuaValue ? (LuaValue) tag : NIL;
}
}
}
return NIL;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent intent = getIntent();
String title = intent.getStringExtra(getString(R.string.webview_intent_title));
String subTitle = intent.getStringExtra(getString(R.string.webview_intent_subtitle));
ActionBar actionBar = getActionBar();
actionBar.setTitle(title);
actionBar.setSubtitle(subTitle);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
fragment = (WebViewFragment) getSupportFragmentManager().findFragmentById(R.id.webview_fragment);
preferences = getSharedPreferences(getString(R.string.login_sp), MODE_PRIVATE);
editor = preferences.edit();
layoutParams = getWindow().getAttributes();
}
public static Object setActionBarDescription(Object info, Activity activity,
int contentDescRes) {
if (info == null) {
info = new SetIndicatorInfo(activity);
}
final SetIndicatorInfo sii = (SetIndicatorInfo) info;
if (sii.setHomeAsUpIndicator != null) {
try {
final ActionBar actionBar = activity.getActionBar();
sii.setHomeActionContentDescription.invoke(actionBar, contentDescRes);
if (Build.VERSION.SDK_INT <= 19) {
// For API 19 and earlier, we need to manually force the
// action bar to generate a new content description.
actionBar.setSubtitle(actionBar.getSubtitle());
}
} catch (Exception e) {
Log.w(TAG, "Couldn't set content description via JB-MR2 API", e);
}
}
return info;
}
private void updateTitle(int position) {
Log.d("updateTitle", ""+position + " count: " + articleCollectionPagerAdapter.getCount());
Slob.Blob blob = articleCollectionPagerAdapter.get(position);
CharSequence pageTitle = articleCollectionPagerAdapter.getPageTitle(position);
Log.d("updateTitle", ""+blob);
ActionBar actionBar = getActionBar();
if (blob != null) {
String dictLabel = blob.owner.getTags().get("label");
actionBar.setTitle(dictLabel);
Application app = (Application)getApplication();
app.history.add(app.getUrl(blob));
}
else {
actionBar.setTitle("???");
}
actionBar.setSubtitle(pageTitle);
}
@Override
public boolean updateStyle(List<PXRuleSet> ruleSets, List<PXStylerContext> contexts) {
if (!super.updateStyle(ruleSets, contexts)) {
return false;
}
PXStylerContext context = contexts.get(0);
ActionBar actionBar = (ActionBar) context.getStyleable();
// We just overwrite the exiting background. No states merging here.
RectF bounds = PXActionBarStyleAdapter.getActionBarBounds(actionBar);
if (PixateFreestyle.ICS_OR_BETTER) {
setBackgroundICS(context, actionBar, bounds);
} else {
// TODO - add a compatible call for earlier API
if (PXLog.isLogging()) {
PXLog.w(getClass().getSimpleName(),
"Unable to set ActionBar split background. API version < 14.");
}
}
return true;
}
/**
* 隐藏状态栏
* <p>
* 在setContentView前调用
*/
protected void hideStatusBar() {
final int sdkVer = Build.VERSION.SDK_INT;
if (sdkVer < 16) {
//4.0及一下
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
}
public static SetIndicatorInfo setActionBarDescription(SetIndicatorInfo info, Activity activity,
int contentDescRes) {
if (info == null) {
info = new SetIndicatorInfo(activity);
}
if (info.setHomeAsUpIndicator != null) {
try {
final ActionBar actionBar = activity.getActionBar();
info.setHomeActionContentDescription.invoke(actionBar, contentDescRes);
if (Build.VERSION.SDK_INT <= 19) {
// For API 19 and earlier, we need to manually force the
// action bar to generate a new content description.
actionBar.setSubtitle(actionBar.getSubtitle());
}
} catch (Exception e) {
Log.w(TAG, "Couldn't set content description via JB-MR2 API", e);
}
}
return info;
}
@Override
public void onPageSelected(int position) {
if (DEBUG) Log.d(TAG, "onPageSelected: position: " + position);
final ActionBar actionBar = getActionBar();
if (mDialpadFragment != null) {
if (mDuringSwipe && position == TAB_INDEX_DIALER) {
// TODO: Figure out if we want this or not. Right now
// - with this call, both fake buttons and real action bar overlap
// - without this call, there's tiny flicker happening to search/menu buttons.
// If we can reduce the flicker without this call, it would be much better.
// updateFakeMenuButtonsVisibility(true);
}
}
if (mCurrentPosition == position) {
Log.w(TAG, "Previous position and next position became same (" + position + ")");
}
actionBar.selectTab(actionBar.getTabAt(position));
mNextPosition = position;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab = actionBar.newTab().setText(R.string.tab1_title)
.setTabListener(new TabListener(this, Fragment1.class.getName()));
actionBar.addTab(tab);
tab = actionBar.newTab().setText(R.string.tab2_title)
.setTabListener(new TabListener(this, Fragment2.class.getName()));
actionBar.addTab(tab);
tab = actionBar.newTab().setText(R.string.tab3_title)
.setTabListener(new TabListener(this, Fragment3.class.getName()));
actionBar.addTab(tab);
// Initiate Pixate
PixateFreestyle.init(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.core_material);
if (isActionBarNecessary() == false) {
ActionBar actionBar = getActionBar();
actionBar.hide();
return;
}
setupActionBarVisibility();
setupActionBarAttributes();
setupLeftNavigationMenu();
setupLeftNavigationClickListener();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_main);
// setup the action bar to show a dropdown list
final ActionBar aBar = getActionBar();
aBar.setDisplayShowTitleEnabled(false);
aBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// setup the dropdown list navigation in the action bar
aBar.setListNavigationCallbacks(
new ArrayAdapter<String>(aBar.getThemedContext(),
android.R.layout.simple_list_item_1,android.R.id.text1,
new String[] { getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
getString(R.string.title_section4),
getString(R.string.title_section5),
getString(R.string.title_section6) } ),this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parallax_everywhere_sample);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override @TargetApi(14)
protected void setActionBarSettings(Activity activity) {
ActionBar actionBar = activity.getActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setIcon(getDrawable());
}
@SuppressLint("NewApi")
public void showSearch() {
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.uv_view_flipper);
viewFlipper.setDisplayedChild(1);
if (hasActionBar()) {
if (originalNavigationMode == -1)
originalNavigationMode = getActionBar().getNavigationMode();
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
}
/**
* Updates the status on the action bar.
*
* @param resId a string resource ID
*/
private void setStatus(int resId) {
FragmentActivity activity = getActivity();
if (null == activity) {
return;
}
final ActionBar actionBar = activity.getActionBar();
if (null == actionBar) {
return;
}
actionBar.setSubtitle(resId);
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
TitlesFragment titleFrag = (TitlesFragment) getFragmentManager()
.findFragmentById(R.id.frag_title);
titleFrag.populateTitles(tab.getPosition());
titleFrag.selectPosition(0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
}
@SuppressLint("WrongConstant")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
Objects.requireNonNull(getSupportActionBar()).setElevation(0);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar);
MenuCompat.setGroupDividerEnabled(menu, true);
return true;
}
/**
* ViewPagerを持つレイアウトを自動的に設定する.
* サブクラスでオーバーライドする場合は setContentView を<strong>実行しないこと</strong>。
*
* @param savedInstanceState パラメータ
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting_page);
mViewPager = (ViewPager) findViewById(R.id.setting_pager);
mViewPager.setAdapter(new DConnectPagerAdapter(this));
if (getActionBar() != null) {
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_HOME);
getActionBar().setTitle(R.string.activity_setting_page_title);
}
}
@Override
public Context getActionBarThemedContext() {
final ActionBar actionBar = mActivity.getActionBar();
final Context context;
if (actionBar != null) {
context = actionBar.getThemedContext();
} else {
context = mActivity;
}
return context;
}
public final ActionBar getActionBar() {
NextBaseActivity activity = getBaseActivity();
if (activity != null) {
return activity.getActionBar();
}
return null;
}
@Override
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Get preferences
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(this);
boolean theme = preferences.getBoolean(Main.PREF_DARK, true);
if (!theme)
setTheme(R.style.AppLightTheme);
setContentView(R.layout.help);
TextView view = findViewById(R.id.help);
CharSequence text = read(this, R.raw.help);
if (view != null)
{
view.setMovementMethod(LinkMovementMethod.getInstance());
view.setText(Html.fromHtml(text.toString()));
}
// Enable back navigation on action bar
ActionBar actionBar = getActionBar();
if (actionBar != null)
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(Themes.getTheme(((Application) getApplication()).getThemeIndex()));
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_license);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setFiles();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
processSetTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.stma_add);
id = getIntent().getIntExtra("id", -5);
isTimeTask = getIntent().getBooleanExtra("time", true);
ActionBar actionBar = getActionBar();
processActionBar(actionBar);
if (actionBar != null) {
actionBar.setTitle(getIntent().getStringExtra("label"));
}
init();
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
Fragment frag = appSectionsPagerAdapter.getItem(tab.getPosition());
if (frag instanceof BaseListFragment) {
((BaseListFragment)frag).finishActionMode();
}
if (tab.getPosition() == 0) {
View v = this.getCurrentFocus();
if (v != null){
InputMethodManager mgr = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}