下面列出了android.support.v4.view.ViewCompat#setPaddingRelative ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
VersionViewHolder(View view) {
super(view);
version = (TextView) view.findViewById(R.id.version);
statusInstalled = (TextView) view.findViewById(R.id.status_installed);
statusSuggested = (TextView) view.findViewById(R.id.status_suggested);
statusIncompatible = (TextView) view.findViewById(R.id.status_incompatible);
versionCode = view.findViewById(R.id.versionCode);
added = (TextView) view.findViewById(R.id.added);
expandArrow = (ImageView) view.findViewById(R.id.expand_arrow);
expandedLayout = (View) view.findViewById(R.id.expanded_layout);
repository = (TextView) view.findViewById(R.id.repository);
size = (TextView) view.findViewById(R.id.size);
api = (TextView) view.findViewById(R.id.api);
buttonInstallUpgrade = (Button) view.findViewById(R.id.button_install_upgrade);
buttonDowngrade = (Button) view.findViewById(R.id.button_downgrade);
busyIndicator = (View) view.findViewById(R.id.busy_indicator);
incompatibleReasons = (TextView) view.findViewById(R.id.incompatible_reasons);
targetArch = (TextView) view.findViewById(R.id.target_arch);
int margin = context.getResources().getDimensionPixelSize(R.dimen.layout_horizontal_margin);
int padding = context.getResources().getDimensionPixelSize(R.dimen.details_activity_padding);
ViewCompat.setPaddingRelative(view, margin + padding + ViewCompat.getPaddingStart(view), view.getPaddingTop(), margin + ViewCompat.getPaddingEnd(view), view.getPaddingBottom());
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (mCenterAlign) {
View firstTab = ((ViewGroup) getChildAt(0)).getChildAt(0);
View lastTab = ((ViewGroup) getChildAt(0)).getChildAt(((ViewGroup) getChildAt(0)).getChildCount() - 1);
ViewCompat.setPaddingRelative(getChildAt(0),
(getWidth() / 2) - (firstTab.getWidth() / 2),
0,
(getWidth() / 2) - (lastTab.getWidth() / 2),
0);
}
if (mAnimatedIndicator == null) {
setupAnimatedIndicator();
}
onPageScrolled(mTempPosition, mTempPositionOffset, mTempPositionOffsetPixels);
}
/**
* sets a single padding on a view, without changing the rest of the padding values of the view
*/
public static void setPaddingOrRelativePadding(View v, ViewPadding paddingType, int paddingValueToSetInPixels) {
switch (paddingType) {
case BOTTOM:
ViewCompat.setPaddingRelative(v, ViewCompat.getPaddingStart(v), v.getPaddingTop(), ViewCompat.getPaddingEnd(v), paddingValueToSetInPixels);
break;
case END_OR_RIGHT:
ViewCompat.setPaddingRelative(v, ViewCompat.getPaddingStart(v), v.getPaddingTop(), paddingValueToSetInPixels, v.getPaddingBottom());
break;
case START_OR_LEFT:
ViewCompat.setPaddingRelative(v, paddingValueToSetInPixels, v.getPaddingTop(), ViewCompat.getPaddingEnd(v), v.getPaddingBottom());
break;
case TOP:
ViewCompat.setPaddingRelative(v, ViewCompat.getPaddingStart(v), paddingValueToSetInPixels, ViewCompat.getPaddingEnd(v), v.getPaddingBottom());
break;
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (tabStrip.isIndicatorAlwaysInCenter() && tabStrip.getChildCount() > 0) {
View firstTab = tabStrip.getChildAt(0);
View lastTab = tabStrip.getChildAt(getChildCount() - 1);
int start = (w - Utils.getMeasuredWidth(firstTab)) / 2 - Utils.getMarginStart(firstTab);
int end = (w - Utils.getMeasuredWidth(lastTab)) / 2 - Utils.getMarginEnd(lastTab);
tabStrip.setMinimumWidth(tabStrip.getMeasuredWidth());
ViewCompat.setPaddingRelative(this, start, getPaddingTop(), end, getPaddingBottom());
setClipToPadding(false);
}
}
/**
* TagView
* 动态创建TextView
* @return
*/
private TextView createNewFlexItemTextView(String str, final int pos) {
TextView textView = new TextView(mContext);
textView.setGravity(Gravity.CENTER);
textView.setText(str);
textView.setTextSize(tagViewBuilder.getTextSize());
textView.setTextColor(ColorUtils.createColorStateList(tagViewBuilder.getTextSelectColor(),tagViewBuilder.getTextSelectColor(),tagViewBuilder.getTextColor()));
textView.setTag(pos);
textView.setBackgroundResource(tagViewBuilder.getBackgroudRes());
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int position= (int) v.getTag();
onTagViewPressListener.onPress(v,tagViewBuilder.getTitles()[position],position);
}
});
float padding = DisplayUtils.dip2px(mContext, tagViewBuilder.getPaddingTopAndBottom());
float paddingLeftAndRight =
DisplayUtils.dip2px(mContext, tagViewBuilder.getPaddingLeftAndRight());
ViewCompat.setPaddingRelative(
textView,
(int)paddingLeftAndRight,
(int)padding,
(int)paddingLeftAndRight,
(int)padding
);
LayoutParams layoutParams = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
float margin = DisplayUtils.dip2px(mContext, tagViewBuilder.getMarginLeftAndRight());
float marginTopAndBottom =
DisplayUtils.dip2px(mContext, tagViewBuilder.getMarginTopAndBottom());
layoutParams.setMargins((int)margin, (int)marginTopAndBottom, (int)margin, (int)marginTopAndBottom);
layoutParams.setMargins(10,10,10,10);
textView.setLayoutParams(layoutParams);
return textView;
}
public void setHelperTextEnabled(boolean _enabled) {
if (mHelperTextEnabled == _enabled) return;
if (_enabled && mErrorEnabled) {
setErrorEnabled(false);
}
if (this.mHelperTextEnabled != _enabled) {
if (_enabled) {
this.mHelperView = new TextView(this.getContext());
this.mHelperView.setTextAppearance(this.getContext(), this.mHelperTextAppearance);
if (mHelperTextColor != null){
this.mHelperView.setTextColor(mHelperTextColor);
}
this.mHelperView.setVisibility(INVISIBLE);
this.addView(this.mHelperView);
if (this.mHelperView != null) {
ViewCompat.setPaddingRelative(
this.mHelperView,
ViewCompat.getPaddingStart(getEditText()),
0, ViewCompat.getPaddingEnd(getEditText()),
getEditText().getPaddingBottom());
}
} else {
this.removeView(this.mHelperView);
this.mHelperView = null;
}
this.mHelperTextEnabled = _enabled;
}
}
public TabView(Context context, int background, int[] margins,
int[] paddings, int tabMinWidth, int tabMaxWidth,
int tabTextSize, int tabTextAppearance, ColorStateList tabTextColors) {
super(context);
if (background != 0) {
setBackgroundDrawable(getDrawable(context, background));
}
ViewCompat.setPaddingRelative(this, paddings[0], paddings[1], paddings[2], paddings[3]);
mTabMinWidth = tabMinWidth;
mTabMaxWidth = tabMaxWidth;
mTabTextSize = tabTextSize;
mTabTextAppearance = tabTextAppearance;
mTabTextColors = tabTextColors;
MarginLayoutParams marginLayoutParams = new MarginLayoutParams(MarginLayoutParams.WRAP_CONTENT, MarginLayoutParams.WRAP_CONTENT);
marginLayoutParams.leftMargin = margins[0];
marginLayoutParams.topMargin = margins[1];
marginLayoutParams.rightMargin = margins[2];
marginLayoutParams.bottomMargin = margins[3];
setLayoutParams(marginLayoutParams);
setGravity(Gravity.CENTER);
}
public TabView(Context context, int background, int[] margins,
int[] paddings, int tabMinWidth, int tabMaxWidth,
int tabTextSize, int tabTextAppearance, ColorStateList tabTextColors) {
super(context);
if (background != 0) {
setBackgroundDrawable(getDrawable(context, background));
}
ViewCompat.setPaddingRelative(this, paddings[0], paddings[1], paddings[2], paddings[3]);
mTabMinWidth = tabMinWidth;
mTabMaxWidth = tabMaxWidth;
mTabTextSize = tabTextSize;
mTabTextAppearance = tabTextAppearance;
mTabTextColors = tabTextColors;
MarginLayoutParams marginLayoutParams = new MarginLayoutParams(MarginLayoutParams.WRAP_CONTENT, MarginLayoutParams.WRAP_CONTENT);
marginLayoutParams.leftMargin = margins[0];
marginLayoutParams.topMargin = margins[1];
marginLayoutParams.rightMargin = margins[2];
marginLayoutParams.bottomMargin = margins[3];
setLayoutParams(marginLayoutParams);
setGravity(Gravity.CENTER);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (tabStrip.isIndicatorAlwaysInCenter() && tabStrip.getChildCount() > 0) {
View firstTab = tabStrip.getChildAt(0);
View lastTab = tabStrip.getChildAt(tabStrip.getChildCount() - 1);
int start = (w - Utils.getMeasuredWidth(firstTab)) / 2 - Utils.getMarginStart(firstTab);
int end = (w - Utils.getMeasuredWidth(lastTab)) / 2 - Utils.getMarginEnd(lastTab);
tabStrip.setMinimumWidth(tabStrip.getMeasuredWidth());
ViewCompat.setPaddingRelative(this, start, getPaddingTop(), end, getPaddingBottom());
setClipToPadding(false);
}
}
private static void updateTopBottomPadding(View view, int topPadding, int bottomPadding) {
if (ViewCompat.isPaddingRelative(view)) {
ViewCompat.setPaddingRelative(view,
ViewCompat.getPaddingStart(view), topPadding,
ViewCompat.getPaddingEnd(view), bottomPadding);
} else {
view.setPadding(view.getPaddingLeft(), topPadding,
view.getPaddingRight(), bottomPadding);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (tabStrip.isIndicatorAlwaysInCenter() && tabStrip.getChildCount() > 0) {
View firstTab = tabStrip.getChildAt(0);
View lastTab = tabStrip.getChildAt(tabStrip.getChildCount() - 1);
int start = (w - Utils.getMeasuredWidth(firstTab)) / 2 - Utils.getMarginStart(firstTab);
int end = (w - Utils.getMeasuredWidth(lastTab)) / 2 - Utils.getMarginEnd(lastTab);
tabStrip.setMinimumWidth(tabStrip.getMeasuredWidth());
ViewCompat.setPaddingRelative(this, start, getPaddingTop(), end, getPaddingBottom());
setClipToPadding(false);
}
}
private static void updateTopBottomPadding(View view, int topPadding, int bottomPadding) {
if (ViewCompat.isPaddingRelative(view)) {
ViewCompat.setPaddingRelative(view,
ViewCompat.getPaddingStart(view), topPadding,
ViewCompat.getPaddingEnd(view), bottomPadding);
} else {
view.setPadding(view.getPaddingLeft(), topPadding,
view.getPaddingRight(), bottomPadding);
}
}
private static void updateTopBottomPadding(View view, int topPadding, int bottomPadding) {
if (ViewCompat.isPaddingRelative(view)) {
ViewCompat.setPaddingRelative(view, ViewCompat.getPaddingStart(view), topPadding, ViewCompat.getPaddingEnd(view), bottomPadding);
} else {
view.setPadding(view.getPaddingLeft(), topPadding, view.getPaddingRight(), bottomPadding);
}
}
public TabView(Context context, Tab tab) {
super(context);
mTab = tab;
if (mTabBackgroundResId != 0) {
setBackgroundDrawable(TintManager.getDrawable(context, mTabBackgroundResId));
}
ViewCompat.setPaddingRelative(this, mTabPaddingStart, mTabPaddingTop,
mTabPaddingEnd, mTabPaddingBottom);
setGravity(Gravity.CENTER);
update();
}
public static void setPaddingStart(TextView target, int paddingStart) {
ViewCompat.setPaddingRelative(target, paddingStart, target.getPaddingTop(),
ViewCompat.getPaddingEnd(target), target.getPaddingBottom());
}
@Override
public void setValue(TextView view, int paddingStart) {
ViewCompat.setPaddingRelative(view, paddingStart, view.getPaddingTop(),
ViewCompat.getPaddingEnd(view), view.getPaddingBottom());
}
@Override
public void setValue(TextView view, int paddingStart) {
ViewCompat.setPaddingRelative(view, paddingStart, view.getPaddingTop(),
ViewCompat.getPaddingEnd(view), view.getPaddingBottom());
}
public static void setPaddingStart(TextView target, int paddingStart) {
ViewCompat.setPaddingRelative(target, paddingStart, target.getPaddingTop(),
ViewCompat.getPaddingEnd(target), target.getPaddingBottom());
}
@SuppressWarnings("deprecation")
@Override
public DefaultAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TabTextView tabTextView = new TabTextView(parent.getContext());
if (mTabSelectedTextColorSet) {
tabTextView.setTextColor(tabTextView.createColorStateList(
tabTextView.getCurrentTextColor(), mTabSelectedTextColor));
}
ViewCompat.setPaddingRelative(tabTextView, mTabPaddingStart, mTabPaddingTop,
mTabPaddingEnd, mTabPaddingBottom);
tabTextView.setTextAppearance(parent.getContext(), mTabTextAppearance);
tabTextView.setGravity(Gravity.CENTER);
tabTextView.setMaxLines(MAX_TAB_TEXT_LINES);
tabTextView.setEllipsize(TextUtils.TruncateAt.END);
if (mTabOnScreenLimit > 0) {
int width = parent.getMeasuredWidth() / mTabOnScreenLimit;
tabTextView.setMaxWidth(width);
tabTextView.setMinWidth(width);
} else {
if (mTabMaxWidth > 0) {
tabTextView.setMaxWidth(mTabMaxWidth);
}
tabTextView.setMinWidth(mTabMinWidth);
}
tabTextView.setTextAppearance(tabTextView.getContext(), mTabTextAppearance);
if (mTabSelectedTextColorSet) {
tabTextView.setTextColor(tabTextView.createColorStateList(
tabTextView.getCurrentTextColor(), mTabSelectedTextColor));
}
if (mTabBackgroundResId != 0) {
tabTextView.setBackgroundDrawable(
AppCompatResources.getDrawable(tabTextView.getContext(), mTabBackgroundResId));
}
tabTextView.setLayoutParams(createLayoutParamsForTabs());
return new ViewHolder(tabTextView);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (! AccountManager.hasActiveBlog()) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
return;
}
if (! AccountManager.getActiveBlog().isLoggedIn()) {
// is it safe to infer that an active blog which is not logged in must mean the
// password has changed or Ghost Auth code is expired?
credentialsExpired();
}
setLayout(R.layout.activity_post_list);
setSupportActionBar(mToolbar);
if (BuildConfig.DEBUG) {
SpectreApplication.getInstance().addDebugDrawer(this);
}
// get rid of the default action bar confetti
//noinspection ConstantConditions
getSupportActionBar().setDisplayOptions(0);
// constants for animation
TypedValue typedColorValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorAccent, typedColorValue, true);
mColorAccent = typedColorValue.data;
getTheme().resolveAttribute(R.attr.colorPrimary, typedColorValue, true);
mColorPrimary = typedColorValue.data;
// initialize post list UI
final String activeBlogUrl = AccountManager.getActiveBlogUrl();
mPostAdapter = new PostAdapter(this, mPosts, activeBlogUrl, getPicasso(), v -> {
int pos = mPostList.getChildLayoutPosition(v);
if (pos == RecyclerView.NO_POSITION) return;
Post post = (Post) mPostAdapter.getItem(pos);
if (post.isMarkedForDeletion()) {
Snackbar.make(mPostList, R.string.status_marked_for_deletion_open_error,
Snackbar.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(PostListActivity.this, PostViewActivity.class);
intent.putExtra(BundleKeys.POST, post);
intent.putExtra(BundleKeys.START_EDITING, false);
Bundle activityOptions = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
v.getWidth(), v.getHeight()).toBundle();
startActivityForResult(intent, REQUEST_CODE_VIEW_POST, activityOptions);
});
mPostList.setAdapter(mPostAdapter);
mPostList.setLayoutManager(new StaggeredGridLayoutManager(
getResources().getInteger(R.integer.post_grid_num_columns),
StaggeredGridLayoutManager.VERTICAL));
mPostList.setItemAnimator(new DefaultItemAnimator());
int hSpace = getResources().getDimensionPixelOffset(R.dimen.card_grid_hspace);
int vSpace = getResources().getDimensionPixelOffset(R.dimen.card_grid_vspace);
mPostList.addItemDecoration(new SpaceItemDecoration(hSpace, vSpace));
// use a fixed-width grid on large screens
int screenWidth = DeviceUtils.getScreenWidth(this);
int maxContainerWidth = getResources().getDimensionPixelSize(R.dimen.post_grid_max_width);
if (screenWidth > maxContainerWidth) {
int containerPadding = (screenWidth - maxContainerWidth) / 2;
ViewCompat.setPaddingRelative(mToolbar,
ViewCompat.getPaddingStart(mToolbar) + containerPadding,
mToolbar.getPaddingTop(),
ViewCompat.getPaddingEnd(mToolbar) + containerPadding,
mToolbar.getPaddingBottom());
ViewCompat.setPaddingRelative(mPostList,
ViewCompat.getPaddingStart(mPostList) + containerPadding,
mPostList.getPaddingTop(),
ViewCompat.getPaddingEnd(mPostList) + containerPadding,
mPostList.getPaddingBottom());
}
final Drawable appbarShadowDrawable;
appbarShadowDrawable = ContextCompat.getDrawable(this, R.drawable.appbar_shadow);
mPostListContainer.setForeground(null); // hide the shadow initially
mPostList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int scrollY = mPostList.computeVerticalScrollOffset();
mAppBarBg.setTranslationY(-scrollY);
mPostListContainer.setForeground(scrollY <= 0 ? null : appbarShadowDrawable);
}
});
mRefreshDataRunnable = () -> refreshData(false);
mRefreshTimeoutRunnable = this::refreshTimedOut;
mSwipeRefreshLayout.setColorSchemeColors(mColorAccent, mColorPrimary);
mSwipeRefreshLayout.setOnRefreshListener(() -> refreshData(false));
}