下面列出了android.view.ViewGroup.MarginLayoutParams#setMargins ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void setMargin(View v, Integer l, Integer t, Integer r, Integer b) {
MarginLayoutParams p = getMarginInfo(v);
if (null == p) {
return;
}
if (l == null)
l = p.leftMargin;
if (t == null)
t = p.topMargin;
if (r == null)
r = p.rightMargin;
if (b == null)
b = p.bottomMargin;
p.setMargins(l, t, r, b);
v.requestLayout();
}
private void updatePosition() {
MarginLayoutParams lp = null;
if (mContainerType == ContainerType.STATUSBAR) {
lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
mHeightPx);
((FrameLayout.LayoutParams)lp).gravity = mPosition == Position.TOP ?
Gravity.TOP : Gravity.BOTTOM;
} else if (mContainerType == ContainerType.KEYGUARD) {
lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
mHeightPx);
if (mPosition == Position.TOP) {
((RelativeLayout.LayoutParams)lp).addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
} else {
((RelativeLayout.LayoutParams)lp).addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
}
}
if (lp != null) {
lp.setMargins(0, mPosition == Position.TOP ? mMarginPx : 0,
0, mPosition == Position.BOTTOM ? mMarginPx : 0);
setLayoutParams(lp);
}
}
/**
* Create a new instance of the bookmarks page.
* @param activity The activity to get context and manage fragments.
* @param tab The tab to load urls.
*/
public BookmarkPage(Activity activity, Tab tab) {
mActivity = activity;
mTab = tab;
mTitle = activity.getString(R.string.bookmarks);
mBackgroundColor = ApiCompatibilityUtils.getColor(activity.getResources(),
R.color.default_primary_color);
mThemeColor = ApiCompatibilityUtils.getColor(
activity.getResources(), R.color.default_primary_color);
mManager = new BookmarkManager(mActivity, false);
Resources res = mActivity.getResources();
MarginLayoutParams layoutParams = new MarginLayoutParams(
MarginLayoutParams.MATCH_PARENT, MarginLayoutParams.MATCH_PARENT);
layoutParams.setMargins(0,
res.getDimensionPixelSize(R.dimen.tab_strip_height)
+ res.getDimensionPixelSize(R.dimen.toolbar_height_no_shadow),
0, 0);
mManager.getView().setLayoutParams(layoutParams);
mManager.setUrlChangeListener(this);
}
public static void setLayout(View view, int x, int y) {
MarginLayoutParams margin = new MarginLayoutParams(
view.getLayoutParams());
margin.setMargins(x, y, x + margin.width, y + margin.height);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
margin);
view.setLayoutParams(layoutParams);
}
public static void placeViewAt(final View view, final int x, final int y, final int w,
final int h) {
final ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp instanceof MarginLayoutParams) {
final MarginLayoutParams marginLayoutParams = (MarginLayoutParams)lp;
marginLayoutParams.width = w;
marginLayoutParams.height = h;
marginLayoutParams.setMargins(x, y, -50, 0);
}
}
public static void placeViewAt(final View view, final int x, final int y, final int w,
final int h) {
final ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp instanceof MarginLayoutParams) {
final MarginLayoutParams marginLayoutParams = (MarginLayoutParams)lp;
marginLayoutParams.width = w;
marginLayoutParams.height = h;
marginLayoutParams.setMargins(x, y, 0, 0);
}
}
private void updatePosition() {
if (mMode == Mode.OFF) return;
MarginLayoutParams lp = null;
if (mContainerType == ContainerType.STATUSBAR) {
lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
mHeightPx);
((FrameLayout.LayoutParams)lp).gravity = mMode == Mode.TOP ?
Gravity.TOP : Gravity.BOTTOM;
} else if (mContainerType == ContainerType.KEYGUARD) {
lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
mHeightPx);
if (mMode == Mode.TOP) {
((RelativeLayout.LayoutParams)lp).addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
} else {
((RelativeLayout.LayoutParams)lp).addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
}
}
if (lp != null) {
lp.setMargins(0, mMode == Mode.TOP ? mEdgeMarginPx : 0,
0, mMode == Mode.BOTTOM ? mEdgeMarginPx : 0);
setLayoutParams(lp);
}
}
public static void placeViewAt(final View view, final int x, final int y, final int w,
final int h) {
final ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp instanceof MarginLayoutParams) {
final MarginLayoutParams marginLayoutParams = (MarginLayoutParams)lp;
marginLayoutParams.width = w;
marginLayoutParams.height = h;
marginLayoutParams.setMargins(x, y, 0, 0);
}
}
/**
* Subtract the top margin size equals status bar's height for view.
*
* @param view The view.
*/
public static void subtractMarginTopEqualStatusBarHeight(@NonNull View view) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
Object haveSetOffset = view.getTag(KEY_OFFSET);
if (haveSetOffset == null || !(Boolean) haveSetOffset) return;
MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
layoutParams.setMargins(layoutParams.leftMargin,
layoutParams.topMargin - getStatusBarHeight(),
layoutParams.rightMargin,
layoutParams.bottomMargin);
view.setTag(KEY_OFFSET, false);
}
private void updateMargins() {
MarginLayoutParams layoutParams = (MarginLayoutParams) mView.getLayoutParams();
if (mCurrentDisplayStyle == UiConfig.DISPLAY_STYLE_WIDE) {
layoutParams.setMargins(mWideMarginSizePixels, layoutParams.topMargin,
mWideMarginSizePixels, layoutParams.bottomMargin);
} else {
layoutParams.setMargins(mDefaultMarginSizePixels, layoutParams.topMargin,
mDefaultMarginSizePixels, layoutParams.bottomMargin);
}
}
private void matchMarginsToPadding(MarginLayoutParams layoutParams) {
@Px int leftMargin = orientation == LinearLayoutManager.VERTICAL ?
recyclerView.getPaddingLeft() : 0;
@Px int topMargin = orientation == LinearLayoutManager.VERTICAL ?
0 : recyclerView.getPaddingTop();
@Px int rightMargin = orientation == LinearLayoutManager.VERTICAL ?
recyclerView.getPaddingRight() : 0;
layoutParams.setMargins(leftMargin, topMargin, rightMargin, 0);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mListView = (ListView) getView().findViewById(android.R.id.list);
int marginTop = getActivity().getResources().getDimensionPixelSize(
R.dimen.search_engine_list_margin_top);
MarginLayoutParams layoutParams = (MarginLayoutParams) mListView.getLayoutParams();
layoutParams.setMargins(0, marginTop, 0, 0);
mListView.setLayoutParams(layoutParams);
mListView.setAdapter(mSearchEngineAdapter);
mListView.setDivider(null);
}
public static void setLayoutX(View view, int x) {
MarginLayoutParams margin = new MarginLayoutParams(
view.getLayoutParams());
margin.setMargins(x, margin.topMargin, x + margin.width,
margin.bottomMargin);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
margin);
view.setLayoutParams(layoutParams);
}
public static void setLayoutY(View view, int y) {
MarginLayoutParams margin = new MarginLayoutParams(
view.getLayoutParams());
margin.setMargins(margin.leftMargin, y, margin.rightMargin, y
+ margin.height);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
margin);
view.setLayoutParams(layoutParams);
}
public static void setMarginTop(final View v, final int margin) {
final MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
lp.setMargins(lp.leftMargin, margin, lp.rightMargin, lp.bottomMargin);
v.setLayoutParams(lp);
}
public static void setMarginLeft(final View v, final int margin) {
final MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
lp.setMargins(margin, lp.topMargin, lp.rightMargin, lp.bottomMargin);
v.setLayoutParams(lp);
}
public static void setMarginBottom(final View v, final int margin) {
final MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, margin);
v.setLayoutParams(lp);
}
@VisibleForTesting
void showToolTip(int toolTipToShowViewId, Rect itemBounds, SwitchAccessMenuLayout menuLayout) {
View toolTipToShow = menuOverlay.findViewById(toolTipToShowViewId);
if (toolTipToShow == null) {
return;
}
// We don't need to hide the previous tool tip if it is the same one we want to show now.
// However, we still need to adjust the margins of the tool tip (below) because the horizontal
// position of the item may have changed even if the vertical position hasn't.
if (lastToolTipViewIdShown != toolTipToShowViewId) {
menuOverlay.findViewById(lastToolTipViewIdShown).setVisibility(View.GONE);
toolTipToShow.setVisibility(View.VISIBLE);
lastToolTipViewIdShown = toolTipToShowViewId;
}
// The length of the tool tip is the longer dimension between its height and width. Since all
// tool tips are just transposed versions of each other, the width of the tool tip up
// represents the longest dimension for all tool tips.
int toolTipLength = menuOverlay.findViewById(R.id.tooltip_up).getWidth();
MarginLayoutParams toolTipMargins = (MarginLayoutParams) toolTipToShow.getLayoutParams();
toolTipMargins.setMargins(0, 0, 0, 0);
if ((toolTipToShowViewId == R.id.tooltip_up) || (toolTipToShowViewId == R.id.tooltip_down)) {
Point screenSize = ScreenUtils.getRealScreenSize(getContext());
boolean isItemNearLeftEdge = itemBounds.centerX() < (screenSize.x / 2);
int distanceFromItemCenterToScreenEdge =
isItemNearLeftEdge ? itemBounds.centerX() : (screenSize.x - itemBounds.centerX());
int distanceFromToolTipCenterToScreenEdge =
(toolTipLength / 2) + menuOverlay.findViewById(R.id.menu_layout).getLeft();
if (distanceFromItemCenterToScreenEdge > minDistanceFromToolTipToScreenEdge) {
// Place the center of the tooltip at the center of the item.
toolTipMargins.leftMargin = itemBounds.centerX() - distanceFromToolTipCenterToScreenEdge;
} else {
// Place the center of the tooltip at the item edge furthest from the screen edge.
int itemEdge = isItemNearLeftEdge ? itemBounds.right : itemBounds.left;
toolTipMargins.leftMargin = itemEdge - distanceFromToolTipCenterToScreenEdge;
}
} else {
MarginLayoutParams menuMargins =
(MarginLayoutParams) menuOverlay.findViewById(R.id.menu_layout).getLayoutParams();
toolTipMargins.topMargin = itemBounds.centerY() - (toolTipLength / 2) - menuMargins.topMargin;
}
toolTipToShow.setLayoutParams(toolTipMargins);
// Set the tooltip and content so a border can be drawn around the menu.
menuLayout.setToolTipView(toolTipToShow);
}
@VisibleForTesting
void adjustGlobalMenuButtonPosition(
@Nullable DisplayCutout displayCutout, List<Rect> cutoutRects) {
LayoutParams globalMenuButtonParams = globalMenuButtonOverlay.getParams();
if (displayCutout == null) {
globalMenuButtonParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
} else {
Point screenSize = ScreenUtils.getRealScreenSize(getContext());
View menuButton = globalMenuButtonOverlay.findViewById(R.id.global_menu_button);
int menuButtonWidth = menuButton.getWidth();
int leftOfCenteredMenuButton = (screenSize.x / 2) - (menuButtonWidth / 2);
Rect centeredMenuButton =
new Rect(
leftOfCenteredMenuButton,
0,
leftOfCenteredMenuButton + menuButtonWidth,
menuButton.getHeight());
// Clear the margins.
MarginLayoutParams menuButtonMarginParams = (MarginLayoutParams) menuButton.getLayoutParams();
menuButtonMarginParams.setMargins(0, 0, 0, 0);
// Find the cutout that intersects the centered menu button. If there's space to the right of
// the cutout, move there. Otherwise if there's space to the left, move there. If neither side
// has space, move just below the cutout.
int safeInsetLeft = displayCutout.getSafeInsetLeft();
int safeInsetRight = displayCutout.getSafeInsetRight();
boolean cutoutIntersectsCenteredMenuButton = false;
for (Rect cutoutRect : cutoutRects) {
if (Rect.intersects(centeredMenuButton, cutoutRect)) {
cutoutIntersectsCenteredMenuButton = true;
if ((screenSize.x - safeInsetRight - cutoutRect.right) > menuButtonWidth) {
globalMenuButtonParams.gravity = Gravity.TOP | Gravity.END;
menuButtonMarginParams.rightMargin = screenSize.x - cutoutRect.right - menuButtonWidth;
} else if ((cutoutRect.left - safeInsetLeft) > menuButtonWidth) {
globalMenuButtonParams.gravity = Gravity.TOP | Gravity.START;
menuButtonMarginParams.leftMargin = cutoutRect.left - menuButtonWidth;
} else {
globalMenuButtonParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
menuButtonMarginParams.topMargin = displayCutout.getSafeInsetTop();
}
}
}
if (!cutoutIntersectsCenteredMenuButton) {
globalMenuButtonParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
}
}
globalMenuButtonOverlay.setParams(globalMenuButtonParams);
}
public static void setMarginRight(final View v, final int margin) {
final MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
lp.setMargins(lp.leftMargin, lp.topMargin, margin, lp.bottomMargin);
v.setLayoutParams(lp);
}