下面列出了android.view.Window#findViewById ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void updateSoftInputWindowLayoutParameters() {
// Override layout parameters to expand {@link SoftInputWindow} to the entire screen.
// See {@link InputMethodService#setinputView(View)} and
// {@link SoftInputWindow#updateWidthHeight(WindowManager.LayoutParams)}.
final Window window = getWindow().getWindow();
ViewLayoutUtils.updateLayoutHeightOf(window, LayoutParams.MATCH_PARENT);
// This method may be called before {@link #setInputView(View)}.
if (mInputView != null) {
// In non-fullscreen mode, {@link InputView} and its parent inputArea should expand to
// the entire screen and be placed at the bottom of {@link SoftInputWindow}.
// In fullscreen mode, these shouldn't expand to the entire screen and should be
// coexistent with {@link #mExtractedArea} above.
// See {@link InputMethodService#setInputView(View) and
// com.android.internal.R.layout.input_method.xml.
final int layoutHeight = isFullscreenMode()
? LayoutParams.WRAP_CONTENT : LayoutParams.MATCH_PARENT;
final View inputArea = window.findViewById(android.R.id.inputArea);
ViewLayoutUtils.updateLayoutHeightOf(inputArea, layoutHeight);
ViewLayoutUtils.updateLayoutGravityOf(inputArea, Gravity.BOTTOM);
ViewLayoutUtils.updateLayoutHeightOf(mInputView, layoutHeight);
}
}
/**
* set StatusBarColor
* <p>
* 1. set Window Flag : WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
* 2. removeFakeStatusBarViewIfExist
* 3. addFakeStatusBarView
* 4. addMarginTopToContentChild
* 5. cancel ContentChild's fitsSystemWindow
*/
static void setStatusBarColor(Activity activity, int statusColor) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
View mContentChild = mContentView.getChildAt(0);
int statusBarHeight = getStatusBarHeight(activity);
removeFakeStatusBarViewIfExist(activity);
addFakeStatusBarView(activity, statusColor, statusBarHeight);
addMarginTopToContentChild(mContentChild, statusBarHeight);
if (mContentChild != null) {
ViewCompat.setFitsSystemWindows(mContentChild, false);
}
}
private void updateSoftInputWindowLayoutParameters() {
// Override layout parameters to expand {@link SoftInputWindow} to the entire screen.
// See {@link InputMethodService#setinputView(View)} and
// {@link SoftInputWindow#updateWidthHeight(WindowManager.LayoutParams)}.
final Window window = getWindow().getWindow();
ViewLayoutUtils.updateLayoutHeightOf(window, LayoutParams.MATCH_PARENT);
// This method may be called before {@link #setInputView(View)}.
if (mInputView != null) {
// In non-fullscreen mode, {@link InputView} and its parent inputArea should expand to
// the entire screen and be placed at the bottom of {@link SoftInputWindow}.
// In fullscreen mode, these shouldn't expand to the entire screen and should be
// coexistent with {@link #mExtractedArea} above.
// See {@link InputMethodService#setInputView(View) and
// com.android.internal.R.layout.input_method.xml.
final int layoutHeight = isFullscreenMode()
? LayoutParams.WRAP_CONTENT : LayoutParams.MATCH_PARENT;
final View inputArea = window.findViewById(android.R.id.inputArea);
ViewLayoutUtils.updateLayoutHeightOf(inputArea, layoutHeight);
ViewLayoutUtils.updateLayoutGravityOf(inputArea, Gravity.BOTTOM);
ViewLayoutUtils.updateLayoutHeightOf(mInputView, layoutHeight);
}
}
private static View applyStatusBarColor(final Window window,
final int color,
boolean isDecor) {
ViewGroup parent = isDecor ?
(ViewGroup) window.getDecorView() :
(ViewGroup) window.findViewById(android.R.id.content);
View fakeStatusBarView = parent.findViewWithTag(TAG_STATUS_BAR);
if (fakeStatusBarView != null) {
if (fakeStatusBarView.getVisibility() == View.GONE) {
fakeStatusBarView.setVisibility(View.VISIBLE);
}
fakeStatusBarView.setBackgroundColor(color);
} else {
fakeStatusBarView = createStatusBarView(window.getContext(), color);
parent.addView(fakeStatusBarView);
}
return fakeStatusBarView;
}
public static int getStatusBarHeight(Activity activity) {
int result = 0;
Rect rect = new Rect();
Window window = activity.getWindow();
if (window != null) {
window.getDecorView().getWindowVisibleDisplayFrame(rect);
android.view.View v = window.findViewById(Window.ID_ANDROID_CONTENT);
android.view.Display display = ((android.view.WindowManager) activity.getSystemService(activity.WINDOW_SERVICE)).getDefaultDisplay();
//return result title bar height
int result1 = display.getHeight() - v.getBottom() + rect.top;
int result2 = display.getHeight() - v.getBottom();
int result3 = v.getTop() - rect.top;
int result4 = display.getHeight() - v.getHeight();
Log.e("StatusBarHeight==", "result1== " + result1 +" result2 = " + result2 + "result3=" + result3 + "result4=" +result4 ) ;
}
return result;
}
static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
Window window = activity.getWindow();
//添加Flag把状态栏设为可绘制模式
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (hideStatusBarBackground) {
//如果为全透明模式,取消设置Window半透明的Flag
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//设置状态栏为透明
window.setStatusBarColor(Color.TRANSPARENT);
//设置window的状态栏不可见
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View
.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
} else {
//如果为半透明模式,添加设置Window半透明的Flag
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//设置系统状态栏处于可见状态
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
//view不根据系统窗口来调整自己的布局
ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
ViewCompat.setFitsSystemWindows(mChildView, false);
ViewCompat.requestApplyInsets(mChildView);
}
}
public void tianjia_click(View v) {
dialog = new Dialog(this, R.style.Transparent);
dialog.setContentView(R.layout.zdy_style);
Window dialogWindow = dialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
WindowManager wm = (WindowManager) FeedbackActivity.this.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
lp.x = 0;
lp.y = 0;
lp.width = width;
lp.height = height;
lp.alpha = 0.7f;
TextView textView1_quxiao = (TextView) dialogWindow.findViewById(R.id.textView1_quxiao);
textView1_quxiao.setOnClickListener(this);
TextView textView2_paizhao = (TextView) dialogWindow.findViewById(R.id.textView2_paizhao);
textView2_paizhao.setOnClickListener(this);
mImageView = (ImageView) dialogWindow.findViewById(R.id.imageView3_phone);
dialog.show();
}
public void tianjia_click(View v) {
dialog = new Dialog(this, R.style.Transparent);
dialog.setContentView(R.layout.zdy_style);
Window dialogWindow = dialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
WindowManager wm = (WindowManager) FeedbackActivity.this.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
lp.x = 0;
lp.y = 0;
lp.width = width;
lp.height = height;
lp.alpha = 0.7f;
TextView textView1_quxiao = (TextView) dialogWindow.findViewById(R.id.textView1_quxiao);
textView1_quxiao.setOnClickListener(this);
TextView textView2_paizhao = (TextView) dialogWindow.findViewById(R.id.textView2_paizhao);
textView2_paizhao.setOnClickListener(this);
mImageView = (ImageView) dialogWindow.findViewById(R.id.imageView3_phone);
dialog.show();
}
private static View applyStatusBarColor(final Window window,
final int color,
boolean isDecor) {
ViewGroup parent = isDecor ?
(ViewGroup) window.getDecorView() :
(ViewGroup) window.findViewById(android.R.id.content);
View fakeStatusBarView = parent.findViewWithTag(TAG_STATUS_BAR);
if (fakeStatusBarView != null) {
if (fakeStatusBarView.getVisibility() == View.GONE) {
fakeStatusBarView.setVisibility(View.VISIBLE);
}
fakeStatusBarView.setBackgroundColor(color);
} else {
fakeStatusBarView = createStatusBarView(window.getContext(), color);
parent.addView(fakeStatusBarView);
}
return fakeStatusBarView;
}
/**
* translucentStatusBar(full-screen)
* <p>
* 1. set Flags to full-screen
* 2. set FitsSystemWindows to false
*
* @param hideStatusBarBackground hide statusBar's shadow
*/
static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (hideStatusBarBackground) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(Color.TRANSPARENT);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
ViewCompat.setFitsSystemWindows(mChildView, false);
ViewCompat.requestApplyInsets(mChildView);
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static void setStatusBarColorLollipop(Activity activity, int statusColor) {
Window window = activity.getWindow();
//取消状态栏透明
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//添加Flag把状态栏设为可绘制模式
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//设置状态栏颜色
window.setStatusBarColor(statusColor);
//设置系统状态栏处于可见状态
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
//让view不根据系统窗口来调整自己的布局
ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
mChildView.setFitsSystemWindows(false);
ViewCompat.requestApplyInsets(mChildView);
}
}
private void updateSoftInputWindowLayoutParameters() {
// Override layout parameters to expand {@link SoftInputWindow} to the entire screen.
// See {@link InputMethodService#setinputView(View)} and
// {@link SoftInputWindow#updateWidthHeight(WindowManager.LayoutParams)}.
final Window window = getWindow().getWindow();
ViewLayoutUtils.updateLayoutHeightOf(window, LayoutParams.MATCH_PARENT);
// This method may be called before {@link #setInputView(View)}.
if (mInputView != null) {
// In non-fullscreen mode, {@link InputView} and its parent inputArea should expand to
// the entire screen and be placed at the bottom of {@link SoftInputWindow}.
// In fullscreen mode, these shouldn't expand to the entire screen and should be
// coexistent with {@link #mExtractedArea} above.
// See {@link InputMethodService#setInputView(View) and
// com.android.internal.R.layout.input_method.xml.
final int layoutHeight = isFullscreenMode()
? LayoutParams.WRAP_CONTENT : LayoutParams.MATCH_PARENT;
final View inputArea = window.findViewById(android.R.id.inputArea);
ViewLayoutUtils.updateLayoutHeightOf(inputArea, layoutHeight);
ViewLayoutUtils.updateLayoutGravityOf(inputArea, Gravity.BOTTOM);
ViewLayoutUtils.updateLayoutHeightOf(mInputView, layoutHeight);
}
}
/**
* 应用 StatusBar View
* @param window {@link Window}
* @param color 背景颜色
* @param isDecor 是否添加在 DecorView 上
* @return StatusBar View
*/
private static View applyStatusBarColor(final Window window, final int color, final boolean isDecor) {
if (window == null) return null;
ViewGroup parent = isDecor ? (ViewGroup) window.getDecorView() : (ViewGroup) window.findViewById(android.R.id.content);
View fakeStatusBarView = parent.findViewWithTag(TAG_STATUS_BAR);
if (fakeStatusBarView != null) {
if (fakeStatusBarView.getVisibility() == View.GONE) {
fakeStatusBarView.setVisibility(View.VISIBLE);
}
fakeStatusBarView.setBackgroundColor(color);
} else {
fakeStatusBarView = createStatusBarView(window.getContext(), color);
parent.addView(fakeStatusBarView);
}
return fakeStatusBarView;
}
@Override
protected void onInitView(@NonNull Window window) {
window.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
mStorageList = (RecyclerView) window.findViewById(R.id.list);
mStorageList.setLayoutManager(new LinearLayoutManager(getContext()));
List<StorageHacker.StorageInfo> empty = new ArrayList<>(6);
mAdapter = new PerformanceViewAdapter(getContext(), empty);
mStorageList.setAdapter(mAdapter);
}
public void setup(Window window,
@IdRes int textButtonId, @IdRes int textSpaceId,
@IdRes int iconButtonId) {
textButton = (Button) window.findViewById(textButtonId);
textSpace = (Space) window.findViewById(textSpaceId);
iconButton = (FloatingActionButton) window.findViewById(iconButtonId);
}
static void setStatusBarColor(Activity activity, int statusColor) {
Window window = activity.getWindow();
//设置Window为全透明
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
//获取父布局
View mContentChild = mContentView.getChildAt(0);
//获取状态栏高度
int statusBarHeight = getStatusBarHeight(activity);
//如果已经存在假状态栏则移除,防止重复添加
removeFakeStatusBarViewIfExist(activity);
//添加一个View来作为状态栏的填充
addFakeStatusBarView(activity, statusColor, statusBarHeight);
//设置子控件到状态栏的间距
addMarginTopToContentChild(mContentChild, statusBarHeight);
//不预留系统栏位置
if (mContentChild != null) {
ViewCompat.setFitsSystemWindows(mContentChild, false);
}
//如果在Activity中使用了ActionBar则需要再将布局与状态栏的高度跳高一个ActionBar的高度,否则内容会被ActionBar遮挡
int action_bar_id = activity.getResources().getIdentifier("action_bar", "id", activity.getPackageName());
View view = activity.findViewById(action_bar_id);
if (view != null) {
TypedValue typedValue = new TypedValue();
if (activity.getTheme().resolveAttribute(R.attr.actionBarSize, typedValue, true)) {
int actionBarHeight = TypedValue.complexToDimensionPixelSize(typedValue.data, activity.getResources()
.getDisplayMetrics());
Eyes.setContentTopPadding(activity, actionBarHeight);
}
}
}
public static void resetActionBarContainerTopMargin(Window window) {
View contentView = window.findViewById(android.R.id.content);
ViewGroup group = (ViewGroup) contentView.getParent();
if (group.getChildCount() > 1) {
View view = group.getChildAt(1);
internalResetActionBarContainer(view);
}
}
@Override
public void onStart() {
super.onStart();
// Need to do this here, since getDialog() returns null in for example onBindDialogView()
final Resources res = getContext().getResources();
final Window window = getDialog().getWindow();
Button button1 = window.findViewById(android.R.id.button1);
Button button2 = window.findViewById(android.R.id.button2);
button1.setTextColor(res.getColor(R.color.black));
button2.setTextColor(res.getColor(R.color.black));
}
/**
* Show alertDialog
*
* @param mActivity activity instance
* @param content show content
* @param btnContent btn content
*/
public void showDialog(Activity mActivity, String content, String btnContent) {
if (mActivity == null) {
return;
}
if (alertDialog == null) {
alertDialog = new AlertDialog.Builder(mActivity, R.style.myCorDialog).create();
}
if (!alertDialog.isShowing()) {
alertDialog.show();
}
alertDialog.setCanceledOnTouchOutside(false);
Window window = alertDialog.getWindow();
if (window != null) {
window.setContentView(R.layout.common_alert_dialog);
TextView contentTv = (TextView) window.findViewById(R.id.dialog_content);
contentTv.setText(content);
Button mOkBtn = (Button) window.findViewById(R.id.dialog_btn);
mOkBtn.setText(btnContent);
mOkBtn.setOnClickListener(v -> {
if (alertDialog != null) {
alertDialog.cancel();
alertDialog = null;
}
});
View btnDivider = window.findViewById(R.id.dialog_btn_divider);
btnDivider.setVisibility(View.GONE);
Button mNeBtn = (Button) window.findViewById(R.id.dialog_negative_btn);
mNeBtn.setVisibility(View.GONE);
}
}
/**
* Activity
* @param view
* @param viewId
* @param <T>
* @return
*/
public static <T> T $(Window view,int viewId) {
return (T) view.findViewById(viewId);
}