下面列出了android.view.View#setBackgroundDrawable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void apply(View view) {
if (view==null){
return;
}
if (RES_TYPE_NAME_COLOR.equals(attrValueTypeName)) {
view.setBackgroundColor(AndroidSkin.getInstance().getSkinColor(
attrValueTypeName, attrValueRefName, attrValueRefId));
} else if (RES_TYPE_NAME_DRAWABLE.equals(attrValueTypeName)) {
Drawable bg = AndroidSkin.getInstance().getSkinDrawable(
attrValueTypeName, attrValueRefName, attrValueRefId);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(bg);
} else {
view.setBackgroundDrawable(bg);
}
}
//LogUtils.d(TAG, "view apply ,id "+attrValueRefId+" " + attrValueTypeName+" "+attrValueRefName );
}
public void setCurrentItem(int item) {
if (this.mViewPager != null) {
if (!(item == -1 || this.mSelectedTabIndex == item)) {
this.mViewPager.setCurrentItem(item);
}
this.mSelectedTabIndex = item;
int tabCount = this.mTabLayout.getChildCount();
int i = 0;
while (i < tabCount) {
View child = this.mTabLayout.getChildAt(i);
boolean isSelected = i == item;
child.setSelected(isSelected);
if (isSelected) {
animateToTab(item);
((TabView) child).setTextColor(this.mContext.getResources().getColor(2131493202));
child.setBackgroundResource(2130839011);
} else {
((TabView) child).setTextColor(this.mContext.getResources().getColor(2131493237));
child.setBackgroundDrawable(null);
}
i++;
}
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int type = getItemViewType(position);
View v = null;
if (convertView != null) {
Log.d("mobeta", "using convertView");
v = convertView;
} else if (type != SECTION_DIV) {
Log.d("mobeta", "inflating normal item");
v = mInflater.inflate(R.layout.drag_sort_listview_list_item_bg_handle, parent, false);
v.setBackgroundDrawable(getBGDrawable(type));
} else {
Log.d("mobeta", "inflating section divider");
v = mInflater.inflate(R.layout.drag_sort_listview_section_div, parent, false);
}
if (type != SECTION_DIV) {
// bind data
((TextView) v).setText(mData.get(dataPosition(position)));
}
return v;
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
// test transition
View v = findViewById(R.id.test_transition);
TransitionDrawable drawable = (TransitionDrawable) v.getBackground();
drawable.startTransition(1000);
// test scale
View testScale = findViewById(R.id.test_scale);
ScaleDrawable testScaleDrawable = (ScaleDrawable) testScale.getBackground();
testScaleDrawable.setLevel(10);
// test clip
ImageView testClip = (ImageView) findViewById(R.id.test_clip);
ClipDrawable testClipDrawable = (ClipDrawable) testClip.getDrawable();
testClipDrawable.setLevel(8000);
// test custom drawable
View testCustomDrawable = findViewById(R.id.test_custom_drawable);
CustomDrawable customDrawable = new CustomDrawable(Color.parseColor("#0ac39e"));
testCustomDrawable.setBackgroundDrawable(customDrawable);
}
}
public void addSubItem(int id, View view, int width, int height) {
createPopupLayout();
view.setLayoutParams(new LinearLayout.LayoutParams(width, height));
popupLayout.addView(view);
view.setTag(id);
view.setOnClickListener(view1 -> {
if (popupWindow != null && popupWindow.isShowing()) {
if (processedPopupClick) {
return;
}
processedPopupClick = true;
popupWindow.dismiss(allowCloseAnimation);
}
if (parentMenu != null) {
parentMenu.onItemClick((Integer) view1.getTag());
} else if (delegate != null) {
delegate.onItemClick((Integer) view1.getTag());
}
});
view.setBackgroundDrawable(Theme.getSelectorDrawable(false));
}
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void setBackground(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(drawable);
} else {
view.setBackgroundDrawable(drawable);
}
}
/**
* Create the view for this fragment, using the arguments given to it.
*/
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello_world, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText(mLabel != null ? mLabel : "(no label)");
tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
return v;
}
public static void setBackgroundCompat(View v, Drawable d) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
v.setBackgroundDrawable(d);
} else {
v.setBackground(d);
}
}
public DialogSheet setRoundedCorners(boolean roundedCorners) {
if (!roundedCorners) {
View bgView = bottomSheetDialog.findViewById(R.id.mainDialogContainer);
if (bgView != null)
bgView.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.dialog_sheet_main_background));
}
return this;
}
@Override
public void process(final View view, String resName, ResourceManager resourceManager, boolean isInUiThread) {
final Drawable drawable = resourceManager.getDrawable(resName);
if (drawable != null) {
if (isInUiThread) {
view.setBackgroundDrawable(drawable);
} else {
UITaskManager.getInstance().post(new Runnable() {
@Override
public void run() {
view.setBackgroundDrawable(drawable);
}
});
}
} else {
try {
final int color = resourceManager.getColor(resName);
if (isInUiThread) {
view.setBackgroundColor(color);
} else {
UITaskManager.getInstance().post(new Runnable() {
@Override
public void run() {
view.setBackgroundColor(color);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Sets background, independence of API Level
*
* @param view target
* @param drawable background
*/
public static void setBackground(View view, Drawable drawable){
if(Build.VERSION.SDK_INT > 16) {
view.setBackground(drawable);
}else{
view.setBackgroundDrawable(drawable);
}
}
@SuppressWarnings("deprecation")
public static void setBackground(final @NonNull View v, final @Nullable Drawable drawable) {
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
v.setBackground(drawable);
} else {
v.setBackgroundDrawable(drawable);
}
}
/**
* Sets a background {@link Drawable} on a view.
*
* @param view
* @param drawable
*/
@SuppressWarnings("deprecation")
public static void setBackgroundDrawable(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackgroundJB(view, drawable);
} else {
view.setBackgroundDrawable(drawable);
}
}
/**
* 将图片设置到View上
*
* @param bitmap
* @param view
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) @SuppressWarnings("deprecation")
private void setImageToView(final Bitmap bitmap, final View view) {
if (view != null) {
if (view instanceof ImageView) {
((ImageView) view).setImageBitmap(bitmap);
} else {
if (Build.VERSION.SDK_INT > 15) {
view.setBackground(new BitmapDrawable(view.getResources(), bitmap));
} else {
view.setBackgroundDrawable(new BitmapDrawable(view.getResources(), bitmap));
}
}
}
}
public static void setBackground(View view, Drawable background) {
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
SDK16.setBackground(view, background);
} else {
view.setBackgroundDrawable(background);
}
}
public SettingsItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.settings_item, null);
mContainerView = itemView;
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
addView(itemView, lp);
itemView.setBackgroundDrawable(getBackground());
mTitleTextView = (TextView) itemView.findViewById(R.id.settings_item_title);
mSubTitleTextView = (TextView) itemView.findViewById(R.id.settings_item_sub_title);
mSwitchButton = (SwitchButton) itemView.findViewById(R.id.settings_item_switch_btn);
mOnclickImageView = (ImageView) itemView.findViewById(R.id.settings_item_arrow);
mInfoTextView = (TextView) itemView.findViewById(R.id.settings_item_info);
mSelectImageView = (ImageView) itemView.findViewById(R.id.settings_item_select);
mTitleContainer = itemView.findViewById(R.id.title_container);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SettingsItem, 0, 0);
String title = a.getString(R.styleable.SettingsItem_item_title);
String subTitle = a.getString(R.styleable.SettingsItem_item_subtitle);
String info = a.getString(R.styleable.SettingsItem_item_info);
mType = a.getInt(R.styleable.SettingsItem_item_type, 1);
mSelected = a.getBoolean(R.styleable.SettingsItem_item_select, false);
int lineMargin = a.getDimensionPixelSize(R.styleable.SettingsItem_item_line_margin, 0);
a.recycle();
setTitle(title);
setSubTitle(subTitle);
setInfo(info);
setType(mType);
View view = new View(getContext());
view.setBackgroundColor(0xffe5e5e5);
lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, 1);
lp.gravity = Gravity.BOTTOM;
// int marging = getResources().getDimensionPixelOffset(R.dimen.settings_item_margin);
lp.setMargins(lineMargin, 0, lineMargin, 0);
addView(view, lp);
}
/**
* android.support.v4.view.ViewCompat SHOULD include this once and for all!!
* But it doesn't...
*
* @param view View
* @param background DrawableBackground
*/
@SuppressWarnings("deprecation")
public static void setBackground(View view, Drawable background) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
UiCompatNotCrash.setBackground(view, background);
} else {
view.setBackgroundDrawable(background);
}
}
private void inflateTitleBar(ViewGroup view) {
ViewStub stub = (ViewStub) view.findViewById(R.id.com_facebook_picker_title_bar_stub);
if (stub != null) {
View titleBar = stub.inflate();
final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.BELOW, R.id.com_facebook_picker_title_bar);
listView.setLayoutParams(layoutParams);
if (titleBarBackground != null) {
titleBar.setBackgroundDrawable(titleBarBackground);
}
doneButton = (Button) view.findViewById(R.id.com_facebook_picker_done_button);
if (doneButton != null) {
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logAppEvents(true);
appEventsLogged = true;
if (onDoneButtonClickedListener != null) {
onDoneButtonClickedListener.onDoneButtonClicked(PickerFragment.this);
}
}
});
if (getDoneButtonText() != null) {
doneButton.setText(getDoneButtonText());
}
if (doneButtonBackground != null) {
doneButton.setBackgroundDrawable(doneButtonBackground);
}
}
titleTextView = (TextView) view.findViewById(R.id.com_facebook_picker_title);
if (titleTextView != null) {
if (getTitleText() != null) {
titleTextView.setText(getTitleText());
}
}
}
}
public static void tintWidget(View view, int color) {
Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground());
DrawableCompat.setTint(wrappedDrawable.mutate(), color);
view.setBackgroundDrawable(wrappedDrawable);
}
private void setBackgroundCompat(View view, Drawable d) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
view.setBackground(d);
else view.setBackgroundDrawable(d);
}