下面列出了android.view.Gravity#LEFT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@ReactProp(name = ViewProps.TEXT_ALIGN)
public void setTextAlign(@Nullable String textAlign) {
if (textAlign == null || "auto".equals(textAlign)) {
mTextAlign = Gravity.NO_GRAVITY;
} else if ("left".equals(textAlign)) {
mTextAlign = Gravity.LEFT;
} else if ("right".equals(textAlign)) {
mTextAlign = Gravity.RIGHT;
} else if ("center".equals(textAlign)) {
mTextAlign = Gravity.CENTER_HORIZONTAL;
} else if ("justify".equals(textAlign)) {
// Fallback gracefully for cross-platform compat instead of error
mTextAlign = Gravity.LEFT;
} else {
throw new JSApplicationIllegalArgumentException("Invalid textAlign: " + textAlign);
}
markUpdated();
}
/**
* HoverView add to FloatLayout
* @param position LEFT, CENTER, RIGHT
* */
public void makeHoverView(Position position) {
int gravity;
if (Position.LEFT.equals(position)) {
gravity = Gravity.BOTTOM|Gravity.LEFT;
} else if (Position.RIGHT.equals(position)) {
gravity = Gravity.BOTTOM|Gravity.RIGHT;
} else {
gravity = Gravity.BOTTOM|Gravity.CENTER;
}
FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
frameParams.gravity = gravity;
mFloatLayout.setLayoutParams(frameParams);
FrameLayout.LayoutParams wrapParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
wrapParams.gravity = gravity;
wrapParams.setMargins(MARGIN, MARGIN, MARGIN, MARGIN);
mFloatLayout.addView(getHoverView(), wrapParams);
mFloatLayout.setBackgroundColor(Color.argb(0, 255, 255, 255));
mRootView.addView(mFloatLayout);
switchHover();
}
public void addView(int width, int height, boolean isTouchable, @NonNull View view) {
// If this view is untouchable then add the corresponding flag, otherwise set to zero which
// won't have any effect on the OR'ing of flags.
int touchableFlag = isTouchable ? 0 : WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
int windowType = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
: WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
width,
height,
windowType,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | touchableFlag,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 0;
mWindowManager.addView(view, params);
}
/** 更新数据 */
public void notifyDataSetChanged() {
mTabsContainer.removeAllViews();
this.mTabCount = mTabEntitys.size();
View tabView;
for (int i = 0; i < mTabCount; i++) {
if (mIconGravity == Gravity.LEFT) {
tabView = View.inflate(mContext, R.layout.layout_tab_left, null);
} else if (mIconGravity == Gravity.RIGHT) {
tabView = View.inflate(mContext, R.layout.layout_tab_right, null);
} else if (mIconGravity == Gravity.BOTTOM) {
tabView = View.inflate(mContext, R.layout.layout_tab_bottom, null);
} else {
tabView = View.inflate(mContext, R.layout.layout_tab_top, null);
}
tabView.setTag(i);
addTab(i, tabView);
}
updateTabStyles();
}
/**
* 显示小悬浮窗
*/
public void showSmallFloatWin() {
if (DEBUG) {
LogX.d(TAG, SUB_TAG, "showSmallFloatWin:创建悬浮窗口");
}
if (smallView == null) {
smallView = new SmallFloatWindowView(Manager.getContext(), dm.density);
smallParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
FloatWindowUtils.getType(),
FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM, PixelFormat.TRANSLUCENT);
smallParams.gravity = Gravity.LEFT | Gravity.TOP;
smallParams.x = (int) xPosition;
smallParams.y = (int) (dm.heightPixels / 4 - yPosition);
smallView.setWindowsParams(smallParams);
smallView.setOnSmallCallback(this);
}
removeOldFloatWindow();
getWindowManager().addView(smallView, smallParams);
currentState = FloatWindowState.SMALL_WINDOW;
}
/**
* @return True, if adjustments were made that require the view to be invalidated.
*/
private boolean adjustBottomLines() {
// Bail out if we have a zero width; lines will be adjusted during next layout.
if (getWidth() == 0) {
return false;
}
int destBottomLines;
textPaint.setTextSize(bottomTextSize);
if (tempErrorText != null || helperText != null) {
Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ?
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
} else {
destBottomLines = minBottomLines;
}
if (bottomLines != destBottomLines) {
getBottomLinesAnimator(destBottomLines).start();
}
bottomLines = destBottomLines;
return true;
}
/**
* Returns the view gravity of the hot corner, or 0 if hot corner is disabled.
* @return A {@link android.view.Gravity} value, or 0 if disabled.
*/
public int getHotCornerGravity() {
String hc = getHotCorner();
if(ARRAY_HOT_CORNER_BOTTOM_LEFT.equals(hc)) {
return Gravity.LEFT | Gravity.BOTTOM;
} else if(ARRAY_HOT_CORNER_BOTTOM_RIGHT.equals(hc)) {
return Gravity.RIGHT | Gravity.BOTTOM;
} else if(ARRAY_HOT_CORNER_TOP_LEFT.equals(hc)) {
return Gravity.LEFT | Gravity.TOP;
} else if(ARRAY_HOT_CORNER_TOP_RIGHT.equals(hc)) {
return Gravity.RIGHT | Gravity.TOP;
}
return 0;
}
public void show(int x, int y, boolean top) {
if (top) {
ivArrowTop.setVisibility(View.VISIBLE);
ivArrowBottom.setVisibility(View.GONE);
mWindow.getAttributes().gravity = Gravity.LEFT + Gravity.TOP;
} else {
ivArrowBottom.setVisibility(View.VISIBLE);
ivArrowTop.setVisibility(View.GONE);
mWindow.getAttributes().gravity = Gravity.LEFT + Gravity.BOTTOM;
}
container.measure(0, 0);
int windowWidth = container.getMeasuredWidth();
int windowX = Math.max(x - windowWidth / 2, MinHorizontalMargin);
windowX = Math.max(windowX, MinHorizontalMargin);
windowX = Math.min(windowX, UIUtil.getScreenWidth()
- MinHorizontalMargin - windowWidth);
int arrowLeft = x - windowX - ArrowWidth / 2;
arrowLeft = Math.max(arrowLeft, HorizontalPadding);
arrowLeft = Math.min(arrowLeft, windowWidth - HorizontalPadding
- ArrowWidth);
if (top) {
lpArrowTop.leftMargin = arrowLeft;
} else {
lpArrowBottom.leftMargin = arrowLeft;
}
mWindow.getAttributes().x = windowX;
mWindow.getAttributes().y = y + Offset;
show();
}
public TabStrip(Context context) {
super(context);
setWillNotDraw(false);
setOrientation(LinearLayout.VERTICAL);
mIndicatorPaint = new Paint();
mIndicatorPaint.setAntiAlias(true);
mIndicatorGravity = mIndicatorGravity == 0 ? Gravity.LEFT : mIndicatorGravity;
mIndicatorRect = new RectF();
setIndicatorGravity();
}
private void init(Context mContext) {
this.mContext = mContext;
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
// 更新浮动窗口位置参数 靠边
DisplayMetrics dm = new DisplayMetrics();
// 获取屏幕信息
mWindowManager.getDefaultDisplay().getMetrics(dm);
mScreenWidth = dm.widthPixels;
mScreenHeight = dm.heightPixels;
Log.e("FloatView-----","宽"+mScreenWidth+"----"+"高"+mScreenHeight);
this.mWmParams = new WindowManager.LayoutParams();
// 设置window type
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mWmParams.type = WindowManager.LayoutParams.TYPE_TOAST;
} else {
mWmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
}
// 设置图片格式,效果为背景透明
mWmParams.format = PixelFormat.RGBA_8888;
// 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
mWmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 调整悬浮窗显示的停靠位置为左侧置�?
mWmParams.gravity = Gravity.LEFT | Gravity.TOP;
mScreenHeight = mWindowManager.getDefaultDisplay().getHeight();
// 以屏幕左上角为原点,设置x、y初始值,相对于gravity
mWmParams.x = 0;
mWmParams.y = mScreenHeight / 2;
// 设置悬浮窗口长宽数据
mWmParams.width = LayoutParams.WRAP_CONTENT;
mWmParams.height = LayoutParams.WRAP_CONTENT;
addView(createView(mContext));
mWindowManager.addView(this, mWmParams);
mTimer = new Timer();
hide();
}
private static int convertGravity(View view, int gravity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
boolean isRtl = view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
if (gravity == Gravity.START) {
gravity = isRtl ? Gravity.RIGHT : Gravity.LEFT;
} else if (gravity == Gravity.END) {
gravity = isRtl ? Gravity.LEFT : Gravity.RIGHT;
}
}
return gravity;
}
private void initLayoutParams() {
mLayoutParams = new WindowManager.LayoutParams();
mLayoutParams.gravity = Gravity.LEFT + Gravity.TOP;
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
mLayoutParams.format = PixelFormat.TRANSLUCENT;
mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
mLayoutParams.width = mWindowManager.getDefaultDisplay().getWidth();
mLayoutParams.height = mWindowManager.getDefaultDisplay().getHeight();
}
/**
* Check the lock mode of the drawer with the given gravity.
*
* @param edgeGravity Gravity of the drawer to check
* @return one of {@link #LOCK_MODE_UNLOCKED}, {@link #LOCK_MODE_LOCKED_CLOSED} or
* {@link #LOCK_MODE_LOCKED_OPEN}.
*/
public int getDrawerLockMode(int edgeGravity) {
final int absGravity = GravityCompat.getAbsoluteGravity(
edgeGravity, ViewCompat.getLayoutDirection(this));
if (absGravity == Gravity.LEFT) {
return mLockModeLeft;
} else if (absGravity == Gravity.RIGHT) {
return mLockModeRight;
}
return LOCK_MODE_UNLOCKED;
}
private void closeOtherDrawer() {
final int otherGrav = mAbsGravity == Gravity.LEFT ? Gravity.RIGHT : Gravity.LEFT;
final View toClose = findDrawerWithGravity(otherGrav);
if (toClose != null) {
closeDrawer(toClose);
}
}
public static void dispatchChildInsets(View child, Object insets, int gravity) {
WindowInsets wi = (WindowInsets) insets;
if (gravity == Gravity.LEFT) {
wi = wi.replaceSystemWindowInsets(wi.getSystemWindowInsetLeft(),
wi.getSystemWindowInsetTop(), 0, wi.getSystemWindowInsetBottom());
} else if (gravity == Gravity.RIGHT) {
wi = wi.replaceSystemWindowInsets(0, wi.getSystemWindowInsetTop(),
wi.getSystemWindowInsetRight(), wi.getSystemWindowInsetBottom());
}
child.dispatchApplyWindowInsets(wi);
}
public void updateFabGravity() {
CoordinatorLayout.LayoutParams mainFabParams = (CoordinatorLayout.LayoutParams) mMainFab.getLayoutParams();
if (HiSettingsHelper.getInstance().isFabLeftSide()) {
mainFabParams.anchorGravity = Gravity.BOTTOM | Gravity.LEFT | Gravity.END;
} else {
mainFabParams.anchorGravity = Gravity.BOTTOM | Gravity.RIGHT | Gravity.END;
}
if (HiSettingsHelper.getInstance().isFabAutoHide()) {
mainFabParams.setBehavior(new FABHideOnScrollBehavior());
} else {
mainFabParams.setBehavior(null);
mMainFab.show();
}
}
private View createBackView() {
ImageView backView = new ImageView(mContext);
backView.setImageDrawable(VenvyResourceUtil.getDrawable(mContext, "img_scanner_btn_back"));
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER_VERTICAL | Gravity.LEFT;
params.leftMargin = VenvyUIUtil.dip2px(mContext, 10);
backView.setLayoutParams(params);
backView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mGridView != null) {
removeView(mGridView);
imageModel.onDestroy();
imageModel = null;
mGridView = null;
imageItemAdapter = null;
mListView.setVisibility(VISIBLE);
mCancelView.setVisibility(GONE);
mTitleView.setText("选择相册");
} else {//关闭diaolog
removeAllViews();
if (imageModel != null) {
imageModel.onDestroy();
imageModel = null;
}
mGridView = null;
imageItemAdapter = null;
mListView = null;
if (imageFolderModel != null) {
imageFolderModel.onDestroy();
imageFolderModel = null;
}
imageFloderAdapter = null;
if (mDismissDialogListener != null) {
mDismissDialogListener.onClick(null);
}
}
}
});
return backView;
}
@Override
public void onCheckedChanged(final RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.select_scene_1: {
// You can start an automatic transition with TransitionManager.go().
TransitionManager.go(mScene1);
break;
}
case R.id.select_scene_2: {
TransitionSet set = new TransitionSet();
Slide slide = new Slide(Gravity.LEFT);
slide.addTarget(R.id.transition_title);
set.addTransition(slide);
set.addTransition(new ChangeBounds());
set.addTransition(new ChangeImageTransform());
set.setOrdering(TransitionSet.ORDERING_TOGETHER);
set.setDuration(350);
TransitionManager.go(mScene2, set);
break;
}
case R.id.select_scene_3: {
// You can also start a transition with a custom TransitionManager.
mTransitionManagerForScene3.transitionTo(mScene3);
break;
}
case R.id.select_scene_4: {
// Alternatively, transition can be invoked dynamically without a Scene.
// For this, we first call TransitionManager.beginDelayedTransition().
TransitionManager.beginDelayedTransition(mSceneRoot, new ChangeBounds());
// Then, we can just change view properties as usual.
View square = mSceneRoot.findViewById(R.id.transition_square);
ViewGroup.LayoutParams params = square.getLayoutParams();
int newSize = getResources().getDimensionPixelSize(R.dimen.square_size_expanded);
params.width = newSize;
params.height = newSize;
square.setLayoutParams(params);
square.invalidate();
break;
}
}
}
/**
* This method will be invoked when the current page is scrolled, either as part
* of a programmatically initiated smooth scroll or a user initiated touch scroll.
* If you override this method you must call through to the superclass implementation
* (e.g. super.onPageScrolled(position, offset, offsetPixels)) before onPageScrolled
* returns.
*
* @param position Position index of the first page currently being displayed.
* Page position+1 will be visible if positionOffset is nonzero.
* @param offset Value from [0, 1) indicating the offset from the page at position.
* @param offsetPixels Value in pixels indicating the offset from position.
*/
protected void onPageScrolled(int position, float offset, int offsetPixels) {
// Offset any decor views if needed - keep them on-screen at all times.
if (mDecorChildCount > 0) {
final int scrollX = getScrollX();
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
final int width = getWidth();
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (!lp.isDecor) continue;
final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
int childLeft = 0;
switch (hgrav) {
default:
childLeft = paddingLeft;
break;
case Gravity.LEFT:
childLeft = paddingLeft;
paddingLeft += child.getWidth();
break;
case Gravity.CENTER_HORIZONTAL:
childLeft = Math.max((width - child.getMeasuredWidth()) / 2,
paddingLeft);
break;
case Gravity.RIGHT:
childLeft = width - paddingRight - child.getMeasuredWidth();
paddingRight += child.getMeasuredWidth();
break;
}
childLeft += scrollX;
final int childOffset = childLeft - child.getLeft();
if (childOffset != 0) {
child.offsetLeftAndRight(childOffset);
}
}
}
if (mOnPageChangeListener != null) {
mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
}
if (mInternalPageChangeListener != null) {
mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
}
transformPages();
mCalledSuper = true;
}
@SuppressLint("RtlHardcoded")
private void getOffset() {
if (!tReGetOffset)
return;
tReGetOffset = false;
final int measuredWidth = getMeasuredWidth();
final int measuredHeight = getMeasuredHeight();
final int paddingStart = Compat.getPaddingStart(this);
final int paddingTop = getPaddingTop();
final int paddingEnd = Compat.getPaddingEnd(this);
final int paddingBottom = getPaddingBottom();
final int itemWidth = drawableWidth * mMax + mDrawablePadding * (mMax - 1);
switch (mGravity) {
default:
case Compat.START:
case Gravity.LEFT:
case Gravity.TOP:
case Compat.START | Gravity.TOP:
case Gravity.LEFT | Gravity.TOP:
xOffset = paddingStart;
yOffset = paddingTop;
break;
case Gravity.CENTER_HORIZONTAL:
case Gravity.CENTER_HORIZONTAL | Gravity.TOP:
xOffset = measuredWidth * 0.5f - itemWidth * 0.5f;
yOffset = paddingTop;
break;
case Compat.END:
case Gravity.RIGHT:
case Compat.END | Gravity.TOP:
case Gravity.RIGHT | Gravity.TOP:
xOffset = measuredWidth - paddingEnd - itemWidth;
yOffset = paddingTop;
break;
case Gravity.CENTER_VERTICAL:
case Gravity.CENTER_VERTICAL | Compat.START:
case Gravity.CENTER_VERTICAL | Gravity.LEFT:
xOffset = paddingStart;
yOffset = measuredHeight * 0.5f - drawableHeight * 0.5f;
break;
case Gravity.CENTER:
xOffset = measuredWidth * 0.5f - itemWidth * 0.5f;
yOffset = measuredHeight * 0.5f - drawableHeight * 0.5f;
break;
case Gravity.CENTER_VERTICAL | Compat.END:
case Gravity.CENTER_VERTICAL | Gravity.RIGHT:
xOffset = measuredWidth - paddingEnd - itemWidth;
yOffset = measuredHeight * 0.5f - drawableHeight * 0.5f;
break;
case Gravity.BOTTOM:
case Gravity.BOTTOM | Compat.START:
case Gravity.BOTTOM | Gravity.LEFT:
xOffset = paddingStart;
yOffset = measuredHeight - paddingBottom - drawableHeight;
break;
case Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL:
xOffset = measuredWidth * 0.5f - itemWidth * 0.5f;
yOffset = measuredHeight - paddingBottom - drawableHeight;
break;
case Gravity.BOTTOM | Compat.END:
case Gravity.BOTTOM | Gravity.RIGHT:
xOffset = measuredWidth - paddingEnd - itemWidth;
yOffset = measuredHeight - paddingBottom - drawableHeight;
break;
}
}