下面列出了android.view.ViewGroup#setBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 初始化提示的布局
*/
private void initLayout() {
mMainLayout = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.widget_hint_layout, this, false);
mImageView = mMainLayout.findViewById(R.id.iv_hint_icon);
mTextView = mMainLayout.findViewById(R.id.iv_hint_text);
if (mMainLayout.getBackground() == null) {
// 默认使用 windowBackground 作为背景
TypedArray ta = getContext().obtainStyledAttributes(new int[]{android.R.attr.windowBackground});
mMainLayout.setBackground(ta.getDrawable(0));
ta.recycle();
}
addView(mMainLayout);
}
private void addMenuItem(ViewGroup menu, String text, int menuIndex) {
int[] colors = {android.R.color.holo_red_light,
android.R.color.holo_green_light,
android.R.color.darker_gray,
android.R.color.holo_orange_light,
android.R.color.holo_blue_light};
Context context = menu.getContext();
ViewGroup item = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.menu_item, menu, false);
((TextView) item.findViewById(R.id.item_text)).setText(text);
CircularSplashView ic = (CircularSplashView) item.findViewById(R.id.circle);
ic.setSplash(getBitmap(context));
ic.setSplashColor(context.getResources().getColor(colors[new Random().nextInt(colors.length)]));
item.setOnClickListener(getMenuItemCLick(menuIndex, context.getResources().getColor(colors[new Random().nextInt(colors.length)])));
int padding = (int) context.getResources().getDimension(R.dimen.menu_item_height_padding);
menu.addView(item, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) context.getResources().getDimension(R.dimen.menu_item_height) + padding));
item.setPadding(0, 0, 0, padding);
item.setBackground(context.getResources().getDrawable(R.drawable.menu_btn, null));
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mTextAndBackground = (ViewGroup) findViewById(R.id.text_and_background);
ColorDrawable colorBackground = (ColorDrawable) mTextAndBackground.getBackground();
mBackgroundColor = colorBackground.getColor();
RippleDrawable rippleBackground = new RippleDrawable(ColorStateList.valueOf(
ThemeUtils.getAttrColor(getContext(), android.R.attr.colorControlHighlight)),
colorBackground, null);
mTextAndBackground.setBackground(rippleBackground);
mTitleView = (TextView) mTextAndBackground.findViewById(R.id.title);
mTextView = (TextView) mTextAndBackground.findViewById(R.id.text);
}
private void initializeMenuline(final ViewGroup item) {
item.setBackgroundColor(mItemBGColor);
if (mStyle.isRoundedTabs()) {
item.setBackground(mStyle.getBgDrawableFor(item, mItemBGColor));
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)mMain.getResources().getDimension(R.dimen.action_menu_width), ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(12,13,12,13);
item.setLayoutParams(lp);
mShortcutActionsList.addView(item);
if (mAnimationDuration>0) {
if (mShortcutActionsList.getChildCount() > mOldNum) {
item.setVisibility(View.GONE);
}
item.setScaleY(.1f);
item.animate()
.scaleY(1f)
.setDuration(mAnimationDuration)
.setStartDelay(mShortcutActionsList.getChildCount() * 10 + 10)
.withStartAction(new Runnable() {
@Override
public void run() {
item.setVisibility(View.VISIBLE);
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
ViewGroup view = (ViewGroup)getWindow().getDecorView();
removeBackgroundRecursively(view);
view.setBackground(getResources().getDrawable(R.drawable.settings_background));
}
public static void recycleViewGroupAndChildViews(ViewGroup viewGroup, boolean recycleBitmap) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof WebView) {
WebView webView = (WebView) child;
webView.loadUrl("about:blank");
webView.stopLoading();
continue;
}
if (child instanceof ViewGroup) {
recycleViewGroupAndChildViews((ViewGroup) child, true);
continue;
}
if (child instanceof ImageView) {
ImageView iv = (ImageView) child;
Drawable drawable = iv.getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
if (recycleBitmap && bitmap != null) {
bitmap.recycle();
}
}
iv.setImageBitmap(null);
iv.setBackground(null);
continue;
}
child.setBackground(null);
}
viewGroup.setBackground(null);
}
public static void recycleViewGroupAndChildViews(ViewGroup viewGroup, boolean recycleBitmap) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof WebView) {
WebView webView = (WebView) child;
webView.loadUrl("about:blank");
webView.stopLoading();
continue;
}
if (child instanceof ViewGroup) {
recycleViewGroupAndChildViews((ViewGroup) child, true);
continue;
}
if (child instanceof ImageView) {
ImageView iv = (ImageView) child;
Drawable drawable = iv.getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
if (recycleBitmap && bitmap != null) {
bitmap.recycle();
}
}
iv.setImageBitmap(null);
iv.setBackground(null);
continue;
}
child.setBackground(null);
}
viewGroup.setBackground(null);
}
public DatePickerCalendarDelegate(DatePicker delegator, Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(delegator, context);
final Locale locale = mCurrentLocale;
mCurrentDate = Calendar.getInstance(locale);
mTempDate = Calendar.getInstance(locale);
mMinDate = Calendar.getInstance(locale);
mMaxDate = Calendar.getInstance(locale);
mMinDate.set(DEFAULT_START_YEAR, Calendar.JANUARY, 1);
mMaxDate.set(DEFAULT_END_YEAR, Calendar.DECEMBER, 31);
final Resources res = mDelegator.getResources();
final TypedArray a = mContext.obtainStyledAttributes(attrs,
R.styleable.DatePicker, defStyleAttr, defStyleRes);
final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
final int layoutResourceId = a.getResourceId(
R.styleable.DatePicker_internalLayout, R.layout.date_picker_material);
// Set up and attach container.
mContainer = (ViewGroup) inflater.inflate(layoutResourceId, mDelegator, false);
mContainer.setSaveFromParentEnabled(false);
mDelegator.addView(mContainer);
// Set up header views.
final ViewGroup header = mContainer.findViewById(R.id.date_picker_header);
mHeaderYear = header.findViewById(R.id.date_picker_header_year);
mHeaderYear.setOnClickListener(mOnHeaderClickListener);
mHeaderMonthDay = header.findViewById(R.id.date_picker_header_date);
mHeaderMonthDay.setOnClickListener(mOnHeaderClickListener);
// For the sake of backwards compatibility, attempt to extract the text
// color from the header month text appearance. If it's set, we'll let
// that override the "real" header text color.
ColorStateList headerTextColor = null;
@SuppressWarnings("deprecation")
final int monthHeaderTextAppearance = a.getResourceId(
R.styleable.DatePicker_headerMonthTextAppearance, 0);
if (monthHeaderTextAppearance != 0) {
final TypedArray textAppearance = mContext.obtainStyledAttributes(null,
ATTRS_TEXT_COLOR, 0, monthHeaderTextAppearance);
final ColorStateList legacyHeaderTextColor = textAppearance.getColorStateList(0);
headerTextColor = applyLegacyColorFixes(legacyHeaderTextColor);
textAppearance.recycle();
}
if (headerTextColor == null) {
headerTextColor = a.getColorStateList(R.styleable.DatePicker_headerTextColor);
}
if (headerTextColor != null) {
mHeaderYear.setTextColor(headerTextColor);
mHeaderMonthDay.setTextColor(headerTextColor);
}
// Set up header background, if available.
if (a.hasValueOrEmpty(R.styleable.DatePicker_headerBackground)) {
header.setBackground(a.getDrawable(R.styleable.DatePicker_headerBackground));
}
a.recycle();
// Set up picker container.
mAnimator = mContainer.findViewById(R.id.animator);
// Set up day picker view.
mDayPickerView = mAnimator.findViewById(R.id.date_picker_day_picker);
mDayPickerView.setFirstDayOfWeek(mFirstDayOfWeek);
mDayPickerView.setMinDate(mMinDate.getTimeInMillis());
mDayPickerView.setMaxDate(mMaxDate.getTimeInMillis());
mDayPickerView.setDate(mCurrentDate.getTimeInMillis());
mDayPickerView.setOnDaySelectedListener(mOnDaySelectedListener);
// Set up year picker view.
mYearPickerView = mAnimator.findViewById(R.id.date_picker_year_picker);
mYearPickerView.setRange(mMinDate, mMaxDate);
mYearPickerView.setYear(mCurrentDate.get(Calendar.YEAR));
mYearPickerView.setOnYearSelectedListener(mOnYearSelectedListener);
// Set up content descriptions.
mSelectDay = res.getString(R.string.select_day);
mSelectYear = res.getString(R.string.select_year);
// Initialize for current locale. This also initializes the date, so no
// need to call onDateChanged.
onLocaleChanged(mCurrentLocale);
setCurrentView(VIEW_MONTH_DAY);
}
public DatePickerCalendarDelegate(DatePicker delegator, Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(delegator, context);
final Locale locale = mCurrentLocale;
mCurrentDate = Calendar.getInstance(locale);
mTempDate = Calendar.getInstance(locale);
mMinDate = Calendar.getInstance(locale);
mMaxDate = Calendar.getInstance(locale);
mMinDate.set(DEFAULT_START_YEAR, Calendar.JANUARY, 1);
mMaxDate.set(DEFAULT_END_YEAR, Calendar.DECEMBER, 31);
final Resources res = mDelegator.getResources();
final TypedArray a = mContext.obtainStyledAttributes(attrs,
R.styleable.DatePicker, defStyleAttr, defStyleRes);
final LayoutInflater inflater = LayoutInflater.from(context);
final int layoutResourceId = a.getResourceId(
R.styleable.DatePicker_dtp_internalLayout, R.layout.date_picker_material);
// Set up and attach container.
mContainer = (ViewGroup) inflater.inflate(layoutResourceId, mDelegator, false);
mDelegator.addView(mContainer);
// Set up header views.
final ViewGroup header = (ViewGroup) mContainer.findViewById(R.id.date_picker_header);
mHeaderYear = (TextView) header.findViewById(R.id.date_picker_header_year);
mHeaderYear.setOnClickListener(mOnHeaderClickListener);
mHeaderMonthDay = (TextView) header.findViewById(R.id.date_picker_header_date);
mHeaderMonthDay.setOnClickListener(mOnHeaderClickListener);
// For the sake of backwards compatibility, attempt to extract the text
// color from the header month text appearance. If it's set, we'll let
// that override the "real" header text color.
ColorStateList headerTextColor = null;
if (headerTextColor == null) {
//headerTextColor = a.getColorStateList(R.styleable.DatePicker_headerTextColor);
headerTextColor = Utils.getColorStateList(context, a, R.styleable.DatePicker_dtp_headerTextColor);
}
if (headerTextColor != null) {
mHeaderYear.setTextColor(headerTextColor);
mHeaderMonthDay.setTextColor(headerTextColor);
}
// Set up header background, if available.
//if (a.hasValueOrEmpty(R.styleable.DatePicker_headerBackground)) {
Drawable headerBg = a.getDrawable(R.styleable.DatePicker_headerBackground);
if (headerBg != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
header.setBackground(headerBg);
} else {
header.setBackgroundDrawable(headerBg);
}
}
a.recycle();
// Set up picker container.
mAnimator = mContainer.findViewById(R.id.animator);
// Set up day picker view.
mDayPickerView = mAnimator.findViewById(R.id.date_picker_day_picker);
mDayPickerView.setFirstDayOfWeek(mFirstDayOfWeek);
mDayPickerView.setMinDate(mMinDate.getTimeInMillis());
mDayPickerView.setMaxDate(mMaxDate.getTimeInMillis());
mDayPickerView.setDate(mCurrentDate.getTimeInMillis());
mDayPickerView.setOnDaySelectedListener(mOnDaySelectedListener);
// Set up year picker view.
mYearPickerView = mAnimator.findViewById(R.id.date_picker_year_picker);
mYearPickerView.setRange(mMinDate, mMaxDate);
mYearPickerView.setYear(mCurrentDate.get(Calendar.YEAR));
mYearPickerView.setOnYearSelectedListener(mOnYearSelectedListener);
// Set up content descriptions.
mSelectDay = res.getString(R.string.select_day);
mSelectYear = res.getString(R.string.select_year);
// Initialize for current locale. This also initializes the date, so no
// need to call onDateChanged.
onLocaleChanged(mCurrentLocale);
setCurrentView(VIEW_MONTH_DAY);
}
public void showLongPressCling(boolean showWelcome) {
mIsVisible = true;
ViewGroup root = (ViewGroup) mLauncher.findViewById(R.id.launcher);
View cling = mInflater.inflate(R.layout.longpress_cling, root, false);
cling.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mLauncher.showOverviewMode(true);
dismissLongPressCling();
return true;
}
});
final ViewGroup content = (ViewGroup) cling.findViewById(R.id.cling_content);
mInflater.inflate(showWelcome ? R.layout.longpress_cling_welcome_content
: R.layout.longpress_cling_content, content);
content.findViewById(R.id.cling_dismiss_longpress_info).setOnClickListener(this);
if (TAG_CROP_TOP_AND_SIDES.equals(content.getTag())) {
Drawable bg = new BorderCropDrawable(mLauncher.getResources().getDrawable(R.drawable.cling_bg),
true, true, true, false);
content.setBackground(bg);
}
root.addView(cling);
if (showWelcome) {
// This is the first cling being shown. No need to animate.
return;
}
// Animate
content.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
ObjectAnimator anim;
if (TAG_CROP_TOP_AND_SIDES.equals(content.getTag())) {
content.setTranslationY(-content.getMeasuredHeight());
anim = LauncherAnimUtils.ofFloat(content, "translationY", 0);
} else {
content.setScaleX(0);
content.setScaleY(0);
PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1);
PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1);
anim = LauncherAnimUtils.ofPropertyValuesHolder(content, scaleX, scaleY);
}
anim.setDuration(SHOW_CLING_DURATION);
anim.setInterpolator(new LogDecelerateInterpolator(100, 0));
anim.start();
}
});
}
public void showLongPressCling(boolean showWelcome) {
ViewGroup root = (ViewGroup) mLauncher.findViewById(R.id.launcher);
View cling = mInflater.inflate(R.layout.longpress_cling, root, false);
cling.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mLauncher.getWorkspace().enterOverviewMode();
dismissLongPressCling();
return true;
}
});
final ViewGroup content = (ViewGroup) cling.findViewById(R.id.cling_content);
mInflater.inflate(showWelcome ? R.layout.longpress_cling_welcome_content
: R.layout.longpress_cling_content, content);
content.findViewById(R.id.cling_dismiss_longpress_info).setOnClickListener(this);
if (TAG_CROP_TOP_AND_SIDES.equals(content.getTag())) {
Drawable bg = new BorderCropDrawable(mLauncher.getResources().getDrawable(R.drawable.cling_bg),
true, true, true, false);
content.setBackground(bg);
}
root.addView(cling);
if (showWelcome) {
// This is the first cling being shown. No need to animate.
return;
}
// Animate
content.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
ObjectAnimator anim;
if (TAG_CROP_TOP_AND_SIDES.equals(content.getTag())) {
content.setTranslationY(-content.getMeasuredHeight());
anim = LauncherAnimUtils.ofFloat(content, "translationY", 0);
} else {
content.setScaleX(0);
content.setScaleY(0);
PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1);
PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1);
anim = LauncherAnimUtils.ofPropertyValuesHolder(content, scaleX, scaleY);
}
anim.setDuration(SHOW_CLING_DURATION);
anim.setInterpolator(new LogDecelerateInterpolator(100, 0));
anim.start();
}
});
}