下面列出了android.view.Window#requestFeature ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Window dialogWindow = getDialog().getWindow();
if (null != dialogWindow) {
WindowManager.LayoutParams attrs = dialogWindow.getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
dialogWindow.setAttributes(attrs);
dialogWindow.requestFeature(Window.FEATURE_NO_TITLE);
}
super.onActivityCreated(savedInstanceState);
if (null != dialogWindow) {
dialogWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
dialogWindow.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Window dialogWindow = getDialog().getWindow();
if (null != dialogWindow) {
WindowManager.LayoutParams attrs = dialogWindow.getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
dialogWindow.setAttributes(attrs);
dialogWindow.requestFeature(Window.FEATURE_NO_TITLE);
}
super.onActivityCreated(savedInstanceState);
if (null != dialogWindow) {
dialogWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
dialogWindow.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
}
private void initDialog() {
contentLayout = new FrameLayout(activity);
contentLayout.setLayoutParams(new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
contentLayout.setFocusable(true);
contentLayout.setFocusableInTouchMode(true);
dialog = new Dialog(activity);
dialog.setCanceledOnTouchOutside(true);//触摸屏幕取消窗体
dialog.setCancelable(true);//按返回键取消窗体
dialog.setOnKeyListener(this);
dialog.setOnDismissListener(this);
Window window = dialog.getWindow();
if (window != null) {
window.setGravity(Gravity.BOTTOM);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//AndroidRuntimeException: requestFeature() must be called before adding content
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setContentView(contentLayout);
}
setSize(screenWidthPixels, WRAP_CONTENT);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.item_highlight, container);
v.findViewById(R.id.page_part_number).setVisibility(View.GONE);
v.findViewById(R.id.date_time).setVisibility(View.GONE);
Dialog dialog = getDialog();
Window window = dialog.getWindow();
if (window != null) {
window.setGravity(Gravity.BOTTOM);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.requestFeature(Window.FEATURE_NO_TITLE);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
return v;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.folderchooser_dialog, null);
Window window = getDialog().getWindow();
window.requestFeature(Window.FEATURE_NO_TITLE);
// retrieve display dimensions
Rect displayRectangle = new Rect();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
int w = (int) (displayRectangle.width() * 0.95);
int h = (int) (displayRectangle.height() * 0.9);
window.setLayout(w, h);
WindowManager.LayoutParams params = window.getAttributes();
window.setAttributes(params);
return view;
}
/**
* 5.0以上切换NavigationBar颜色
* !!!注意!!!需要在Activity的onCreate()之前调用以下方法
*/
public static void changeNavigationBarColor(Activity activity, int color)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
Window window = activity.getWindow();
window.requestFeature(Window.FEATURE_NO_TITLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setNavigationBarColor(color);//切换导航栏颜色
}
}
private void initDialog() {
contentLayout = new FrameLayout(activity);
contentLayout.setLayoutParams(new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
contentLayout.setFocusable(true);
contentLayout.setFocusableInTouchMode(true);
dialog = new Dialog(activity);
dialog.setCanceledOnTouchOutside(true);//触摸屏幕取消窗体
dialog.setCancelable(true);//按返回键取消窗体
dialog.setOnKeyListener(this);
dialog.setOnDismissListener(this);
Window window = dialog.getWindow();
if (window != null) {
window.setGravity(Gravity.BOTTOM);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//AndroidRuntimeException: requestFeature() must be called before adding content
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setContentView(contentLayout);
}
setSize(screenWidthPixels, WRAP_CONTENT);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Window window = getDialog().getWindow();
if (window == null) return;
window.requestFeature(Window.FEATURE_NO_TITLE);
super.onActivityCreated(savedInstanceState);
// window.setBackgroundDrawable(new ColorDrawable(0x00000000));
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//设置无标题
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置从底部弹出
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setAttributes(params);
return null;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
Window window = dialog.getWindow();
window.requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
static void processAddTranslucent(Activity activity) {
Window window = activity.getWindow();
if (window != null) {
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setBackgroundDrawableResource(R.color.realTranslucent);
if (Build.VERSION.SDK_INT >= 19) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
}
private void initView() {
Window window = getWindow();
if (window == null) {
return;
}
window.requestFeature(Window.FEATURE_NO_TITLE);
int layoutId = getLayoutResId();
if(layoutId != 0){
setContentView(layoutId);
}else{
View contentView = onCreateView();
if(contentView != null){
setContentView(contentView);
}else{
throw new IllegalArgumentException("initialize failed.check if you have call onCreateView or getLayoutResId");
}
}
setCancelable(true);
setCanceledOnTouchOutside(true);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
}
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
onInitView(window);
}
@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
Window window = dialog.getWindow();
if (window != null) {
window.requestFeature(Window.FEATURE_NO_TITLE);
}
return dialog;
}
@NonNull
@Override
public final Dialog onCreateDialog(@Nullable Bundle bundle) {
Dialog dialog = super.onCreateDialog(bundle);
Context context = dialog.getContext();
int surfaceColor =
MaterialAttributes.resolveOrThrow(
context, R.attr.colorSurface, TimePickerDialog.class.getCanonicalName());
MaterialShapeDrawable background =
new MaterialShapeDrawable(
context,
null,
0,
R.style.Widget_MaterialComponents_TimePicker);
background.initializeElevationOverlay(context);
background.setFillColor(ColorStateList.valueOf(surfaceColor));
Window window = dialog.getWindow();
window.setBackgroundDrawable(background);
window.requestFeature(Window.FEATURE_NO_TITLE);
// On some Android APIs the dialog won't wrap content by default. Explicitly update here.
window.setLayout(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
return dialog;
}
public static void requestFullscreen(final Activity pActivity) {
final Window window = pActivity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
window.requestFeature(Window.FEATURE_NO_TITLE);
}
/**
* @param baseActivity
* @param statusBarColor 状态栏的颜色
* @param navigationBarColor 导航栏的颜色
*/
public static void immersiveAboveAPI23(AppCompatActivity baseActivity, boolean isMarginStatusBar
, boolean isMarginNavigationBar, int statusBarColor, int navigationBarColor, boolean isDarkStatusBarIcon) {
try {
Window window = baseActivity.getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
//4.4版本及以上 5.0版本及以下
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (isMarginStatusBar && isMarginNavigationBar) {
//5.0版本及以上
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
CropLightStatusBarUtils.setLightStatusBar(baseActivity, isMarginStatusBar
, isMarginNavigationBar
, statusBarColor == Color.TRANSPARENT
, isDarkStatusBarIcon);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else if (!isMarginStatusBar && !isMarginNavigationBar) {
window.requestFeature(Window.FEATURE_NO_TITLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
CropLightStatusBarUtils.setLightStatusBar(baseActivity, isMarginStatusBar
, isMarginNavigationBar
, statusBarColor == Color.TRANSPARENT
, isDarkStatusBarIcon);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else if (!isMarginStatusBar && isMarginNavigationBar) {
window.requestFeature(Window.FEATURE_NO_TITLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
CropLightStatusBarUtils.setLightStatusBar(baseActivity, isMarginStatusBar
, isMarginNavigationBar
, statusBarColor == Color.TRANSPARENT
, isDarkStatusBarIcon);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else {
//留出来状态栏 不留出来导航栏 没找到办法。。
return;
}
window.setStatusBarColor(statusBarColor);
window.setNavigationBarColor(navigationBarColor);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param baseActivity
* @param statusBarColor 状态栏的颜色
* @param navigationBarColor 导航栏的颜色
*/
public static void immersiveAboveAPI23(AppCompatActivity baseActivity, boolean isMarginStatusBar
, boolean isMarginNavigationBar, int statusBarColor, int navigationBarColor, boolean isDarkStatusBarIcon) {
try {
Window window = baseActivity.getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
//4.4版本及以上 5.0版本及以下
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (isMarginStatusBar && isMarginNavigationBar) {
//5.0版本及以上
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
LightStatusBarUtils.setLightStatusBar(baseActivity, isMarginStatusBar
, isMarginNavigationBar
, statusBarColor == Color.TRANSPARENT
, isDarkStatusBarIcon);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else if (!isMarginStatusBar && !isMarginNavigationBar) {
window.requestFeature(Window.FEATURE_NO_TITLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
LightStatusBarUtils.setLightStatusBar(baseActivity, isMarginStatusBar
, isMarginNavigationBar
, statusBarColor == Color.TRANSPARENT
, isDarkStatusBarIcon);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else if (!isMarginStatusBar && isMarginNavigationBar) {
window.requestFeature(Window.FEATURE_NO_TITLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
LightStatusBarUtils.setLightStatusBar(baseActivity, isMarginStatusBar
, isMarginNavigationBar
, statusBarColor == Color.TRANSPARENT
, isDarkStatusBarIcon);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else {
//留出来状态栏 不留出来导航栏 没找到办法。。
return;
}
window.setStatusBarColor(statusBarColor);
window.setNavigationBarColor(navigationBarColor);
}
} catch (Exception e) {
}
}
/**
* @param baseActivity
* @param statusBarColor 状态栏的颜色
* @param navigationBarColor 导航栏的颜色
*/
public static void immersiveAboveAPI23(AppCompatActivity baseActivity, boolean isMarginStatusBar
, boolean isMarginNavigationBar, int statusBarColor, int navigationBarColor, boolean isDarkStatusBarIcon) {
try {
Window window = baseActivity.getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
//4.4版本及以上 5.0版本及以下
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (isMarginStatusBar && isMarginNavigationBar) {
//5.0版本及以上
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
CropLightStatusBarUtils.setLightStatusBar(baseActivity, isMarginStatusBar
, isMarginNavigationBar
, statusBarColor == Color.TRANSPARENT
, isDarkStatusBarIcon);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else if (!isMarginStatusBar && !isMarginNavigationBar) {
window.requestFeature(Window.FEATURE_NO_TITLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
CropLightStatusBarUtils.setLightStatusBar(baseActivity, isMarginStatusBar
, isMarginNavigationBar
, statusBarColor == Color.TRANSPARENT
, isDarkStatusBarIcon);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else if (!isMarginStatusBar && isMarginNavigationBar) {
window.requestFeature(Window.FEATURE_NO_TITLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
CropLightStatusBarUtils.setLightStatusBar(baseActivity, isMarginStatusBar
, isMarginNavigationBar
, statusBarColor == Color.TRANSPARENT
, isDarkStatusBarIcon);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else {
//留出来状态栏 不留出来导航栏 没找到办法。。
return;
}
window.setStatusBarColor(statusBarColor);
window.setNavigationBarColor(navigationBarColor);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static final void requestFullscreen(final Activity pActivity) {
final Window window = pActivity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
window.requestFeature(Window.FEATURE_NO_TITLE);
}
@Override
public void onCreate(Bundle sis) {
super.onCreate(sis);
final Window window = getWindow();
window.requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_sample_list);
final ScrollableLayout scrollableLayout = findView(R.id.scrollable_layout);
final RecyclerView recyclerView = findView(R.id.recycler_view);
final SampleHeaderView headerView = findView(R.id.header);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
scrollableLayout.setCanScrollVerticallyDelegate(new CanScrollVerticallyDelegate() {
@Override
public boolean canScrollVertically(int direction) {
return recyclerView.canScrollVertically(direction);
}
});
scrollableLayout.addOnScrollChangedListener(new SampleHeaderViewOnScrollChangedListener(headerView));
scrollableLayout.setOnFlingOverListener(new OnFlingOverListener() {
@Override
public void onFlingOver(int y, long duration) {
recyclerView.smoothScrollBy(0, y);
}
});
scrollableLayout.setOverScrollListener(new ZoomInHeaderOverScrollListener(headerView, recyclerView));
final ViewTypesAdapter<SampleListItem> adapter = ViewTypesAdapter.builder(SampleListItem.class)
.register(SampleListItem.class, new SampleListItemViewType())
.setHasStableIds(true)
.registerOnClickListener(new OnItemClickListener<SampleListItem, Holder>() {
@Override
public void onItemClick(SampleListItem item, Holder holder) {
// yeah, conditions...
if (ScrollableDialog.class.equals(item.sampleActivityClass())) {
final ScrollableDialog dialog = new ScrollableDialog();
dialog.show(getSupportFragmentManager(), "tag.Dialog");
} else {
final Intent intent = new Intent(SampleListActivity.this, item.sampleActivityClass());
startActivity(intent);
}
}
})
.build(this);
adapter.setItems(sampleItems(getApplicationContext()));
recyclerView.setAdapter(adapter);
}