下面列出了android.app.ActionBar#setCustomView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressLint("NewApi")
private void setupActionBar() {
ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.setCustomView(Resources.layout.search_actionbar(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
View backButton =
actionBar.getCustomView()
.findViewById(Resources.id.search_actionbar_back(this));
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
finish();
}
});
}
}
@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));
}
/**
* 设置ActionBar的布局
*
* @param layoutId 布局Id
*/
public void setActionBarLayout(int layoutId) {
ActionBar actionBar = getActionBar();
if (null != actionBar) {
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(layoutId, null);
ActionBar.LayoutParams layout = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
actionBar.setCustomView(v, layout);
}
}
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_activity);
ButterKnife.inject(this);
pagerView.setAdapter(new SamplePagerAdapter(this));
Switch enabledSwitch = new Switch(this);
enabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (first) {
first = false;
Toast.makeText(SampleActivity.this, R.string.first_run, LENGTH_LONG).show();
}
scalpelView.setLayerInteractionEnabled(isChecked);
invalidateOptionsMenu();
}
});
ActionBar actionBar = getActionBar();
actionBar.setCustomView(enabledSwitch);
actionBar.setDisplayOptions(DISPLAY_SHOW_TITLE | DISPLAY_SHOW_CUSTOM);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// actionBar.setHomeAsUpIndicator(null);
// add the custom view to the action bar
actionBar.setCustomView(R.layout.toolbarsearch);
EditText search = (EditText) actionBar.getCustomView().findViewById(
R.id.searchfield);
search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
onClick(null);
return false;
}
});
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
public void init(ActionBar actionbar) {
LayoutInflater inflator = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.photoviewer_action_bar, null);
mPhotoTitle = (TextView) v.findViewById(R.id.photoTitle);
mPhotoAuthor = (TextView) v.findViewById(R.id.photoAuthor);
mTextUtils.setFont(mPhotoTitle, TextUtils.FONT_ROBOTOLIGHT);
mTextUtils.setFont(mPhotoAuthor, TextUtils.FONT_ROBOTOTHIN);
actionbar.setDisplayShowCustomEnabled(true);
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setCustomView(v);
}
public void init(ActionBar actionbar) {
LayoutInflater inflator = (LayoutInflater)
mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.action_bar_title_item, null);
TextView abTitle = (TextView) v.findViewById(R.id.abTitle);
mTextUtils.setFont(abTitle, TextUtils.FONT_SHADOWSINTOLIGHT);
abTitle.setText(mActivity.getString(R.string.app_name));
actionbar.setDisplayShowCustomEnabled(true);
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setCustomView(v);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// BEGIN_INCLUDE (inflate_set_custom_view)
// Inflate a "Done" custom action bar view to serve as the "Up" affordance.
final LayoutInflater inflater = (LayoutInflater) getActionBar().getThemedContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
final View customActionBarView = inflater.inflate(
R.layout.actionbar_custom_view_done, null);
customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// "Done"
finish();
}
});
// Show the custom action bar view and hide the normal Home icon and title.
final ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setCustomView(customActionBarView);
// END_INCLUDE (inflate_set_custom_view)
setContentView(R.layout.activity_done_button);
}
private void renderLayout(ArrayList<DeliveryRequest> list) {
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_top); // load your layout
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_CUSTOM); // show it
actionBar.setDisplayHomeAsUpEnabled(true);
numVS = (TextView) actionBar.getCustomView().findViewById(R.id.num_vs);
numVS.setText("0");
listAdapter.clear();
listAdapter.notifyDataSetChanged();
for (DeliveryRequest dr : list) {
new AsyncTask<DeliveryRequest, Void, PublishRow>() {
@Override
protected PublishRow doInBackground(DeliveryRequest... params) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(params[0].getLastTime());
String info = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).format(cal.getTime());
return new PublishRow(params[0].getId(), params[0].getUrl(), params[0].getClientID(),params[0].getClientSecret(), params[0].isActive(), "Last connection: " + info, params[0].getVsname());
}
@Override
protected void onPostExecute(PublishRow result) {
listAdapter.add(result);
listAdapter.notifyDataSetChanged();
numVS.setText(listAdapter.getCount() + "");
}
}.execute(dr);
}
TextView lastUpdate = (TextView) actionBar.getCustomView().findViewById(
R.id.lastUpdate);
lastUpdate.setText("Last update:\n" + (new Date()).toString());
}
private void renderLayout(ArrayList<Subscription> list) {
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_top); // load your layout
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_CUSTOM); // show it
getActionBar().setDisplayHomeAsUpEnabled(true);
numVS = (TextView) actionBar.getCustomView().findViewById(R.id.num_vs);
numVS.setText("0");
listAdapter.clear();
listAdapter.notifyDataSetChanged();
for (Subscription su : list) {
new AsyncTask<Subscription, Void, SubscribeRow>() {
@Override
protected SubscribeRow doInBackground(Subscription... params) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(params[0].getLastTime());
String info = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).format(cal.getTime());
return new SubscribeRow(params[0].getId(), params[0].getUrl(), params[0].isActive(), "Last connection: " + info, params[0].getVsname());
}
@Override
protected void onPostExecute(SubscribeRow result) {
listAdapter.add(result);
listAdapter.notifyDataSetChanged();
numVS.setText(listAdapter.getCount() + "");
}
}.execute(su);
}
TextView lastUpdate = (TextView) actionBar.getCustomView().findViewById(
R.id.lastUpdate);
lastUpdate.setText("Last update:\n" + (new Date()).toString());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Customize the action bar
ActionBar actionBar = getActionBar();
if(actionBar != null) {
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.header, null);
Typeface scriptFont = Typeface.createFromAsset(getAssets(), "fonts/script.ttf");
TextView title = (TextView) v.findViewById(R.id.title);
title.setTypeface(scriptFont);
actionBar.setCustomView(v);
}
//Initialize Views
postListView = (AnimatedListView) findViewById(R.id.post_list);
postListView.setOnItemClickListener(postItemSelectedListener);
progressBar = (ProgressBar) findViewById(R.id.loader);
//Register as a Post List update listener
KWBApplication application = (KWBApplication) getApplication();
application.registerPostUpdateListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
Bitmap b = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
actionBar.
setBackgroundDrawable(new BitmapDrawable(getResources(), b));
// add the custom view to the action bar
actionBar.setCustomView(R.layout.actionbar_view);
EditText search = (EditText) actionBar.getCustomView().findViewById(
R.id.searchfield);
search.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Toast.makeText(MainActivity.this, "Search triggered",
Toast.LENGTH_LONG).show();
return false;
}
});
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
}
@Override public View onCreateActionBarView(LayoutInflater inflater) {
ActionBar ab = getActivity().getActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowCustomEnabled(true);
ab.setCustomView(R.layout.paycheckactivity_income);
mIncome = (EditMoney) ab.getCustomView().findViewById(R.id.amount);
mIncome.setCents(mIncomeValue);
mIncome.addTextChangedListener(this);
mDescription = (EditText) ab.getCustomView().findViewById(R.id.description);
return ab.getCustomView();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_view);
actionBar.setDisplayShowCustomEnabled(true);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null && savedInstanceState.getInt("theme", -1) != -1) {
mThemeId = savedInstanceState.getInt("theme");
this.setTheme(mThemeId);
}
setContentView(R.layout.main);
Directory.initializeDirectory(new OnDirectoryChanged());
ActionBar bar = getActionBar();
int i;
for (i = 0; i < Directory.getCategoryCount(); i++) {
bar.addTab(bar.newTab().setText(Directory.getCategory(i).getName())
.setTabListener(this));
}
mActionBarView = getLayoutInflater().inflate(
R.layout.action_bar_custom, null);
bar.setCustomView(mActionBarView);
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowHomeEnabled(true);
// If category is not saved to the savedInstanceState,
// 0 is returned by default.
if(savedInstanceState != null) {
int category = savedInstanceState.getInt("category");
bar.selectTab(bar.getTabAt(category));
}
}
protected void init() {
setContentView(R.layout.wallpaper_cropper);
mCropView = (CropView) findViewById(R.id.cropView);
mProgressView = findViewById(R.id.loading);
Intent cropIntent = getIntent();
final Uri imageUri = cropIntent.getData();
if (imageUri == null) {
Log.e(LOGTAG, "No URI passed in intent, exiting WallpaperCropActivity");
finish();
return;
}
// Action bar
// Show the custom action bar view
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_set_wallpaper);
actionBar.getCustomView().setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean finishActivityWhenDone = true;
cropImageAndSetWallpaper(imageUri, null, finishActivityWhenDone);
}
});
mSetWallpaperButton = findViewById(R.id.set_wallpaper_button);
// Load image in background
final BitmapRegionTileSource.UriBitmapSource bitmapSource =
new BitmapRegionTileSource.UriBitmapSource(getContext(), imageUri);
mSetWallpaperButton.setEnabled(false);
Runnable onLoad = new Runnable() {
public void run() {
if (bitmapSource.getLoadingState() != BitmapSource.State.LOADED) {
Toast.makeText(getContext(), R.string.wallpaper_load_fail,
Toast.LENGTH_LONG).show();
finish();
} else {
mSetWallpaperButton.setEnabled(true);
}
}
};
setCropViewTileSource(bitmapSource, true, false, null, onLoad);
}
@Override
protected void onCreate (final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.prefs = new Prefs(getBaseContext());
if (!this.prefs.isConfigured()) {
startActivity(new Intent(getApplicationContext(), SetupActivity.class));
finish();
return;
}
this.prefs.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
try {
this.conf = this.prefs.asConfig();
}
catch (final Exception e) { // No point continuing if any exception.
LOG.wtf("Unparsable config.", e);
DialogHelper.alertAndClose(this, e);
return;
}
this.imageCache = new HybridBitmapCache(this, C.MAX_MEMORY_IMAGE_CACHE);
if (this.prefs.getSharedPreferences().getBoolean(AdvancedPrefFragment.KEY_THREAD_INSPECTOR, false)) {
final TextView jobStatus = (TextView) findViewById(R.id.jobStatus);
jobStatus.setVisibility(View.VISIBLE);
this.executorStatus = new ExecutorStatus(jobStatus);
}
this.localEs = ExecUtils.newBoundedCachedThreadPool(C.LOCAL_MAX_THREADS, new LogWrapper("LES"), this.executorStatus);
this.netEs = ExecUtils.newBoundedCachedThreadPool(C.NET_MAX_THREADS, new LogWrapper("NES"), this.executorStatus);
this.msgHandler = new MessageHandler(this);
final float columnWidth = UiPrefFragment.readColumnWidth(this, this.prefs);
this.columnsRtl = UiPrefFragment.readColumnsRtl(this.prefs);
// If this becomes too memory intensive, switch to android.support.v4.app.FragmentStatePagerAdapter.
final SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), this, columnWidth);
this.viewPager = (SidebarAwareViewPager) findViewById(R.id.pager);
this.pageSelectionListener = new VisiblePageSelectionListener(columnWidth);
final MultiplexingOnPageChangeListener onPageChangeListener = new MultiplexingOnPageChangeListener(
this.pageSelectionListener,
new NotificationClearingPageSelectionListener(this));
this.viewPager.setOnPageChangeListener(onPageChangeListener);
this.viewPager.setAdapter(sectionsPagerAdapter);
if (!showPageFromIntent(getIntent())) {
if (this.columnsRtl) {
gotoPage(0);
}
else {
onPageChangeListener.onPageSelected(this.viewPager.getCurrentItem());
}
}
final ActionBar ab = getActionBar();
ab.setDisplayShowHomeEnabled(true);
ab.setHomeButtonEnabled(true);
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowCustomEnabled(true);
this.columnTitleStrip = new ColumnTitleStrip(ab.getThemedContext());
this.columnTitleStrip.setViewPager(this.viewPager);
this.columnTitleStrip.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
this.columnTitleStrip.setColumnClickListener(new TitleClickListener(this));
ab.setCustomView(this.columnTitleStrip);
AlarmReceiver.configureAlarms(this);
new CheckBackgroundUpdatesRunning(this, this.executorStatus).executeOnExecutor(this.localEs);
}
protected void init() {
setContentView(R.layout.wallpaper_cropper);
mCropView = (CropView) findViewById(R.id.cropView);
Intent cropIntent = getIntent();
final Uri imageUri = cropIntent.getData();
if (imageUri == null) {
Log.e(LOGTAG, "No URI passed in intent, exiting WallpaperCropActivity");
finish();
return;
}
// Action bar
// Show the custom action bar view
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_set_wallpaper);
actionBar.getCustomView().setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean finishActivityWhenDone = true;
cropImageAndSetWallpaper(imageUri, null, finishActivityWhenDone);
}
});
mSetWallpaperButton = findViewById(R.id.set_wallpaper_button);
// Load image in background
final BitmapRegionTileSource.UriBitmapSource bitmapSource =
new BitmapRegionTileSource.UriBitmapSource(this, imageUri, 1024);
mSetWallpaperButton.setEnabled(false);
Runnable onLoad = new Runnable() {
public void run() {
if (bitmapSource.getLoadingState() != BitmapSource.State.LOADED) {
Toast.makeText(WallpaperCropActivity.this,
getString(R.string.wallpaper_load_fail),
Toast.LENGTH_LONG).show();
finish();
} else {
mSetWallpaperButton.setEnabled(true);
}
}
};
setCropViewTileSource(bitmapSource, true, false, onLoad);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInstance = this;
setContentView(R.layout.activity_settings);
prefs = new PrefsGet(getSharedPreferences(getPackageName(), Context.MODE_MULTI_PROCESS));
mContext = this;
mIsLollipop = Tools.isLollipop(true);
mIsAtLeastLollipop = Tools.isLollipop(false);
if (showChangelog(prefs)) {
launchChangelog();
}
if (mIsAtLeastLollipop && needsUsPermission()) {
launchUsPermission(mContext);
}
display = getWindowManager().getDefaultDisplay();
updateDisplayWidth();
myService = new ServiceCall(mContext);
myService.setConnection(mConnection);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setTitle(R.string.title_activity_settings);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setCustomView(R.layout.action_spinner);
setUpSpinner((Spinner) actionBar.getCustomView().findViewById(R.id.config_spinner));
actionBar.setDisplayShowCustomEnabled(true);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(4);
mGetFragments = new GetFragments();
mGetFragments.setFm(getFragmentManager());
mGetFragments.setVp(mViewPager);
ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
};
mViewPager.setOnPageChangeListener(pageChangeListener);
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
pageChangeListener.onPageSelected(GENERAL_TAB);
}
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);
}