下面列出了android.view.View#setBackgroundColor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
v = LayoutInflater.from(MainActivity.this).inflate(R.layout.row_drawer, null);
v.setOnClickListener(this);
}
v.setTag(position);
Tab tab = (Tab)getItem(position);
((TextView)v).setText(tab.toString());
if(tab == mSelectedTab) {
v.setBackgroundColor(ThemeManager.getInstance().getCurrentTheme() == 0 ? mBackgroundColorLight : mBackgroundColorDark);
((TextView)v).setTextColor(0xFFFFFFFF);
}
else {
v.setBackgroundResource(0);
((TextView)v).setTextColor(ThemeManager.getInstance().getCurrentTheme() == 0 ? mTextColorLight : mTextColorDark);
}
return v;
}
@Override
public void updateViewScaled(TransitionController controller, View target, float scaledProgress) {
//source: http://stackoverflow.com/questions/18216285/android-animate-color-change-from-color-to-color
final float[] from = new float[3],
to = new float[3];
Color.colorToHSV(fromColor, from);
Color.colorToHSV(toColor, to);
final float[] hsv = new float[3];
// Transition along each axis of HSV (hue, saturation, value)
hsv[0] = from[0] + (to[0] - from[0]) * scaledProgress;
hsv[1] = from[1] + (to[1] - from[1]) * scaledProgress;
hsv[2] = from[2] + (to[2] - from[2]) * scaledProgress;
target.setBackgroundColor(Color.HSVToColor(hsv));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View background = findViewById(R.id.coordinator_layout);
View.OnClickListener onClickChangeBackgroundColor = new View.OnClickListener() {
@Override
public void onClick(View view) {
background.setBackgroundColor(
Color.argb(255, r.nextInt(255), r.nextInt(255), r.nextInt(255)));
}
};
background.setOnClickListener(onClickChangeBackgroundColor);
findViewById(R.id.button_the_first).setOnClickListener(onClickChangeBackgroundColor);
findViewById(R.id.button_the_second).setOnClickListener(onClickChangeBackgroundColor);
findViewById(R.id.button_the_third).setOnClickListener(onClickChangeBackgroundColor);
}
/**
* 设置状态栏颜色(5.0以下无半透明效果,不建议使用)
*
* @param activity 需要设置的 activity
* @param color 状态栏颜色值
*/
@Deprecated
public static void setColorDiff(Activity activity, @ColorInt int color) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
}
transparentStatusBar(activity);
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
// 移除半透明矩形,以免叠加
View fakeStatusBarView = contentView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
if (fakeStatusBarView != null) {
if (fakeStatusBarView.getVisibility() == View.GONE) {
fakeStatusBarView.setVisibility(View.VISIBLE);
}
fakeStatusBarView.setBackgroundColor(color);
} else {
contentView.addView(createStatusBarView(activity, color));
}
setRootView(activity);
}
private void setupStatusBarView(Context context, ViewGroup decorViewGroup) {
mStatusBarTintView = new View(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, mConfig.getStatusBarHeight());
params.gravity = Gravity.TOP;
if (mNavBarAvailable && !mConfig.isNavigationAtBottom()) {
params.rightMargin = mConfig.getNavigationBarWidth();
}
mStatusBarTintView.setLayoutParams(params);
mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);
mStatusBarTintView.setVisibility(View.GONE);
decorViewGroup.addView(mStatusBarTintView);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = new ManageChatUserCell(mContext, 2, true);
view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
((ManageChatUserCell) view).setDelegate((cell, click) -> {
TLObject object = getItem((Integer) cell.getTag());
if (object instanceof TLRPC.ChatParticipant) {
TLRPC.ChatParticipant participant = (TLRPC.ChatParticipant) getItem((Integer) cell.getTag());
return createMenuForParticipant(participant, !click);
} else {
return false;
}
});
return new RecyclerListView.Holder(view);
}
@Override
public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.settings_section_layout, parent, false);
convertView.setBackgroundColor(0xffffffff);
}
TextView textView = (TextView) convertView.findViewById(R.id.settings_section_text);
if (selectedContacts.size() == 1) {
textView.setText(selectedContacts.size() + " " + LocaleController.getString("MEMBER", R.string.MEMBER));
} else {
textView.setText(selectedContacts.size() + " " + LocaleController.getString("MEMBERS", R.string.MEMBERS));
}
return convertView;
}
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) {
LinearLayout childLayout = (LinearLayout) this.mTabLayout.getChildAt(i);
for (int j = 0; j < childLayout.getChildCount(); j++) {
boolean isSelected = i == item;
View view = childLayout.getChildAt(j);
if (view instanceof TabView) {
view.setSelected(isSelected);
if (isSelected) {
animateToTab(item);
((TabView) view).setTextColor(this.mContext.getResources().getColor(2131493202));
} else {
((TabView) view).setTextColor(this.mContext.getResources().getColor(2131493237));
}
} else if (isSelected) {
view.setBackgroundColor(this.mContext.getResources().getColor(2131493202));
} else {
view.setBackgroundDrawable(null);
}
}
i++;
}
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
// we have to set the background color at this point otherwise we loose the ripple effect
view.setBackgroundColor(ThemeUtils.getThemeColor(getContext(), R.attr.odyssey_color_background));
return view;
}
/**
* 显示SnackBar
*/
public void show() {
final View view = mParent.get();
if (view == null) {
return;
}
if (mMessageColor != DEFAULT_COLOR) {
SpannableString spannableString = new SpannableString(mMessage);
ForegroundColorSpan colorSpan = new ForegroundColorSpan(mMessageColor);
spannableString.setSpan(colorSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mWeakReference = new WeakReference<>(Snackbar.make(view, spannableString, mDuration));
} else {
mWeakReference = new WeakReference<>(Snackbar.make(view, mMessage, mDuration));
}
final Snackbar snackbar = mWeakReference.get();
final View snackView = snackbar.getView();
if (mBgResource != -1) {
snackView.setBackgroundResource(mBgResource);
} else if (mBgColor != DEFAULT_COLOR) {
snackView.setBackgroundColor(mBgColor);
}
if (mBottomMargin != 0) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) snackView.getLayoutParams();
params.bottomMargin = mBottomMargin;
}
if (mActionText.length() > 0 && mActionListener != null) {
if (mActionTextColor != DEFAULT_COLOR) {
snackbar.setActionTextColor(mActionTextColor);
}
snackbar.setAction(mActionText, mActionListener);
}
snackbar.show();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.item_navigation_drawer, null);
}
NavigationDrawerItem p = getItem(position);
if (p != null && v != null) {
View col = v.findViewById(R.id.drawer_item_color);
ImageView ico = (ImageView) v.findViewById(R.id.drawer_item_icon);
TextView name = (TextView) v.findViewById(R.id.drawer_item_name);
FontManager.handleFontTags(v);
col.setBackgroundColor(Utils.color(p.getColor()));
ico.setImageResource(p.getIcon());
name.setText(p.getName());
}
return v;
}
private void showDivider(boolean darkColorScheme) {
View divider = findViewById(R.id.divider);
if (divider != null) divider.setVisibility(View.VISIBLE);
if (darkColorScheme && divider != null) {
divider.setBackgroundColor(getResources().getColor(R.color.black_with_10));
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View res = inflater.inflate(R.layout.fragment_recycler_list, container, false);
list = (RecyclerView) res.findViewById(R.id.listView);
list.setLayoutManager(new LinearLayoutManager(getActivity()));
emptyView = (TextView) res.findViewById(R.id.emptyView);
emptyView.setText(R.string.blocked_loading);
res.setBackgroundColor(ActorSDK.sharedActor().style.getBackyardBackgroundColor());
emptyView.setTextColor(ActorSDK.sharedActor().style.getTextSecondaryColor());
adapter = new BlockedAdapter(new ArrayList<User>(), new BlockedAdapter.OnBlockedClickListener() {
@Override
public void onClick(UserVM u) {
execute(messenger().unblockUser(u.getId())
.then(new Consumer<Void>() {
@Override
public void apply(Void aVoid) {
checkBlockedList();
}
}));
}
});
list.setAdapter(adapter);
checkBlockedList();
return res;
}
/**
* 初始化DkDropDownMenu
*
* @param tabTexts
* @param popupViews
* @param contentView
*/
public void setDropDownMenu(@NonNull List<String> tabTexts, @NonNull List<View> popupViews,
@NonNull View contentView) {
if (tabTexts.size() != popupViews.size()) {
throw new IllegalArgumentException("params not match, tabTexts.size() should be equal" +
" popupViews.size()");
}
for (int i = 0; i < tabTexts.size(); i++) {
addTab(tabTexts, i);
}
containerView.addView(contentView, 0);
maskView = new View(getContext());
maskView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams
.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
maskView.setBackgroundColor(maskColor);
maskView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
closeMenu();
}
});
containerView.addView(maskView, 1);
maskView.setVisibility(GONE);
popupMenuViews = new FrameLayout(getContext());
popupMenuViews.setVisibility(GONE);
containerView.addView(popupMenuViews, 2);
for (int i = 0; i < popupViews.size(); i++) {
if (popupViews.get(i).getLayoutParams() == null) {
popupViews.get(i).setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
popupMenuViews.addView(popupViews.get(i), i);
}
}
/**
* Update the provided preview layout.
* @param previewLayout the layout to update
*/
protected void updatePreview( View previewLayout )
{
View previewBackground = previewLayout.findViewById(R.id.widgetframe_inner);
if (previewBackground != null)
{
SuntimesTheme.ThemeBackground background = (SuntimesTheme.ThemeBackground)spinBackground.getSelectedItem();
if (background != null)
{
if (background.supportsCustomColors())
previewBackground.setBackgroundColor(chooseColorBackground.getColor());
else previewBackground.setBackgroundResource(background.getResID());
int[] padding = choosePadding.getPaddingPixels(this);
previewBackground.setPadding(padding[0], padding[1], padding[2], padding[3]);
}
}
TextView previewTitle = (TextView)previewLayout.findViewById(R.id.text_title);
if (previewTitle != null)
{
String displayText = editDisplay.getText().toString().trim();
String titleText = (displayText.isEmpty() ? chooseName.getThemeName() : displayText);
previewTitle.setVisibility(View.VISIBLE);
previewTitle.setTextColor(chooseColorTitle.getColor());
boolean boldText = checkTitleBold.isChecked();
if (boldText)
previewTitle.setText(SuntimesUtils.createBoldSpan(null, titleText, titleText));
else previewTitle.setText(titleText);
updateSizeFromChooser(previewTitle, chooseTitleSize);
}
updatePreview_sun(previewLayout);
updatePreview_moon(previewLayout);
updatePreview_clock(previewLayout);
int displayed = preview.getDisplayedChild();
if (displayed == PREVIEWID_SUNPOS_3x1)
updatePreview_position0(previewLayout);
else if (displayed == PREVIEWID_SUNPOS_3x2)
updatePreview_position1(previewLayout);
//updatePreview_solstice(previewLayout); // TODO
}
@Override
public void set(View view, int color) {
view.setBackgroundColor(color);
}
private void init() {
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(WheelUtils.dip2px(mContext, 20), 0, WheelUtils.dip2px(mContext, 20), 0);
mTitle = new TextView(mContext);
mTitle.setTextColor(WheelConstants.DIALOG_WHEEL_COLOR);
mTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
mTitle.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams titleParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
WheelUtils.dip2px(mContext, 50));
layout.addView(mTitle, titleParams);
mLine1 = new View(mContext);
mLine1.setBackgroundColor(WheelConstants.DIALOG_WHEEL_COLOR);
LinearLayout.LayoutParams lineParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
WheelUtils.dip2px(mContext, 2));
layout.addView(mLine1, lineParams);
mWheelView = new WheelView(mContext);
mWheelView.setSkin(WheelView.Skin.Holo);
mWheelView.setWheelAdapter(new ArrayWheelAdapter(mContext));
mStyle = new WheelView.WheelViewStyle();
mStyle.textColor = Color.GRAY;
mStyle.selectedTextZoom = 1.2f;
mWheelView.setStyle(mStyle);
mWheelView.setOnWheelItemSelectedListener((position, text) -> {
mSelectedPos = position;
mSelectedText = text;
});
ViewGroup.MarginLayoutParams wheelParams =
new ViewGroup.MarginLayoutParams(LinearLayout.LayoutParams
.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(mWheelView, wheelParams);
mLine2 = new View(mContext);
mLine2.setBackgroundColor(WheelConstants.DIALOG_WHEEL_COLOR);
LinearLayout.LayoutParams line2Params =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
WheelUtils.dip2px(mContext, 1f));
layout.addView(mLine2, line2Params);
mButton = new TextView(mContext);
mButton.setTextColor(WheelConstants.DIALOG_WHEEL_COLOR);
mButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
mButton.setGravity(Gravity.CENTER);
mButton.setClickable(true);
mButton.setOnClickListener(this);
mButton.setText("OK");
LinearLayout.LayoutParams buttonParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
WheelUtils.dip2px(mContext, 45));
layout.addView(mButton, buttonParams);
mDialog = new AlertDialog.Builder(mContext).create();
mDialog.setView(layout);
mDialog.setCanceledOnTouchOutside(false);
}
/**
* Programmatically build up the view hierarchy to avoid the need for resources.
*
* @return View hierarchy
*/
private View generateHierarchy(Context context) {
Resources resources = getResources();
LayoutParams params;
int fivePx = dpToPx(5, resources);
int tenPx = dpToPx(10, resources);
int twentyPx = dpToPx(20, resources);
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(
0,
ViewGroup.LayoutParams.WRAP_CONTENT,
1f);
tableLayoutParams.setMargins(0, 0, fivePx, 0);
LinearLayout seekWrapper;
FrameLayout root = new FrameLayout(context);
params = createLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPx(300, resources));
root.setLayoutParams(params);
FrameLayout container = new FrameLayout(context);
params = createMatchParams();
params.setMargins(0, twentyPx, 0, 0);
container.setLayoutParams(params);
container.setBackgroundColor(Color.argb(100, 0, 0, 0));
root.addView(container);
mSpringSelectorSpinner = new Spinner(context, Spinner.MODE_DIALOG);
params = createMatchWrapParams();
params.gravity = Gravity.TOP;
params.setMargins(tenPx, tenPx, tenPx, 0);
mSpringSelectorSpinner.setLayoutParams(params);
container.addView(mSpringSelectorSpinner);
LinearLayout linearLayout = new LinearLayout(context);
params = createMatchWrapParams();
params.setMargins(0, 0, 0, dpToPx(80, resources));
params.gravity = Gravity.BOTTOM;
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(LinearLayout.VERTICAL);
container.addView(linearLayout);
seekWrapper = new LinearLayout(context);
params = createMatchWrapParams();
params.setMargins(tenPx, tenPx, tenPx, twentyPx);
seekWrapper.setPadding(tenPx, tenPx, tenPx, tenPx);
seekWrapper.setLayoutParams(params);
seekWrapper.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(seekWrapper);
mTensionSeekBar = new SeekBar(context);
mTensionSeekBar.setLayoutParams(tableLayoutParams);
seekWrapper.addView(mTensionSeekBar);
mTensionLabel = new TextView(getContext());
mTensionLabel.setTextColor(mTextColor);
params = createLayoutParams(
dpToPx(50, resources),
ViewGroup.LayoutParams.MATCH_PARENT);
mTensionLabel.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
mTensionLabel.setLayoutParams(params);
mTensionLabel.setMaxLines(1);
seekWrapper.addView(mTensionLabel);
seekWrapper = new LinearLayout(context);
params = createMatchWrapParams();
params.setMargins(tenPx, tenPx, tenPx, twentyPx);
seekWrapper.setPadding(tenPx, tenPx, tenPx, tenPx);
seekWrapper.setLayoutParams(params);
seekWrapper.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(seekWrapper);
mFrictionSeekBar = new SeekBar(context);
mFrictionSeekBar.setLayoutParams(tableLayoutParams);
seekWrapper.addView(mFrictionSeekBar);
mFrictionLabel = new TextView(getContext());
mFrictionLabel.setTextColor(mTextColor);
params = createLayoutParams(dpToPx(50, resources), ViewGroup.LayoutParams.MATCH_PARENT);
mFrictionLabel.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
mFrictionLabel.setLayoutParams(params);
mFrictionLabel.setMaxLines(1);
seekWrapper.addView(mFrictionLabel);
View nub = new View(context);
params = createLayoutParams(dpToPx(60, resources), dpToPx(40, resources));
params.gravity = Gravity.TOP | Gravity.CENTER;
nub.setLayoutParams(params);
nub.setOnTouchListener(new OnNubTouchListener());
nub.setBackgroundColor(Color.argb(255, 0, 164, 209));
root.addView(nub);
return root;
}
private void setBackgroundAlpha(View view, float alpha, int baseColor) {
int a = Math.min(255, Math.max(0, (int) (alpha * 255))) << 24;
int rgb = 0x00ffffff & baseColor;
view.setBackgroundColor(a + rgb);
}
@Override
public EasyRVHolder setBackgroundColor(int viewId, int color) {
View view = getView(viewId);
view.setBackgroundColor(color);
return this;
}