下面列出了android.view.ViewGroup#MarginLayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void setViewMargin(View view, int margin) {
WeakReference<View> viewWeakReference = new WeakReference<>(view);
if (viewWeakReference.get() != null) {
if (viewWeakReference.get().getLayoutParams() != null &&
(viewWeakReference.get().getLayoutParams() instanceof ViewGroup.MarginLayoutParams)) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
if (margin != -1) {
params.leftMargin = margin;
params.rightMargin = margin;
params.topMargin = margin;
params.bottomMargin = margin;
}
viewWeakReference.get().setLayoutParams(params);
}
}
}
@Override
protected void execute(View view, int val)
{
if(!(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams))
{
return ;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
lp.leftMargin = val;
}
public void updatePaddings() {
if (listView != null && (expandingEnabled || fullScreenLayoutEnabled)) {
int actionBarHeight = obtainActionBarHeight(activity);
int statusBarHeight = statusBar.getHeight();
int bottomNavigationBarHeight = navigationBar.getBottom();
int rightNavigationBarHeight = navigationBar.getRight();
setNewPadding((View) listView.getParent(), 0, 0, rightNavigationBarHeight, 0);
setNewPadding(listView, KEEP, statusBarHeight + actionBarHeight, KEEP, bottomNavigationBarHeight);
if (actionModeView != null) {
((ViewGroup.MarginLayoutParams) actionModeView.getLayoutParams()).rightMargin =
rightNavigationBarHeight;
}
if (additionalViews != null) {
for (LinkedHashMap.Entry<View, Boolean> additional : additionalViews.entrySet()) {
additional.getKey().setPadding(0, statusBarHeight + (additional.getValue() ? actionBarHeight : 0),
rightNavigationBarHeight, bottomNavigationBarHeight);
}
}
if (drawerListView != null) {
int paddingTop = C.API_LOLLIPOP && drawerOverToolbarEnabled && toolbarView != null
? statusBarHeight : statusBarHeight + actionBarHeight;
if (drawerHeader != null) {
setNewPadding(drawerHeader, KEEP, paddingTop, KEEP, KEEP);
setNewPadding(drawerListView, KEEP, 0, KEEP, bottomNavigationBarHeight);
} else {
setNewPadding(drawerListView, KEEP, paddingTop, KEEP, bottomNavigationBarHeight);
}
}
if (contentForeground != null) {
contentForeground.invalidateSelf();
}
if (statusBarContentForeground != null) {
statusBarContentForeground.invalidateSelf();
}
if (statusBarDrawerForeground != null) {
statusBarDrawerForeground.invalidateSelf();
}
}
}
/**
* Show the snackbar.
*/
public Snackbar show() {
final View view = this.view;
if (view == null) return null;
if (messageColor != COLOR_DEFAULT) {
SpannableString spannableString = new SpannableString(message);
ForegroundColorSpan colorSpan = new ForegroundColorSpan(messageColor);
spannableString.setSpan(
colorSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
);
sReference = new WeakReference<>(Snackbar.make(view, spannableString, duration));
} else {
sReference = new WeakReference<>(Snackbar.make(view, message, duration));
}
final Snackbar snackbar = sReference.get();
final View snackbarView = snackbar.getView();
if (bgResource != -1) {
snackbarView.setBackgroundResource(bgResource);
} else if (bgColor != COLOR_DEFAULT) {
snackbarView.setBackgroundColor(bgColor);
}
if (bottomMargin != 0) {
ViewGroup.MarginLayoutParams params =
(ViewGroup.MarginLayoutParams) snackbarView.getLayoutParams();
params.bottomMargin = bottomMargin;
}
if (actionText.length() > 0 && actionListener != null) {
if (actionTextColor != COLOR_DEFAULT) {
snackbar.setActionTextColor(actionTextColor);
}
snackbar.setAction(actionText, actionListener);
}
snackbar.show();
return snackbar;
}
/**
* refresh's margin if set
*/
private void refreshMargin() {
if (isWeakReferenceValid()) {
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getTextView().get().getLayoutParams();
layoutParams.bottomMargin = mEdgeMarginInPx;
layoutParams.topMargin = mEdgeMarginInPx;
layoutParams.rightMargin = mEdgeMarginInPx;
layoutParams.leftMargin = mEdgeMarginInPx;
getTextView().get().setLayoutParams(layoutParams);
}
}
/**
* set toolbar layout
*
* @param layoutResId
* @param barHeight
* @return
*/
public View setTitleLayout(@LayoutRes int layoutResId, int barHeight) {
if (mTitleView != null) {
return mTitleView;
}
setViewStubLayoutRes(R.id.base_action_bar_stub, layoutResId);
mTitleView = inflateViewStub(R.id.base_action_bar_stub);
if (mContentWrapView != null) {
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mContentWrapView
.getLayoutParams();
lp.topMargin = barHeight;
}
return mTitleView;
}
static int getMarginEnd(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginEnd(lp);
}
/**
* Add the top margin size equals status bar's height for view.
*
* @param view The view.
*/
public static void addMarginTopEqualStatusBarHeight(@NonNull View view) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
view.setTag(TAG_OFFSET);
Object haveSetOffset = view.getTag(KEY_OFFSET);
if (haveSetOffset != null && (Boolean) haveSetOffset) return;
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
layoutParams.setMargins(layoutParams.leftMargin,
layoutParams.topMargin + getStatusBarHeight(),
layoutParams.rightMargin,
layoutParams.bottomMargin);
view.setTag(KEY_OFFSET, true);
}
public static int getMarginLeft(android.view.View view) {
ViewGroup.LayoutParams params = view.getLayoutParams();
if (params instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params;
return lp.leftMargin;
}
return 0;
}
/**
* 设置标题栏MarginTop值为导航栏的高度
* Sets title bar margin top.
*
* @param activity the activity
* @param view the view
*/
public static void setTitleBarMarginTop(Activity activity, @NonNull View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
layoutParams.setMargins(layoutParams.leftMargin,
layoutParams.topMargin + getStatusBarHeight(activity),
layoutParams.rightMargin,
layoutParams.bottomMargin);
}
}
static int getMarginEnd(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginEnd(lp);
}
/**
* Modifies various views parameters and visibility based on the user preferences.
*/
private void applyPrefToViews() {
// Workaround v21+ status bar transparency issue.
// This is disabled if the status bar is hidden.
if (Utils.atLeastLollipop()
&& (PreferenceHelper.getWindowBarMode().equals("none")
|| PreferenceHelper.getWindowBarMode().equals("nav"))) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
ViewGroup.MarginLayoutParams homeParams = (ViewGroup.MarginLayoutParams) slidingHome
.getLayoutParams();
homeParams.topMargin = ViewUtils.getStatusBarHeight();
}
slidingHome.post(new Runnable() {
@Override public void run() {
// Hide the favourites panel when there's nothing to show.
if (pinnedAppsAdapter.isEmpty()) {
pinnedAppsContainer.setTranslationY(pinnedAppsContainer.getMeasuredHeight());
isFavouritesVisible = false;
} else {
isFavouritesVisible = true;
}
}
});
// Switch on wallpaper shade.
if (PreferenceHelper.useWallpaperShade()) {
// Tints the navigation bar with a semi-transparent shade.
if (Utils.atLeastLollipop()) {
getWindow().setNavigationBarColor(
getResources().getColor(R.color.navigationBarShade));
}
binding.wallpaperShade.setBackgroundResource(R.drawable.image_inner_shadow);
}
if ("transparent".equals(PreferenceHelper.getListBackground())) {
appsListContainer.setBackgroundColor(
Utils.getColorFromAttr(this, R.attr.backgroundColorAlt));
} else if ("none".equals(PreferenceHelper.getListBackground())) {
appsListContainer.setBackgroundColor(Color.TRANSPARENT);
}
}
/**
* {@inheritDoc}
*/
public LayoutParams(ViewGroup.MarginLayoutParams source) {
super(source);
if (gravity == 0)
gravity = GravityCompat.START | Gravity.TOP;
}
public static void setMarginEnd(ViewGroup.MarginLayoutParams lp, int marginEnd) {
lp.setMarginEnd(marginEnd);
}
protected void renderLayout(BaseCell cell, View view) {
if (cell.style != null) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp == null || !(lp instanceof VirtualLayoutManager.LayoutParams)) {
if (lp == null) {
lp = new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
} else {
lp = new VirtualLayoutManager.LayoutParams(lp.width, lp.height);
}
view.setLayoutParams(lp);
}
if (lp instanceof VirtualLayoutManager.LayoutParams) {
VirtualLayoutManager.LayoutParams params = (VirtualLayoutManager.LayoutParams) lp;
if (cell.style.height >= 0) {
params.storeOriginHeight();
params.height = cell.style.height;
} else {
params.restoreOriginHeight();
}
if (cell.style.width >= 0) {
params.storeOriginWidth();
params.width = cell.style.width;
} else {
params.restoreOriginWidth();
}
params.mAspectRatio = cell.style.aspectRatio;
params.zIndex = cell.style.zIndex;
if (params.zIndex == 0) {
if (cell.parent != null && cell.parent.style != null) {
params.zIndex = cell.parent.style.zIndex;
}
}
if (VERSION.SDK_INT >= 21) {
view.setZ(params.zIndex);
}
} else {
if (cell.style.height >= 0) {
lp.height = cell.style.height;
}
if (cell.style.width >= 0) {
lp.width = cell.style.width;
}
}
if (lp instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) lp;
layoutParams.topMargin = cell.style.margin[MARGIN_TOP_INDEX];
layoutParams.leftMargin = cell.style.margin[MARGIN_LEFT_INDEX];
layoutParams.bottomMargin = cell.style.margin[MARGIN_BOTTOM_INDEX];
layoutParams.rightMargin = cell.style.margin[MARGIN_RIGHT_INDEX];
}
// reset translation animation before reused
view.setTranslationX(0);
view.setTranslationY(0);
}
}
/**
* {@inheritDoc}
*/
public LayoutParams(ViewGroup.MarginLayoutParams source) {
super(source);
}
public static int getMarginEnd(ViewGroup.MarginLayoutParams lp) {
return lp.getMarginEnd();
}
public LayoutParams(ViewGroup.MarginLayoutParams source) {
super(source);
}
@Override
public void setMarginEnd(ViewGroup.MarginLayoutParams lp, int marginEnd) {
MarginLayoutParamsCompatJellybeanMr1.setMarginEnd(lp, marginEnd);
}
/**
* Get the relative ending margin that was set.
*
* <p>On platform versions supporting bidirectional text and layouts
* this value will be resolved into the LayoutParams object's left or right
* margin as appropriate when the associated View is attached to a window
* or when the layout direction of that view changes.</p>
*
* @param lp LayoutParams to query
* @return the margin along the ending edge in pixels
*/
public static int getMarginEnd(ViewGroup.MarginLayoutParams lp) {
return IMPL.getMarginEnd(lp);
}