下面列出了android.support.v4.view.ViewCompat#setBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void toggleEmojiView(boolean value){
if (value){
if (bubble != null) {
ViewCompat.setBackground(bubble, null);
bubble.setPadding(0, 0, 0, 0);
}
if (text != null){
text.setTextSize(EMOJI_VIEW_TEXT_SIZE);
}
}else {
if (bubble != null) {
ViewCompat.setBackground(bubble, defaultBackgroundDrawable);
bubble.setPadding(defaultPaddingLeft, defaultPaddingTop, defaultPaddingRight, defaultPaddingBottom);
}
if (text != null){
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, defaultTextSizePx);
}
}
}
public void createMenu(RecyclerView.ViewHolder viewHolder, SwipeMenu swipeMenu, Controller controller,
int direction, OnItemMenuClickListener itemClickListener) {
removeAllViews();
this.mViewHolder = viewHolder;
this.mItemClickListener = itemClickListener;
List<SwipeMenuItem> items = swipeMenu.getMenuItems();
for (int i = 0; i < items.size(); i++) {
SwipeMenuItem item = items.get(i);
LayoutParams params = new LayoutParams(item.getWidth(), item.getHeight());
params.weight = item.getWeight();
LinearLayout parent = new LinearLayout(getContext());
parent.setId(i);
parent.setGravity(Gravity.CENTER);
parent.setOrientation(VERTICAL);
parent.setLayoutParams(params);
ViewCompat.setBackground(parent, item.getBackground());
parent.setOnClickListener(this);
addView(parent);
SwipeMenuBridge menuBridge = new SwipeMenuBridge(controller, direction, i);
parent.setTag(menuBridge);
if (item.getImage() != null) {
ImageView iv = createIcon(item);
parent.addView(iv);
}
if (!TextUtils.isEmpty(item.getText())) {
TextView tv = createTitle(item);
parent.addView(tv);
}
}
}
@Override
public final void applyStyle(MessagesListStyle style) {
super.applyStyle(style);
if (time != null) {
time.setTextColor(style.getOutcomingImageTimeTextColor());
time.setTextSize(TypedValue.COMPLEX_UNIT_PX, style.getOutcomingImageTimeTextSize());
time.setTypeface(time.getTypeface(), style.getOutcomingImageTimeTextStyle());
}
if (imageOverlay != null) {
ViewCompat.setBackground(imageOverlay, style.getOutcomingImageOverlayDrawable());
}
}
/**
* 为指定View添加阴影
* @param view 目标View
* @param shapeRadius View的圆角
* @param shadowColor 阴影的颜色
* @param shadowRadius 阴影的宽度
* @param offsetX 阴影水平方向的偏移量
* @param offsetY 阴影垂直方向的偏移量
*/
public static void setShadowDrawable(View view, int shapeRadius, int shadowColor, int shadowRadius, int offsetX, int offsetY) {
ShadowDrawable drawable = new ShadowDrawable.Builder()
.setShapeRadius(shapeRadius)
.setShadowColor(shadowColor)
.setShadowRadius(shadowRadius)
.setOffsetX(offsetX)
.setOffsetY(offsetY)
.builder();
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
ViewCompat.setBackground(view, drawable);
}
/**
* 初始化画笔操作
*/
private void initPaint() {
bgPaint.setColor(backgroundColor);
bgPaint.setAntiAlias(true);
bgPaint.setStyle(Paint.Style.FILL);
setLayerType(LAYER_TYPE_SOFTWARE, null);
setWillNotDraw(false);
ViewCompat.setBackground(this, null);
}
private void addTag(Tag tag) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
float x = getXCoOrdForTag(tag.getX_co_ord());
float y = getYCoOrdForTag(tag.getY_co_ord());
final View tagView = layoutInflater.
inflate(R.layout.view_for_tag, mRoot, false);
final TextView tagTextView = tagView.findViewById(R.id.tag_text_view);
final LinearLayout carrotTopContainer =
tagView.findViewById(R.id.carrot_top);
final LinearLayout textContainer =
tagView.findViewById(R.id.tag_text_container);
if (mTagTextDrawable != null) {
ViewCompat.setBackground(textContainer, mTagTextDrawable);
}
if (mCarrotTopDrawable != null) {
ViewCompat.setBackground(carrotTopContainer, mCarrotTopDrawable);
}
tagTextView.setText(tag.getUnique_tag_id());
setColorForTag(tagView);
tagView.setX(x);
tagView.setY(y);
mTagList.add(tagView);
mRoot.addView(tagView);
}
public void createMenu(SwipeMenu swipeMenu, SwipeSwitch swipeSwitch,
SwipeMenuItemClickListener swipeMenuItemClickListener,
@SwipeMenuRecyclerView.DirectionMode int direction) {
removeAllViews();
this.mSwipeSwitch = swipeSwitch;
this.mItemClickListener = swipeMenuItemClickListener;
this.mDirection = direction;
List<SwipeMenuItem> items = swipeMenu.getMenuItems();
for (int i = 0; i < items.size(); i++) {
SwipeMenuItem item = items.get(i);
LayoutParams params = new LayoutParams(item.getWidth(), item.getHeight());
params.weight = item.getWeight();
LinearLayout parent = new LinearLayout(getContext());
parent.setId(i);
parent.setGravity(Gravity.CENTER);
parent.setOrientation(VERTICAL);
parent.setLayoutParams(params);
ViewCompat.setBackground(parent, item.getBackground());
parent.setOnClickListener(this);
addView(parent);
SwipeMenuBridge menuBridge = new SwipeMenuBridge(mDirection, i, mSwipeSwitch, parent);
parent.setTag(menuBridge);
if (item.getImage() != null) {
ImageView iv = createIcon(item);
menuBridge.mImageView = iv;
parent.addView(iv);
}
if (!TextUtils.isEmpty(item.getText())) {
TextView tv = createTitle(item);
menuBridge.mTextView = tv;
parent.addView(tv);
}
}
}
private void setThemeBackground(View view) {
TypedValue a = new TypedValue();
getActivity().getTheme().resolveAttribute(android.R.attr.windowBackground, a, true);
if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT) {
view.setBackgroundColor(a.data);
} else {
try {
Drawable d = ResourcesCompat.getDrawable(getActivity().getResources(), a.resourceId, getActivity().getTheme());
ViewCompat.setBackground(view, d);
} catch (Resources.NotFoundException ignore) {
}
}
}
public static void setShapeBg(View view, int shape, int bgColor, int radius) {
ViewCompat.setBackground(view, getDrawable(shape, bgColor, radius));
}
public static void setShapeBg(View view, int shape, GradientDrawable.Orientation orientation,
int[] bgColor, float[] radius) {
ViewCompat.setBackground(view, getDrawable(shape, orientation, bgColor, 0, 0, radius));
}
private void init(Context context, AttributeSet attrs) {
init(context);
MessageInputStyle style = MessageInputStyle.parse(context, attrs);
this.messageInput.setMaxLines(style.getInputMaxLines());
this.messageInput.setHint(style.getInputHint());
this.messageInput.setText(style.getInputText());
this.messageInput.setTextSize(TypedValue.COMPLEX_UNIT_PX, style.getInputTextSize());
this.messageInput.setTextColor(style.getInputTextColor());
this.messageInput.setHintTextColor(style.getInputHintColor());
ViewCompat.setBackground(this.messageInput, style.getInputBackground());
setCursor(style.getInputCursorDrawable());
this.attachmentButton.setVisibility(style.showAttachmentButton() ? VISIBLE : GONE);
this.attachmentButton.setImageDrawable(style.getAttachmentButtonIcon());
this.attachmentButton.getLayoutParams().width = style.getAttachmentButtonWidth();
this.attachmentButton.getLayoutParams().height = style.getAttachmentButtonHeight();
ViewCompat.setBackground(this.attachmentButton, style.getAttachmentButtonBackground());
this.attachmentButtonSpace.setVisibility(style.showAttachmentButton() ? VISIBLE : GONE);
this.attachmentButtonSpace.getLayoutParams().width = style.getAttachmentButtonMargin();
this.messageSendButton.setImageDrawable(style.getInputButtonIcon());
this.messageSendButton.getLayoutParams().width = style.getInputButtonWidth();
this.messageSendButton.getLayoutParams().height = style.getInputButtonHeight();
ViewCompat.setBackground(messageSendButton, style.getInputButtonBackground());
this.sendButtonSpace.getLayoutParams().width = style.getInputButtonMargin();
if (getPaddingLeft() == 0
&& getPaddingRight() == 0
&& getPaddingTop() == 0
&& getPaddingBottom() == 0) {
setPadding(
style.getInputDefaultPaddingLeft(),
style.getInputDefaultPaddingTop(),
style.getInputDefaultPaddingRight(),
style.getInputDefaultPaddingBottom()
);
}
this.delayTypingStatusMillis = style.getDelayTypingStatus();
}
public static void setShapeBg(View view, int shape, int bgColor, float[] radius) {
ViewCompat.setBackground(view, getDrawable(shape, bgColor, radius));
}
@Override
protected void apply(Property prop, View view, Object newValue) {
ViewCompat.setBackground(view, (Drawable) newValue);
}
public MinimalKSnack setBackgrounDrawable(@NonNull @DrawableRes int drawableInt){
// Set drawable to view.
ViewCompat.setBackground(lnrSnack, ContextCompat.getDrawable(lnrSnack.getContext(), drawableInt));
return this;
}
public void setItemBackground(int background) {
Drawable backgroundDrawable = background == 0
? null : ContextCompat.getDrawable(getContext(), background);
ViewCompat.setBackground(this, backgroundDrawable);
}
public static void setSelectorBg(View view, int state, GradientDrawable.Orientation[] orientation,
int[][] bgColor, int[] borderColor, int borderWidth, int radius) {
ViewCompat.setBackground(view, getDrawable(state, GradientDrawable.RECTANGLE, orientation, bgColor,
borderColor, borderWidth, radius));
}
public static void setSelectorBg(View view, int state, int shape, GradientDrawable.Orientation[] orientation,
int[][] bgColor, int[] borderColor, int borderWidth, int radius) {
ViewCompat.setBackground(view, getDrawable(state, shape, orientation, bgColor, borderColor, borderWidth, radius));
}
/**
* 设置与状态关联的背景 -> 有不同圆角的纯色背景
* @param view
* @param state
* @param bgColor
* @param radius
*/
public static void setSelectorBg(View view, int state, int[] bgColor, float[] radius) {
ViewCompat.setBackground(view, getDrawable(state, GradientDrawable.RECTANGLE, bgColor, radius));
}
/**
* 设置与状态关联的背景 -> 有相同圆角的渐变背景
* @param view
* @param state
* @param orientation
* @param bgColor
* @param borderColor
* @param borderWidth
* @param radius
*/
public static void setSelectorBg(View view, int state, GradientDrawable.Orientation orientation,
int[][] bgColor, int[] borderColor, int borderWidth, int radius) {
ViewCompat.setBackground(view, getDrawable(state, GradientDrawable.RECTANGLE, orientation, bgColor,
borderColor, borderWidth, radius));
}
/**
* 设置与状态关联的背景 -> 有相同圆角的纯色背景
* @param view
* @param state
* @param bgColor
* @param radius
*/
public static void setSelectorBg(View view, int state, int[] bgColor, int radius) {
ViewCompat.setBackground(view, getDrawable(state, GradientDrawable.RECTANGLE, bgColor, radius));
}