下面列出了android.support.v7.app.ActionBar#setDisplayShowCustomEnabled ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
getTheme().applyStyle(PrefUtilities.getInstance().getFontStyle().getResId(), true);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(getTitle());
if (!(this instanceof NewsActivity)) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
}
}
setLastColor();
}
@Override
public void applySelfActionBar() {
if (parentActivity == null) {
return;
}
try {
ActionBar actionBar = parentActivity.getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowCustomEnabled(false);
actionBar.setCustomView(null);
updateSubtitle();
((LaunchActivity) parentActivity).fixBackButton();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setActionBarLayout(int layoutId, Context mContext) {
ActionBar actionBar = getSupportActionBar();
if (null != actionBar) {
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(layoutId, new LinearLayout(mContext), false);
ActionBar.LayoutParams layout = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
actionBar.setCustomView(v, layout);
media_net = (Button)findViewById(R.id.media_network);
media_history = (Button)findViewById(R.id.media_history);
media_setting = (Button)findViewById(R.id.media_setting);
media_net.setOnClickListener(this);
media_setting.setOnClickListener(this);
media_history.setOnClickListener(this);
}else{
Toast.makeText(MainActivity.this, "ActionBar不存在", Toast.LENGTH_SHORT).show();
}
}
public void setActionBarLayout(int layoutId, Context mContext) {
ActionBar actionBar = getSupportActionBar();
if (null != actionBar) {
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(layoutId, new LinearLayout(mContext), false);
ActionBar.LayoutParams layout = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
actionBar.setCustomView(v, layout);
netHistory = (Button) findViewById(R.id.net_history);
netScan = (Button) findViewById(R.id.net_scan);
netSetting = (Button) findViewById(R.id.net_setting);
netScan.setOnClickListener(this);
netHistory.setOnClickListener(this);
netSetting.setOnClickListener(this);
}else{
Toast.makeText(NetMediaActivty.this, "ActionBar不存在", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (isRootFragment) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
if (titleRes != 0) {
actionBar.setTitle(titleRes);
} else {
actionBar.setTitle(title);
}
actionBar.setSubtitle(subtitle);
actionBar.setDisplayShowCustomEnabled(showCustom);
actionBar.setDisplayHomeAsUpEnabled(homeAsUp);
actionBar.setDisplayShowHomeEnabled(showHome);
actionBar.setDisplayShowTitleEnabled(showTitle);
onConfigureActionBar(actionBar);
}
}
}
private void initCustomActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.material_menu_action_bar);
materialMenu = (MaterialMenuView) actionBar.getCustomView().findViewById(R.id.action_bar_menu);
materialMenu.setOnClickListener(this);
}
public static void setProgressSpinnerVisible(AppCompatActivity activity, boolean visible) {
ActionBar bar = activity.getSupportActionBar();
if (bar.getCustomView() == null) {
ProgressBar spinner = new ProgressBar(activity);
spinner.setIndeterminate(true);
bar.setCustomView(spinner);
}
bar.setDisplayShowCustomEnabled(visible);
}
@Override
protected void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
}
@Override
protected void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
}
/**
* 初始化ActionBar
*/
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setCustomView(R.layout.search_view);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setTitle(R.string.my_note);
searchView = (SearchView) ((LinearLayout) actionBar.getCustomView()).getChildAt(0);
}
public static void initCustomActionBar(AppCompatActivity activity) {
ActionBar actionBar = activity.getSupportActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.pure_android_action_bar);
}
public void closeSearch() {
if (isSearchOpened) {
editSearch.setText("");
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
restoreColorScheme();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editSearch.getWindowToken(), 0);
isSearchOpened = false;
actionBar.setDisplayShowCustomEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text = getIntent().getStringExtra("source_code");
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(false);
actionBar.setTitle("Code Preview");
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
String data = "<html>\n" +
"<header>\n" +
"<link rel=\"stylesheet\" href=\"highlight-default.min.css\">\n" +
"<script src=\"highlight.min.js\"></script>\n" +
"<script>hljs.initHighlightingOnLoad();</script>\n" +
"</header>\n" +
"<body>\n" +
"<pre><code>" +
text.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace("\"", """)
.replace("\n", "<br/>")
+ "</code></pre>" +
"</body>\n" +
"</html>";
webView.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "utf-8", "");
setContentView(webView);
}
public void closeSearch() {
if (isSearchOpened) {
editSearch.setText("");
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
restoreColorScheme();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editSearch.getWindowToken(), 0);
isSearchOpened = false;
actionBar.setDisplayShowCustomEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Configure ActionBar
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setElevation(0);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(false);
if (STYLE.getToolBarColor() != 0) {
actionBar.setBackgroundDrawable(new ColorDrawable(STYLE.getToolBarColor()));
}
}
// Setting basic content
FrameLayout rootLayout = new FrameLayout(this);
rootLayout.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
rootLayout.setBackgroundColor(STYLE.getMainBackgroundColor());
rootLayout.setId(R.id.content_frame);
setContentView(rootLayout);
// Setting Background Color
getWindow().setBackgroundDrawable(new ColorDrawable(STYLE.getMainBackgroundColor()));
}
public void setShowCustom(boolean showCustom) {
this.showCustom = showCustom;
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowCustomEnabled(showCustom);
}
}
@SuppressLint("PrivateResource")
@Override
protected void onCreate(Bundle savedInstanceState) {
switch (Integer.valueOf(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("theme", "0"))) {
case ActivityMain.THEME_LIGHT : setTheme(R.style.AppThemeLight); break;
case ActivityMain.THEME_DARK : setTheme(R.style.AppThemeDark); break;
default: setTheme(R.style.AppThemeLight); break;
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mReplaceFragmentStrategy = new PreferenceScreenNavigationStrategy.ReplaceFragment(this, R.anim.abc_fade_in, R.anim.abc_fade_out, R.anim.abc_fade_in, R.anim.abc_fade_out);
if (savedInstanceState == null) {
mSettingsFragment = FragmentSettings.newInstance(null);
getSupportFragmentManager().beginTransaction().add(R.id.content, mSettingsFragment, "Settings").commit();
} else {
mSettingsFragment = (FragmentSettings) getSupportFragmentManager().findFragmentByTag("Settings");
}
getSupportFragmentManager().addOnBackStackChangedListener(this);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
}
// Cross-fading title setup.
mTitle = getTitle();
mTitleSwitcher = new TextSwitcher(mToolbar.getContext());
mTitleSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView tv = new AppCompatTextView(mToolbar.getContext());
TextViewCompat.setTextAppearance(tv, R.style.TextAppearance_AppCompat_Widget_ActionBar_Title);
return tv;
}
});
mTitleSwitcher.setCurrentText(mTitle);
if (ab != null) {
ab.setCustomView(mTitleSwitcher);
ab.setDisplayShowCustomEnabled(true);
ab.setDisplayShowTitleEnabled(false);
}
// Add to hierarchy before accessing layout params.
// int margin = Util.dpToPxOffset(this, 16);
// ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mTitleSwitcher.getLayoutParams();
// lp.leftMargin = margin;
// lp.rightMargin = margin;
mTitleSwitcher.setInAnimation(this, R.anim.abc_fade_in);
mTitleSwitcher.setOutAnimation(this, R.anim.abc_fade_out);
android.support.v7.preference.PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
}
@Override
public void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
//You can initialize custom action bar here.
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
TextView tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
tvTitle.setText(getClass().getSimpleName().replace("Activity", ""));
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ImageView ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
ActionMenuView actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
String title = getIntent().getStringExtra(EXTRA_TITLE);
tvTitle.setText(TextUtils.isEmpty(title) ? getClass().getSimpleName().replace("Activity", "") : title);
}
@Override
public void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
//You can initialize custom action bar here.
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
TextView tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
tvTitle.setText(getClass().getSimpleName().replace("Activity", ""));
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ImageView ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
ActionMenuView actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
String title = getIntent().getStringExtra(EXTRA_TITLE);
tvTitle.setText(TextUtils.isEmpty(title) ? getClass().getSimpleName().replace("Activity", "") : title);
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picture);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(false);
actionBar.setTitle(R.string.media_picture);
int statbarHeight = Screen.getStatusBarHeight();
if (Build.VERSION.SDK_INT >= 19) {
toolbar.setPadding(0, statbarHeight, 0, 0);
}
final Bundle bundle = getIntent().getExtras();
path = bundle.getString(ARG_FILE_PATH);
int sender = bundle.getInt(ARG_OWNER, 0);
toolbar.setVisibility(View.GONE);
transitionTop = bundle.getInt(ARG_IMAGE_TOP, 0);
transitionLeft = bundle.getInt(ARG_IMAGE_LEFT, 0);
transitionWidth = bundle.getInt(ARG_IMAGE_WIDTH, 0);
transitionHeight = bundle.getInt(ARG_IMAGE_HEIGHT, 0);
transitionView = (ImageView) findViewById(R.id.transition);
backgroundView = findViewById(R.id.background);
containerView = findViewById(R.id.container);
containerView.setAlpha(0);
fragment = new PictureFragment();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.add(R.id.container, fragment)
.commit();
Bitmap bitmap = null;
try {
bitmap = ImageLoading.loadBitmapOptimized(path);
bitmapWidth = bitmap.getWidth();
bitmapHeight = bitmap.getHeight();
} catch (ImageLoadException e) {
e.printStackTrace();
return;
}
transitionView.setImageBitmap(bitmap);
if (bitmap != null)
bitmap = null;
MediaFullscreenAnimationUtils.animateForward(transitionView, bitmapWidth, bitmapHeight, transitionLeft, transitionTop, transitionWidth, transitionHeight,
new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
containerView.setAlpha(1);
transitionView.setAlpha(0f);
}
});
MediaFullscreenAnimationUtils.animateBackgroundForward(backgroundView, null);
}