下面列出了android.widget.PopupWindow#setClippingEnabled ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void apply(PopupWindow mPopupWindow) {
mPopupWindow.setClippingEnabled(this.mClippEnable);
if(this.mIgnoreCheekPress) {
mPopupWindow.setIgnoreCheekPress();
}
if(this.mInputMode != -1) {
mPopupWindow.setInputMethodMode(this.mInputMode);
}
if(this.mSoftInputMode != -1) {
mPopupWindow.setSoftInputMode(this.mSoftInputMode);
}
if(this.mOnDismissListener != null) {
mPopupWindow.setOnDismissListener(this.mOnDismissListener);
}
if(this.mOnTouchListener != null) {
mPopupWindow.setTouchInterceptor(this.mOnTouchListener);
}
mPopupWindow.setTouchable(this.mTouchable);
}
private void layoutAndShowPopupWindow(Key key, int xPosition) {
popupWindow = new PopupWindow(popupView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setClippingEnabled(false);
int[] location = new int[2];
key.getLocationInWindow(location);
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
popupView.measure(measureSpec, measureSpec);
int popupWidth = popupView.getMeasuredWidth();
int spaceAboveKey = key.getHeight() / 4;
int x = xPosition - popupWidth / popupView.getChildCount() / 2;
int screenWidth = getScreenWidth();
if (x < 0) {
x = 0;
} else if (x + popupWidth > screenWidth) {
x = screenWidth - popupWidth;
}
int y = location[1] - popupView.getMeasuredHeight() - spaceAboveKey;
popupWindow.showAtLocation(key, Gravity.NO_GRAVITY, x, y);
//popupWindow.showAsDropDown(key, 0, -500);
}
/**
* 弹出popupWindow
* Show popup.
*
* @param gravity the gravity
* @param width the width
* @param height the height
* @param animationStyle the animation style
*/
private void showPopup(int gravity, int width, int height, int animationStyle) {
mPopupView = LayoutInflater.from(mActivity).inflate(R.layout.popup_demo, null);
mPopupWindow = new PopupWindow(mPopupView, width, height);
//以下属性响应空白处消失和实体按键返回消失popup
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setFocusable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//沉浸式模式下,以下两个属性并不起作用
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//重点,此方法可以让布局延伸到状态栏和导航栏
mPopupWindow.setClippingEnabled(false);
//设置动画
mPopupWindow.setAnimationStyle(animationStyle);
//弹出
mPopupWindow.showAtLocation(getWindow().getDecorView(), gravity, 0, 0);
//弹出后背景alpha值
backgroundAlpha(0.5f);
//消失后恢复背景alpha值
mPopupWindow.setOnDismissListener(() -> backgroundAlpha(1f));
//适配弹出popup后布局被状态栏和导航栏遮挡问题
updatePopupView();
}
/**
* 添加一些属性设置
* @param popupWindow
*/
private void apply(PopupWindow popupWindow){
popupWindow.setClippingEnabled(mClippEnable);
if(mIgnoreCheekPress){
popupWindow.setIgnoreCheekPress();
}
if(mInputMode!=-1){
popupWindow.setInputMethodMode(mInputMode);
}
if(mSoftInputMode!=-1){
popupWindow.setSoftInputMode(mSoftInputMode);
}
if(mOnDismissListener!=null){
popupWindow.setOnDismissListener(mOnDismissListener);
}
if(mOnTouchListener!=null){
popupWindow.setTouchInterceptor(mOnTouchListener);
}
popupWindow.setTouchable(mTouchable);
}
public PastePopupMenu() {
mContainer = new PopupWindow(mContext, null,
android.R.attr.textSelectHandleWindowStyle);
mContainer.setSplitTouchEnabled(true);
mContainer.setClippingEnabled(false);
mContainer.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mContainer.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
final int[] POPUP_LAYOUT_ATTRS = {
android.R.attr.textEditPasteWindowLayout,
android.R.attr.textEditNoPasteWindowLayout,
android.R.attr.textEditSidePasteWindowLayout,
android.R.attr.textEditSideNoPasteWindowLayout,
};
mPasteViews = new View[POPUP_LAYOUT_ATTRS.length];
mPasteViewLayouts = new int[POPUP_LAYOUT_ATTRS.length];
TypedArray attrs = mContext.obtainStyledAttributes(POPUP_LAYOUT_ATTRS);
for (int i = 0; i < attrs.length(); ++i) {
mPasteViewLayouts[i] = attrs.getResourceId(attrs.getIndex(i), 0);
}
attrs.recycle();
}
public PastePopupMenu() {
mContainer = new PopupWindow(mContext, null,
android.R.attr.textSelectHandleWindowStyle);
mContainer.setSplitTouchEnabled(true);
mContainer.setClippingEnabled(false);
mContainer.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mContainer.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
final int[] POPUP_LAYOUT_ATTRS = {
android.R.attr.textEditPasteWindowLayout,
android.R.attr.textEditNoPasteWindowLayout,
android.R.attr.textEditSidePasteWindowLayout,
android.R.attr.textEditSideNoPasteWindowLayout,
};
mPasteViews = new View[POPUP_LAYOUT_ATTRS.length];
mPasteViewLayouts = new int[POPUP_LAYOUT_ATTRS.length];
TypedArray attrs = mContext.obtainStyledAttributes(POPUP_LAYOUT_ATTRS);
for (int i = 0; i < attrs.length(); ++i) {
mPasteViewLayouts[i] = attrs.getResourceId(attrs.getIndex(i), 0);
}
attrs.recycle();
}
public TextSelectionHandlePopup(Context ctx, boolean rightHandle) {
mView = new TextSelectionHandleView(ctx, rightHandle);
mWindow = new PopupWindow(mView.getContext(), null, android.R.attr.textSelectHandleWindowStyle);
mWindow.setSplitTouchEnabled(true);
mWindow.setClippingEnabled(false);
mWindow.setContentView(mView);
mWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
mView.measure(0, 0);
}
/**
* Constructor to create a new reactions popup with an anchor view.
*
* @param context Context the reactions popup is running in, through which it
* can access the current theme, resources, etc.
* @param anchor Anchor view for this popup. The popup will appear on top of
*/
public ReactionsPopup(@NonNull Context context, @NonNull View anchor) {
this.anchorView = anchor;
popup = new PopupWindow();
popup.setWindowLayoutMode(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
reactionsView = new ReactionsView(context);
reactionsView.setVisibility(View.VISIBLE);
popup.setContentView(reactionsView);
popup.setFocusable(true);
popup.setClippingEnabled(true);
popup.setBackgroundDrawable(
ContextCompat.getDrawable(context, R.drawable.rounded_corners_reactions));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
popup.setElevation(10);
}
reactionsView.setCallback(reactionType -> {
if (reactionClickListener != null) {
reactionClickListener.onReactionItemClick(reactionType);
}
});
popup.setOnDismissListener(() -> {
if (onDismissListener != null) {
onDismissListener.onDismiss(reactionsView);
}
});
}
public LegacyPastePopupMenu(
Context context, View parent, final PastePopupMenuDelegate delegate) {
mParent = parent;
mDelegate = delegate;
mContext = context;
mContainer = new PopupWindow(mContext, null, android.R.attr.textSelectHandleWindowStyle);
mContainer.setSplitTouchEnabled(true);
mContainer.setClippingEnabled(false);
mContainer.setAnimationStyle(0);
mContainer.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mContainer.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
final int[] popupLayoutAttrs = {
android.R.attr.textEditPasteWindowLayout,
};
TypedArray attrs = mContext.getTheme().obtainStyledAttributes(popupLayoutAttrs);
mPasteViewLayout = attrs.getResourceId(attrs.getIndex(0), 0);
attrs.recycle();
// Convert line offset dips to pixels.
mLineOffsetY = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 5.0f, mContext.getResources().getDisplayMetrics());
mWidthOffsetX = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 30.0f, mContext.getResources().getDisplayMetrics());
final int statusBarHeightResourceId =
mContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (statusBarHeightResourceId > 0) {
mStatusBarHeight =
mContext.getResources().getDimensionPixelSize(statusBarHeightResourceId);
}
}
private static PopupWindow createPopupWindow(ViewGroup content) {
ViewGroup popupContentHolder = new LinearLayout(content.getContext());
PopupWindow popupWindow = new PopupWindow(popupContentHolder);
popupWindow.setClippingEnabled(false);
popupWindow.setAnimationStyle(0);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
content.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
popupContentHolder.addView(content);
return popupWindow;
}
private static PopupWindow createPopupWindow(ViewGroup content) {
ViewGroup popupContentHolder = new LinearLayout(content.getContext());
PopupWindow popupWindow = new PopupWindow(popupContentHolder);
popupWindow.setClippingEnabled(false);
popupWindow.setAnimationStyle(0);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
content.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
popupContentHolder.addView(content);
return popupWindow;
}
HandleView(CursorController controller, int pos, View parent,
PositionObserver parentPositionObserver) {
super(parent.getContext());
Context context = parent.getContext();
mParent = parent;
mController = controller;
mContainer = new PopupWindow(context, null, android.R.attr.textSelectHandleWindowStyle);
mContainer.setSplitTouchEnabled(true);
mContainer.setClippingEnabled(false);
TypedArray a = context.obtainStyledAttributes(TEXT_VIEW_HANDLE_ATTRS);
mTextSelectHandleLeftRes = a.getResourceId(a.getIndex(LEFT), 0);
mTextSelectHandleRes = a.getResourceId(a.getIndex(CENTER), 0);
mTextSelectHandleRightRes = a.getResourceId(a.getIndex(RIGHT), 0);
a.recycle();
setOrientation(pos);
// Convert line offset dips to pixels.
mLineOffsetY = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
LINE_OFFSET_Y_DIP, context.getResources().getDisplayMetrics());
mAlpha = 1.f;
mParentPositionListener = new PositionObserver.Listener() {
@Override
public void onPositionChanged(int x, int y) {
updateParentPosition(x, y);
}
};
mParentPositionObserver = parentPositionObserver;
}
HandleView(CursorController controller, int pos, View parent,
PositionObserver parentPositionObserver) {
super(parent.getContext());
Context context = parent.getContext();
mParent = parent;
mController = controller;
mContainer = new PopupWindow(context, null, android.R.attr.textSelectHandleWindowStyle);
mContainer.setSplitTouchEnabled(true);
mContainer.setClippingEnabled(false);
TypedArray a = context.obtainStyledAttributes(TEXT_VIEW_HANDLE_ATTRS);
mTextSelectHandleLeftRes = a.getResourceId(a.getIndex(LEFT), 0);
mTextSelectHandleRes = a.getResourceId(a.getIndex(CENTER), 0);
mTextSelectHandleRightRes = a.getResourceId(a.getIndex(RIGHT), 0);
a.recycle();
setOrientation(pos);
// Convert line offset dips to pixels.
mLineOffsetY = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
LINE_OFFSET_Y_DIP, context.getResources().getDisplayMetrics());
mAlpha = 1.f;
mParentPositionListener = new PositionObserver.Listener() {
@Override
public void onPositionChanged(int x, int y) {
updateParentPosition(x, y);
}
};
mParentPositionObserver = parentPositionObserver;
}
public LatinKeyboardView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO(klausw): migrate attribute styles to LatinKeyboardView?
TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.LatinKeyboardBaseView, defStyle, R.style.LatinKeyboardBaseView);
LayoutInflater inflate =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int previewLayout = 0;
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.LatinKeyboardBaseView_keyPreviewLayout:
previewLayout = a.getResourceId(attr, 0);
if (previewLayout == R.layout.null_layout) previewLayout = 0;
break;
case R.styleable.LatinKeyboardBaseView_keyPreviewOffset:
mPreviewOffset = a.getDimensionPixelOffset(attr, 0);
break;
case R.styleable.LatinKeyboardBaseView_keyPreviewHeight:
mPreviewHeight = a.getDimensionPixelSize(attr, 80);
break;
case R.styleable.LatinKeyboardBaseView_popupLayout:
mPopupLayout = a.getResourceId(attr, 0);
if (mPopupLayout == R.layout.null_layout) mPopupLayout = 0;
break;
}
}
final Resources res = getResources();
// If true, popups are forced to remain inside the keyboard area. If false,
// they can extend above it. Enable clipping just for Android P since drawing
// outside the keyboard area doesn't work on that version.
boolean clippingEnabled = (Build.VERSION.SDK_INT >= 28 /* Build.VERSION_CODES.P */);
if (previewLayout != 0) {
mPreviewPopup = new PopupWindow(context);
if (!isInEditMode())
Log.i(TAG, "new mPreviewPopup " + mPreviewPopup + " from " + this);
mPreviewText = (TextView) inflate.inflate(previewLayout, null);
mPreviewTextSizeLarge = (int) res.getDimension(R.dimen.key_preview_text_size_large);
mPreviewPopup.setContentView(mPreviewText);
mPreviewPopup.setBackgroundDrawable(null);
mPreviewPopup.setTouchable(false);
mPreviewPopup.setAnimationStyle(R.style.KeyPreviewAnimation);
mPreviewPopup.setClippingEnabled(clippingEnabled);
} else {
mShowPreview = false;
}
if (mPopupLayout != 0) {
mMiniKeyboardParent = this;
mMiniKeyboardPopup = new PopupWindow(context);
if (!isInEditMode())
Log.i(TAG, "new mMiniKeyboardPopup " + mMiniKeyboardPopup + " from " + this);
mMiniKeyboardPopup.setBackgroundDrawable(null);
mMiniKeyboardPopup.setAnimationStyle(R.style.MiniKeyboardAnimation);
mMiniKeyboardPopup.setClippingEnabled(clippingEnabled);
mMiniKeyboardVisible = false;
}
}
/**
* Construct a CandidateView for showing suggested words for completion.
* @param context
* @param attrs
*/
public CandidateView(Context context, AttributeSet attrs) {
super(context, attrs);
mSelectionHighlight = context.getResources().getDrawable(
R.drawable.list_selector_background_pressed);
LayoutInflater inflate =
(LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resources res = context.getResources();
mPreviewPopup = new PopupWindow(context);
mPreviewText = (TextView) inflate.inflate(R.layout.candidate_preview, null);
mPreviewPopup.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mPreviewPopup.setContentView(mPreviewText);
mPreviewPopup.setBackgroundDrawable(null);
mPreviewPopup.setAnimationStyle(R.style.KeyPreviewAnimation);
// Enable clipping for Android P, keep disabled for older versions.
boolean clippingEnabled = (Build.VERSION.SDK_INT >= 28 /* Build.VERSION_CODES.P */);
mPreviewPopup.setClippingEnabled(clippingEnabled);
mColorNormal = res.getColor(R.color.candidate_normal);
mColorRecommended = res.getColor(R.color.candidate_recommended);
mColorOther = res.getColor(R.color.candidate_other);
mDivider = res.getDrawable(R.drawable.keyboard_suggest_strip_divider);
mAddToDictionaryHint = res.getString(R.string.hint_add_to_dictionary);
mPaint = new Paint();
mPaint.setColor(mColorNormal);
mPaint.setAntiAlias(true);
mPaint.setTextSize(mPreviewText.getTextSize() * LatinIME.sKeyboardSettings.candidateScalePref);
mPaint.setStrokeWidth(0);
mPaint.setTextAlign(Align.CENTER);
mDescent = (int) mPaint.descent();
mMinTouchableWidth = (int)res.getDimension(R.dimen.candidate_min_touchable_width);
mGestureDetector = new GestureDetector(
new CandidateStripGestureListener(mMinTouchableWidth));
setWillNotDraw(false);
setHorizontalScrollBarEnabled(false);
setVerticalScrollBarEnabled(false);
scrollTo(0, getScrollY());
}