下面列出了android.view.View#getRootView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void focusInLocked(View view) {
if (DEBUG) Log.v(TAG, "focusIn: " + dumpViewInfo(view));
if (view != null && view.isTemporarilyDetached()) {
// This is a request from a view that is temporarily detached from a window.
if (DEBUG) Log.v(TAG, "Temporarily detached view, ignoring");
return;
}
if (mCurRootView != view.getRootView()) {
// This is a request from a window that isn't in the window with
// IME focus, so ignore it.
if (DEBUG) Log.v(TAG, "Not IME target window, ignoring");
return;
}
mNextServedView = view;
scheduleCheckFocusLocked(view);
}
/** @hide */
protected void attachToAnchor(View anchor, int xoff, int yoff, int gravity) {
detachFromAnchor();
final ViewTreeObserver vto = anchor.getViewTreeObserver();
if (vto != null) {
vto.addOnScrollChangedListener(mOnScrollChangedListener);
}
anchor.addOnAttachStateChangeListener(mOnAnchorDetachedListener);
final View anchorRoot = anchor.getRootView();
anchorRoot.addOnAttachStateChangeListener(mOnAnchorRootDetachedListener);
anchorRoot.addOnLayoutChangeListener(mOnLayoutChangeListener);
mAnchor = new WeakReference<>(anchor);
mAnchorRoot = new WeakReference<>(anchorRoot);
mIsAnchorRootAttached = anchorRoot.isAttachedToWindow();
mParentRootView = mAnchorRoot;
mAnchorXoff = xoff;
mAnchorYoff = yoff;
mAnchoredGravity = gravity;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (Preferences.SHOW_COMPASS_NOTE.get()) {
final ViewGroup root = (ViewGroup) (view.getRootView());
final View toast = LayoutInflater.from(getActivity()).inflate(R.layout.compass_toast_menu, root, false);
root.addView(toast);
toast.setOnClickListener(v -> root.removeView(toast));
toast.postDelayed(() -> {
if (toast.getRootView() == root)
root.removeView(toast);
}, 10000);
}
}
/**
* Invoke this method to unregister a view hierarchy.
*
* @param view A view that belongs to the view hierarchy/window to unregister
*
* @see #addWindow(View, String)
*/
public void removeWindow(View view) {
View rootView;
mWindowsLock.writeLock().lock();
try {
rootView = view.getRootView();
mWindows.remove(rootView);
} finally {
mWindowsLock.writeLock().unlock();
}
mFocusLock.writeLock().lock();
try {
if (mFocusedWindow == rootView) {
mFocusedWindow = null;
}
} finally {
mFocusLock.writeLock().unlock();
}
fireWindowsChangedEvent();
}
/**
* Invoke this method to unregister a view hierarchy.
*
* @param view A view that belongs to the view hierarchy/window to unregister
*
* @see #addWindow(View, String)
*/
public void removeWindow(View view) {
View rootView;
mWindowsLock.writeLock().lock();
try {
rootView = view.getRootView();
mWindows.remove(rootView);
} finally {
mWindowsLock.writeLock().unlock();
}
mFocusLock.writeLock().lock();
try {
if (mFocusedWindow == rootView) {
mFocusedWindow = null;
}
} finally {
mFocusLock.writeLock().unlock();
}
fireWindowsChangedEvent();
}
/** Returns the content view that is the parent of the provided view. */
@Nullable
public static ViewGroup getContentView(@Nullable View view) {
if (view == null) {
return null;
}
View rootView = view.getRootView();
ViewGroup contentView = rootView.findViewById(android.R.id.content);
if (contentView != null) {
return contentView;
}
// Account for edge cases: Parent's parent can be null without ever having found
// android.R.id.content (e.g. if view is in an overlay during a transition).
// Additionally, sometimes parent's parent is neither a ViewGroup nor a View (e.g. if view
// is in a PopupWindow).
if (rootView != view && rootView instanceof ViewGroup) {
return (ViewGroup) rootView;
}
return null;
}
/**
* Constructs a {@link TextBubble} instance.
* @param context Context to draw resources from.
* @param rootView The {@link View} to use for size calculations and for display.
* @param stringId The id of the string resource for the text that should be shown.
* @param accessibilityStringId The id of the string resource of the accessibility text.
*/
public TextBubble(Context context, View rootView, @StringRes int stringId,
@StringRes int accessibilityStringId) {
mContext = context;
mRootView = rootView.getRootView();
mStringId = stringId;
mAccessibilityStringId = accessibilityStringId;
mPopupWindow = new PopupWindow(mContext);
mDrawable = new ArrowBubbleDrawable(context);
mHandler = new Handler();
mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setBackgroundDrawable(mDrawable);
mPopupWindow.setAnimationStyle(R.style.TextBubbleAnimation);
mPopupWindow.setTouchInterceptor(this);
mPopupWindow.setOnDismissListener(mDismissListener);
mMarginPx = context.getResources().getDimensionPixelSize(R.dimen.text_bubble_margin);
// Set predefined styles for the TextBubble.
mDrawable.setBubbleColor(
ApiCompatibilityUtils.getColor(mContext.getResources(), R.color.google_blue_500));
}
/**
* Invoke this method to unregister a view hierarchy.
*
* @param view A view that belongs to the view hierarchy/window to unregister
* @see #addWindow(View, String)
*/
public void removeWindow(View view) {
View rootView;
mWindowsLock.writeLock().lock();
try {
rootView = view.getRootView();
mWindows.remove(rootView);
} finally {
mWindowsLock.writeLock().unlock();
}
mFocusLock.writeLock().lock();
try {
if (mFocusedWindow == rootView) {
mFocusedWindow = null;
}
} finally {
mFocusLock.writeLock().unlock();
}
fireWindowsChangedEvent();
}
/**
* Invoke this method to change the currently focused window.
*
* @param view A view that belongs to the view hierarchy/window that has focus,
* or null to remove focus
*/
public void setFocusedWindow(View view) {
mFocusLock.writeLock().lock();
try {
mFocusedWindow = view == null ? null : view.getRootView();
} finally {
mFocusLock.writeLock().unlock();
}
fireFocusChangedEvent();
}
/**
* Invoke this method to change the currently focused window.
*
* @param view A view that belongs to the view hierarchy/window that has focus,
* or null to remove focus
*/
public void setFocusedWindow(View view) {
mFocusLock.writeLock().lock();
try {
mFocusedWindow = view == null ? null : view.getRootView();
} finally {
mFocusLock.writeLock().unlock();
}
fireFocusChangedEvent();
}
public static int getRelativeLeft(View myView)
{
if (myView.getParent() == myView.getRootView())
return myView.getLeft();
else
return myView.getLeft() + UIHelpers.getRelativeLeft((View) myView.getParent());
}
/**
* Invoke this method to change the currently focused window.
*
* @param view A view that belongs to the view hierarchy/window that has focus,
* or null to remove focus
*/
public void setFocusedWindow(View view) {
mFocusLock.writeLock().lock();
try {
mFocusedWindow = view == null ? null : view.getRootView();
} finally {
mFocusLock.writeLock().unlock();
}
fireFocusChangedEvent();
}
/**
* Takes screenshot of current screen
*
* @param view to be taken screenshot of
* @return bitmap of the screenshot
*/
private static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
/**
* Invoke this method to change the currently focused window.
*
* @param view A view that belongs to the view hierarchy/window that has focus,
* or null to remove focus
*/
public void setFocusedWindow(View view) {
mFocusLock.writeLock().lock();
try {
mFocusedWindow = view == null ? null : view.getRootView();
} finally {
mFocusLock.writeLock().unlock();
}
fireFocusChangedEvent();
}
/**
* Invoke this method to change the currently focused window.
*
* @param view A view that belongs to the view hierarchy/window that has focus,
* or null to remove focus
*/
public void setFocusedWindow(View view) {
mFocusLock.writeLock().lock();
try {
mFocusedWindow = view == null ? null : view.getRootView();
} finally {
mFocusLock.writeLock().unlock();
}
fireFocusChangedEvent();
}
FooterViewHolder(View itemView) {
super(itemView);
if (!Preferences.get(mContext).isShadowEnabled()) {
View shadow = itemView.findViewById(R.id.shadow);
shadow.setVisibility(View.GONE);
View root = shadow.getRootView();
root.setPadding(0, 0, 0, 0);
}
}
/**
* Invoke this method to change the currently focused window.
*
* @param view A view that belongs to the view hierarchy/window that has focus,
* or null to remove focus
*/
public void setFocusedWindow(View view) {
mFocusLock.writeLock().lock();
try {
mFocusedWindow = view == null ? null : view.getRootView();
} finally {
mFocusLock.writeLock().unlock();
}
fireFocusChangedEvent();
}
public static Dialog showPopupFromAnchor(final View anchor, int idLayout, boolean cancelable) {
try {
if (anchor == null || idLayout == 0) {
return null;
}
final Context context = anchor.getContext();
View rootView = anchor.getRootView();
Rect rectAnchor = new Rect();
int[] location = new int[2];
anchor.getLocationInWindow(location);
rectAnchor.set(location[0], location[1], location[0] + anchor.getWidth(), location[1] + anchor.getHeight());
Rect rectRoot = new Rect();
rootView.getLocationInWindow(location);
rectRoot.set(location[0], location[1], location[0] + rootView.getWidth(), location[1] + rootView.getHeight());
final Dialog dialog = new Dialog(context,
android.R.style.Theme_Translucent_NoTitleBar);
View dialogView = LayoutInflater.from(context).inflate(idLayout, (ViewGroup)anchor.getRootView(), false);
dialogView.measure(
View.MeasureSpec.makeMeasureSpec(rectRoot.width(), View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec((rectAnchor.top - rectRoot.top), View.MeasureSpec.AT_MOST));
dialog.setTitle(null);
dialog.setContentView(dialogView);
dialog.setCancelable(true);
// Setting dialogview
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.x = rectAnchor.left - PopupDialog.dpToPx(20, context);
wlp.y = rectAnchor.top - dialogView.getMeasuredHeight();
wlp.width = dialogView.getMeasuredWidth();
wlp.height = dialogView.getMeasuredHeight();
wlp.gravity = Gravity.TOP | Gravity.START;
wlp.dimAmount = 0.6f;
wlp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
if (cancelable) {
dialog.setCanceledOnTouchOutside(true);
}
View btnClose = dialogView.findViewById(R.id.btnClose);
if (btnClose != null) {
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
dialog.show();
return dialog;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static int getRelativeTop(View view) {
if (view.getParent() == view.getRootView())
return view.getTop();
else
return view.getTop() + getRelativeTop((View) view.getParent());
}