下面列出了android.widget.FrameLayout#setBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initBottomSheet() {
mBottomSheetView = new FrameLayout(getContext());
mBottomSheetView.setLayoutParams(generateDefaultLayoutParams());
mBottomSheetView.setBackground(createBottomSheetBackgroundDrawable());
mBottomSheetView.setPadding(
mBottomSheetView.getPaddingLeft(),
((int) mConfig.getExtraPaddingTop()),
mBottomSheetView.getPaddingRight(),
((int) mConfig.getExtraPaddingBottom())
);
// Creating the actual sheet content view
final View createdSheetView = Preconditions.checkNonNull(onCreateSheetContentView(getContext()));
// adding the created views
mBottomSheetView.addView(createdSheetView);
addView(mBottomSheetView);
}
private void initializeLayout(Context context) {
mViews = new LinkedHashSet<>();
FrameLayout gridLayout = new FrameLayout(context);
gridLayout.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_dotted_grid));
gridLayout.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT));
mBackground = new FrameLayout(context);
mBackground.addView(gridLayout);
mBackground.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT));
mBackground.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_solid_border));
mBackground.setVisibility(INVISIBLE);
addView(mBackground);
// now set the child clipping to false
setClipChildren(false);
}
public MessageInternalUserTextViewHolder(View itemView, CustomSettings customSettings) {
super(itemView, customSettings);
avatar = (ImageView) itemView.findViewById(R.id.message_user_text_image_view_avatar);
carrot = (ImageView) itemView.findViewById(R.id.message_user_text_image_view_carrot);
initials = (TextView) itemView.findViewById(R.id.message_user_text_text_view_initials);
text = (TextView) itemView.findViewById(R.id.message_user_text_text_view_text);
timestamp = (TextView) itemView.findViewById(R.id.message_user_text_text_view_timestamp);
avatarContainer = (ViewGroup) itemView.findViewById(R.id.message_user_text_view_group_avatar);
bubble = (FrameLayout) itemView.findViewById(R.id.message_user_text_view_group_bubble);
Drawable drawable = ContextCompat.getDrawable(itemView.getContext(), R.drawable.shape_rounded_rectangle_white);
// Drawable drawable = itemView.getContext().getDrawable();
drawable.setColorFilter(customSettings.localBubbleBackgroundColor, PorterDuff.Mode.SRC_ATOP);
bubble.setBackground(drawable);
carrot.setColorFilter(customSettings.localBubbleBackgroundColor);
text.setTextColor(customSettings.localBubbleTextColor);
timestamp.setTextColor(customSettings.timestampColor);
}
public MessageExternalUserTextViewHolder(View itemView, final CustomSettings customSettings) {
super(itemView, customSettings);
avatar = (ImageView) itemView.findViewById(R.id.message_scout_text_image_view_avatar);
carrot = (ImageView) itemView.findViewById(R.id.message_scout_text_image_view_carrot);
text = (TextView) itemView.findViewById(R.id.message_scout_text_text_view_text);
timestamp = (TextView) itemView.findViewById(R.id.message_scout_text_text_view_timestamp);
avatarContainer = (ViewGroup) itemView.findViewById(R.id.message_scout_text_image_view_avatar_group);
initials = (TextView) itemView.findViewById(R.id.message_scout_text_text_view_initials);
bubble = (FrameLayout) itemView.findViewById(R.id.message_scout_text_view_group_bubble);
Drawable drawable = ContextCompat.getDrawable(itemView.getContext(), R.drawable.shape_rounded_rectangle_white);
// Drawable drawable = itemView.getContext().getDrawable();
drawable.setColorFilter(customSettings.externalBubbleBackgroundColor, PorterDuff.Mode.SRC_ATOP);
bubble.setBackground(drawable);
carrot.setColorFilter(customSettings.externalBubbleBackgroundColor);
text.setTextColor(customSettings.externalBubbleTextColor);
timestamp.setTextColor(customSettings.timestampColor);
}
/**
* 显示水印,铺满整个页面
*
* @param activity 活动
* @param text 水印
*/
public void show(Activity activity, String text) {
WatermarkDrawable drawable = new WatermarkDrawable();
drawable.mText = text;
drawable.mTextColor = mTextColor;
drawable.mTextSize = mTextSize;
drawable.mRotation = mRotation;
ViewGroup rootView = activity.findViewById(android.R.id.content);
FrameLayout layout = new FrameLayout(activity);
layout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
layout.setBackground(drawable);
rootView.addView(layout);
}
/**
* 创建自定义 Toast View
*
* @param message 文本消息
* @return View
*/
private static View createTextToastView(String message) {
// 画圆角矩形背景
float rc = dp2px(6);
RoundRectShape shape = new RoundRectShape(new float[]{rc, rc, rc, rc, rc, rc, rc, rc}, null, null);
ShapeDrawable drawable = new ShapeDrawable(shape);
drawable.getPaint().setColor(Color.argb(225, 240, 240, 240));
drawable.getPaint().setStyle(Paint.Style.FILL);
drawable.getPaint().setAntiAlias(true);
drawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
// 创建View
FrameLayout layout = new FrameLayout(Utils.getContext());
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setPadding(dp2px(16), dp2px(12), dp2px(16), dp2px(12));
layout.setBackground(drawable);
TextView textView = new TextView(Utils.getContext());
textView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
textView.setTextSize(15);
textView.setText(message);
textView.setLineSpacing(dp2px(4), 1f);
textView.setTextColor(Color.BLACK);
layout.addView(textView);
return layout;
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private void selectCard(boolean Selected, FrameLayout Card)
{
// Use new API beyond Jelly Bean but keep compatibility with ICS
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
Card.setBackgroundDrawable(Selected ? mRes.getDrawable(R.drawable.card_bg_selected) : mRes.getDrawable(R.drawable.card_bg));
}
else {
Card.setBackground(Selected ? mRes.getDrawable(R.drawable.card_bg_selected) : mRes.getDrawable(R.drawable.card_bg));
}
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private void selectCard(boolean Selected, FrameLayout Card)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
Card.setBackgroundDrawable(Selected ? mRes.getDrawable(R.drawable.card_bg_selected) : mRes.getDrawable(R.drawable.card_bg));
else
Card.setBackground(Selected ? mRes.getDrawable(R.drawable.card_bg_selected) : mRes.getDrawable(R.drawable.card_bg));
}
public static void setBackroundTint(FrameLayout frameLayout, int color) {
Drawable buttonDrawable = frameLayout.getBackground();
buttonDrawable = DrawableCompat.wrap(buttonDrawable);
DrawableCompat.setTint(buttonDrawable, color);
frameLayout.setBackground(buttonDrawable);
}
/**
* コンストラクタ
*
* @param context Context
*/
TrashView(Context context) {
super(context);
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mMetrics = new DisplayMetrics();
mWindowManager.getDefaultDisplay().getMetrics(mMetrics);
mAnimationHandler = new AnimationHandler(this);
mIsEnabled = true;
mParams = new WindowManager.LayoutParams();
mParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
mParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
mParams.type = OVERLAY_TYPE;
mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
mParams.format = PixelFormat.TRANSLUCENT;
// INFO:Windowの原点のみ左下に設定
mParams.gravity = Gravity.LEFT | Gravity.BOTTOM;
// 各種Viewの設定
// TrashViewに直接貼り付けられるView(このViewを介さないと、削除Viewと背景Viewのレイアウトがなぜか崩れる)
mRootView = new FrameLayout(context);
mRootView.setClipChildren(false);
// 削除アイコンのルートView
mTrashIconRootView = new FrameLayout(context);
mTrashIconRootView.setClipChildren(false);
mFixedTrashIconView = new ImageView(context);
mActionTrashIconView = new ImageView(context);
// 背景View
mBackgroundView = new FrameLayout(context);
mBackgroundView.setAlpha(0.0f);
final GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{0x00000000, 0x50000000});
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
mBackgroundView.setBackgroundDrawable(gradientDrawable);
} else {
mBackgroundView.setBackground(gradientDrawable);
}
// 背景Viewの貼り付け
final LayoutParams backgroundParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (BACKGROUND_HEIGHT * mMetrics.density));
backgroundParams.gravity = Gravity.BOTTOM;
mRootView.addView(mBackgroundView, backgroundParams);
// アクションアイコンの貼り付け
final LayoutParams actionTrashIconParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
actionTrashIconParams.gravity = Gravity.CENTER;
mTrashIconRootView.addView(mActionTrashIconView, actionTrashIconParams);
// 固定アイコンの貼付け
final LayoutParams fixedTrashIconParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
fixedTrashIconParams.gravity = Gravity.CENTER;
mTrashIconRootView.addView(mFixedTrashIconView, fixedTrashIconParams);
// 削除アイコンの貼り付け
final LayoutParams trashIconParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
trashIconParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
mRootView.addView(mTrashIconRootView, trashIconParams);
// TrashViewに貼り付け
addView(mRootView);
// 初回描画処理用
getViewTreeObserver().addOnPreDrawListener(this);
}
public void initLeftBg()
{
left_btn = (FrameLayout) this.findViewById(R.id.left_layout);
left_btn.setBackground(ImageUtils.createBGSelector(wContext.get().getResources().getColor(R.color.atom_ui_primary_color),
wContext.get().getResources().getColor(R.color.atom_ui_button_primary_color_pressed)));
}
/**
* コンストラクタ
*
* @param context Context
*/
TrashView(Context context) {
super(context);
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mMetrics = new DisplayMetrics();
mWindowManager.getDefaultDisplay().getMetrics(mMetrics);
mAnimationHandler = new AnimationHandler(this);
mIsEnabled = true;
mParams = new WindowManager.LayoutParams();
mParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
mParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
mParams.type = OVERLAY_TYPE;
mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
mParams.format = PixelFormat.TRANSLUCENT;
// INFO:Windowの原点のみ左下に設定
mParams.gravity = Gravity.LEFT | Gravity.BOTTOM;
// 各種Viewの設定
// TrashViewに直接貼り付けられるView(このViewを介さないと、削除Viewと背景Viewのレイアウトがなぜか崩れる)
mRootView = new FrameLayout(context);
mRootView.setClipChildren(false);
// 削除アイコンのルートView
mTrashIconRootView = new FrameLayout(context);
mTrashIconRootView.setClipChildren(false);
mFixedTrashIconView = new ImageView(context);
mActionTrashIconView = new ImageView(context);
// 背景View
mBackgroundView = new FrameLayout(context);
mBackgroundView.setAlpha(0.0f);
final GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{0x00000000, 0x50000000});
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
mBackgroundView.setBackgroundDrawable(gradientDrawable);
} else {
mBackgroundView.setBackground(gradientDrawable);
}
// 背景Viewの貼り付け
final FrameLayout.LayoutParams backgroundParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (BACKGROUND_HEIGHT * mMetrics.density));
backgroundParams.gravity = Gravity.BOTTOM;
mRootView.addView(mBackgroundView, backgroundParams);
// アクションアイコンの貼り付け
final FrameLayout.LayoutParams actionTrashIconParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
actionTrashIconParams.gravity = Gravity.CENTER;
mTrashIconRootView.addView(mActionTrashIconView, actionTrashIconParams);
// 固定アイコンの貼付け
final FrameLayout.LayoutParams fixedTrashIconParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
fixedTrashIconParams.gravity = Gravity.CENTER;
mTrashIconRootView.addView(mFixedTrashIconView, fixedTrashIconParams);
// 削除アイコンの貼り付け
final FrameLayout.LayoutParams trashIconParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
trashIconParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
mRootView.addView(mTrashIconRootView, trashIconParams);
// TrashViewに貼り付け
addView(mRootView);
// 初回描画処理用
getViewTreeObserver().addOnPreDrawListener(this);
}