android.widget.TextView#setBackground ( )源码实例Demo

下面列出了android.widget.TextView#setBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View rowView = convertView;

    if(rowView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.dialog_entry_activity, parent, false);
    }

    TextView label = (TextView) rowView.findViewById(R.id.dialog_entry_activity);
    label.setText(this.getItem(position));
    if(position == 0) {
        label.setTypeface(null, Typeface.BOLD | Typeface.ITALIC);
        label.setBackground(ContextCompat.getDrawable(context, R.drawable.border));
    }

    return rowView;
}
 
MyViewHolder(View itemView) {
    super(itemView);
    context = itemView.getContext();
    textView_parentName = itemView.findViewById(R.id.tv_parentName);
    linearLayout_childItems = itemView.findViewById(R.id.ll_child_items);
    linearLayout_childItems.setVisibility(View.GONE);
    int intMaxNoOfChild = 0;
    for (int index = 0; index < dummyParentDataItems.size(); index++) {
        int intMaxSizeTemp = dummyParentDataItems.get(index).getChildDataItems().size();
        if (intMaxSizeTemp > intMaxNoOfChild) intMaxNoOfChild = intMaxSizeTemp;
    }
    for (int indexView = 0; indexView < intMaxNoOfChild; indexView++) {
        TextView textView = new TextView(context);
        textView.setId(indexView);
        textView.setPadding(0, 20, 0, 20);
        textView.setGravity(Gravity.CENTER);
        textView.setBackground(ContextCompat.getDrawable(context, R.drawable.background_sub_module_text));
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        textView.setOnClickListener(this);
        linearLayout_childItems.addView(textView, layoutParams);
    }
    textView_parentName.setOnClickListener(this);
}
 
源代码3 项目: a   文件: FindSecondAdapter.java
@Override
public View getView(FlowLayout parent, int position, final FindKindBean findKindBean) {
    TextView tv = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_flow_find_item,
            parent, false);
    tv.setText(findKindBean.getKindName());

    if(findKindBean.getKindUrl().equals(url)){

        tv.setBackground(parent.getContext().getResources().getDrawable(R.drawable.bg_flow_source_item_selected));
    }
    //Random myRandom = new Random();
    //int ranColor = 0xff000000 | myRandom.nextInt(0x00ffffff);
    //tv.setBackgroundColor(ranColor);
    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(null != onItemClickListener){
                onItemClickListener.itemClick(v,findKindBean);
            }
        }
    });
    return tv;
}
 
@SuppressLint("NewApi")
private void setBackground(Context activity,
		ActionSheetConfig actionSheetConfig, TextView tv, int count,
		int index) {
	if (!TextUtils.isEmpty(actionSheetConfig.title)
			|| actionSheetConfig.titleLayout != null) {
		// 有tips

		if (index == -1) {
			tv.setBackgroundColor(activity.getResources().getColor(
					R.color.transparent));
		} else {
			// item底部背景
			tv.setBackground(activity.getResources().getDrawable(
					R.drawable.actionsheet_wechat_style_button_item));

		}

	} else {
		// item底部背景
		tv.setBackground(activity.getResources().getDrawable(
				R.drawable.actionsheet_wechat_style_button_item));
	}
}
 
源代码5 项目: Musicoco   文件: MainSheetsController.java
private void updateTextAndColor(TextView categoryView, int count, TextView countView, int[] colors) {

        categoryView.setBackgroundColor(colors[2]);
        categoryView.setTextColor(colors[3]);

        GradientDrawable countD = new GradientDrawable();
        countD.setColor(colors[0]);
        countD.setCornerRadius(countView.getHeight() / 2);
        countView.setBackground(countD);
        countView.setTextColor(colors[1]);

        int c = count;
        if (countView == mCountRecent) {
            int recent = activity.getResources().getInteger(R.integer.sheet_recent_count);
            c = count > recent ? recent : c;
        }
        countView.setText(String.valueOf(c));

    }
 
源代码6 项目: VCL-Android   文件: StringPresenter.java
public void onBindViewHolder(ViewHolder viewHolder, Object item) {
    Resources res = viewHolder.view.getContext().getResources();
    TextView tv = (TextView) viewHolder.view;
    tv.setText(item.toString());
    if (res.getString(R.string.preferences).equals(item.toString())) {
        tv.setBackground(res.getDrawable(R.drawable.ic_menu_preferences_big));
    }
    tv.setHeight(res.getDimensionPixelSize(R.dimen.grid_card_thumb_height));
    tv.setWidth(res.getDimensionPixelSize(R.dimen.grid_card_thumb_width));
}
 
源代码7 项目: PhotoEditor   文件: TextStyleBuilder.java
protected void applyBackgroundDrawable(TextView textView, Drawable bg) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        textView.setBackground(bg);
    } else {
        textView.setBackgroundDrawable(bg);
    }
}
 
源代码8 项目: MaterialTabHost   文件: MaterialTabHost.java
/**
 * add new tab with title text
 *
 * @param title title text
 */
public void addTab(CharSequence title) {
    int layoutId = getLayoutId(type);
    TextView tv = (TextView) inflater.inflate(layoutId, tabWidget, false);
    tv.setText(title);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        tv.setBackgroundResource(R.drawable.mth_tab_widget_background_ripple);

    } else {
        // create background using colorControlActivated
        StateListDrawable d = new StateListDrawable();
        d.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(colorControlActivated));
        d.setAlpha(180);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            tv.setBackground(d);
        } else {
            tv.setBackgroundDrawable(d);
        }
    }

    int tabId = tabWidget.getTabCount();

    addTab(newTabSpec(String.valueOf(tabId))
            .setIndicator(tv)
            .setContent(android.R.id.tabcontent));
}
 
源代码9 项目: Telegram   文件: BottomSheet.java
public BottomSheetCell(Context context, int type) {
    super(context);

    currentType = type;
    setBackgroundDrawable(Theme.getSelectorDrawable(false));
    //setPadding(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(16), 0);

    imageView = new ImageView(context);
    imageView.setScaleType(ImageView.ScaleType.CENTER);
    imageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_dialogIcon), PorterDuff.Mode.MULTIPLY));
    addView(imageView, LayoutHelper.createFrame(56, 48, Gravity.CENTER_VERTICAL | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT)));

    textView = new TextView(context);
    textView.setLines(1);
    textView.setSingleLine(true);
    textView.setGravity(Gravity.CENTER_HORIZONTAL);
    textView.setEllipsize(TextUtils.TruncateAt.END);
    if (type == 0) {
        textView.setTextColor(Theme.getColor(Theme.key_dialogTextBlack));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
        addView(textView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL));
    } else if (type == 1) {
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(Theme.getColor(Theme.key_dialogTextBlack));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
        textView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
        addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
    } else if (type == 2) {
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(Theme.getColor(Theme.key_featuredStickers_buttonText));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
        textView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
        textView.setBackground(Theme.createSimpleSelectorRoundRectDrawable(AndroidUtilities.dp(4), Theme.getColor(Theme.key_featuredStickers_addButton), Theme.getColor(Theme.key_featuredStickers_addButtonPressed)));
        addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, 0, 16, 16, 16, 16));
    }
}
 
源代码10 项目: Telegram-FOSS   文件: BottomSheet.java
public BottomSheetCell(Context context, int type) {
    super(context);

    currentType = type;
    setBackgroundDrawable(Theme.getSelectorDrawable(false));
    //setPadding(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(16), 0);

    imageView = new ImageView(context);
    imageView.setScaleType(ImageView.ScaleType.CENTER);
    imageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_dialogIcon), PorterDuff.Mode.MULTIPLY));
    addView(imageView, LayoutHelper.createFrame(56, 48, Gravity.CENTER_VERTICAL | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT)));

    textView = new TextView(context);
    textView.setLines(1);
    textView.setSingleLine(true);
    textView.setGravity(Gravity.CENTER_HORIZONTAL);
    textView.setEllipsize(TextUtils.TruncateAt.END);
    if (type == 0) {
        textView.setTextColor(Theme.getColor(Theme.key_dialogTextBlack));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
        addView(textView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL));
    } else if (type == 1) {
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(Theme.getColor(Theme.key_dialogTextBlack));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
        textView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
        addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
    } else if (type == 2) {
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(Theme.getColor(Theme.key_featuredStickers_buttonText));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
        textView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
        textView.setBackground(Theme.createSimpleSelectorRoundRectDrawable(AndroidUtilities.dp(4), Theme.getColor(Theme.key_featuredStickers_addButton), Theme.getColor(Theme.key_featuredStickers_addButtonPressed)));
        addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, 0, 16, 16, 16, 16));
    }
}
 
源代码11 项目: iMoney   文件: ProductHotFragment.java
@Override
protected void initData(String content) {
    Random random = new Random();

    for (String text : mData) {
        TextView tv = new TextView(getActivity());
        tv.setText(text);
        tv.setTextSize(UIUtils.dp2px(6));

        // 提供边距的对象,并设置到textView中
        ViewGroup.MarginLayoutParams mp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mp.leftMargin = UIUtils.dp2px(8);
        mp.topMargin = UIUtils.dp2px(8);
        mp.rightMargin = UIUtils.dp2px(8);
        mp.bottomMargin = UIUtils.dp2px(8);

        tv.setLayoutParams(mp);

        // 设置背景
        int red = random.nextInt(210);
        int green = random.nextInt(210);
        int blue = random.nextInt(210);

        // 方式一:
        tv.setBackground(DrawUtils.getDrawable(Color.rgb(red, green, blue), UIUtils.dp2px(4)));

        // 方式二:
        tv.setBackground(DrawUtils.getSelector(DrawUtils.getDrawable(Color.rgb(red, green, blue),
                UIUtils.dp2px(4)), DrawUtils.getDrawable(Color.WHITE, UIUtils.dp2px(4))));
        // 当设置了点击事件时,默认textView就是可点击的了
        tv.setOnClickListener(v -> Toast.makeText(
                ProductHotFragment.this.getActivity(), tv.getText(), Toast.LENGTH_SHORT).show());

        // 设置内边距:
        int padding = UIUtils.dp2px(4);
        tv.setPadding(padding, padding, padding, padding);
        flowLayout.addView(tv);
    }
}
 
源代码12 项目: sa-sdk-android   文件: DebugModeSelectDialog.java
private void initView() {
    //标题:SDK 调试模式选择
    TextView debugModeTitle = findViewById(R.id.sensors_analytics_debug_mode_title);
    debugModeTitle.setText("SDK 调试模式选择");

    //取消
    TextView debugModeCancel = findViewById(R.id.sensors_analytics_debug_mode_cancel);
    debugModeCancel.setText("取消");
    debugModeCancel.setOnClickListener(this);

    //开启调试模式(不导入数据)
    TextView debugModeOnly = findViewById(R.id.sensors_analytics_debug_mode_only);
    debugModeOnly.setText("开启调试模式(不导入数据)");
    debugModeOnly.setOnClickListener(this);

    //"开启调试模式(导入数据)"
    TextView debugModeTrack = findViewById(R.id.sensors_analytics_debug_mode_track);
    debugModeTrack.setText("开启调试模式(导入数据)");
    debugModeTrack.setOnClickListener(this);

    String msg = "调试模式已关闭";
    if (currentDebugMode == SensorsDataAPI.DebugMode.DEBUG_ONLY) {
        msg = "当前为 调试模式(不导入数据)";
    } else if (currentDebugMode == SensorsDataAPI.DebugMode.DEBUG_AND_TRACK) {
        msg = "当前为 测试模式(导入数据)";
    }
    TextView debugModeMessage = findViewById(R.id.sensors_analytics_debug_mode_message);
    debugModeMessage.setText(msg);

    //设置按钮点击效果
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        debugModeCancel.setBackground(getDrawable());
        debugModeOnly.setBackground(getDrawable());
        debugModeTrack.setBackground(getDrawable());
    } else {
        debugModeCancel.setBackgroundDrawable(getDrawable());
        debugModeOnly.setBackgroundDrawable(getDrawable());
        debugModeTrack.setBackgroundDrawable(getDrawable());
    }
}
 
源代码13 项目: Mobike   文件: MainActivity.java
private void setMyClickable(TextView tv) {
    mTvAllmobike.setClickable(true);
    mTvMobike.setClickable(true);
    mTvMobikelite.setClickable(true);
    mTvMobikelite.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
    mTvAllmobike.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
    mTvMobike.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

    tv.setClickable(false);
    tv.setBackground(getResources().getDrawable(R.drawable.top_tab_select));
    CURRENT_MOBIKETYPE = Integer.parseInt((String) tv.getTag());
}
 
源代码14 项目: android-login   文件: TextSizeTransition.java
private static Bitmap captureTextBitmap(TextView textView) {
  Drawable background = textView.getBackground();
  textView.setBackground(null);
  int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
  int height = textView.getHeight() - textView.getPaddingTop() - textView.getPaddingBottom();
  if (width == 0 || height == 0) {
    return null;
  }
  Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  canvas.translate(-textView.getPaddingLeft(), -textView.getPaddingTop());
  textView.draw(canvas);
  textView.setBackground(background);
  return bitmap;
}
 
源代码15 项目: TwistyTimer   文件: TimerGraphFragment.java
private void fadeStatTab(TextView tab) {
    tab.setTextColor(ThemeUtils.fetchAttrColor(mContext, R.attr
            .graph_stats_card_text_color_faded));
    tab.setBackground(buttonDrawableFaded);
}
 
源代码16 项目: FimiX8-RE   文件: X8TabItem.java
private void sove() {
    GradientDrawable dd = new GradientDrawable();
    dd.setCornerRadii(new float[]{(float) this.radius, (float) this.radius, (float) this.radius, (float) this.radius, (float) this.radius, (float) this.radius, (float) this.radius, (float) this.radius});
    dd.setStroke(this.lineStroke, this.lineColor);
    if (VERSION.SDK_INT >= 16) {
        setBackground(dd);
    } else {
        setBackgroundDrawable(dd);
    }
    removeAllViews();
    if (this.curIndex >= this.textArr.length || this.curIndex < 0) {
        this.curIndex = 0;
    }
    for (int i = 0; i < this.textArr.length; i++) {
        TextView tv = new TextView(getContext());
        LayoutParams params = new LayoutParams(0, -1);
        if (i > 0) {
            params.leftMargin = this.space;
        }
        GradientDrawable d = getFitGradientDrawable(i);
        if (this.curIndex == i) {
            tv.setTextColor(this.selectTextColor);
            d.setColor(this.selectTabBg);
        } else {
            tv.setTextColor(this.unSelectTextColor);
            d.setColor(this.unSelectTabBg);
        }
        tv.setText(this.textArr[i]);
        tv.setGravity(17);
        tv.setTextSize(0, this.textSize);
        if (VERSION.SDK_INT >= 16) {
            tv.setBackground(d);
        } else {
            tv.setBackgroundDrawable(d);
        }
        params.weight = 1.0f;
        tv.setLayoutParams(params);
        tv.setTag(Integer.valueOf(i));
        tv.setOnClickListener(this);
        addView(tv);
    }
}
 
源代码17 项目: Telegram   文件: ChartHeaderView.java
public ChartHeaderView(Context context) {
    super(context);
    TextPaint textPaint = new TextPaint();
    textPaint.setTextSize(14);
    textPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    textMargin = (int) textPaint.measureText("00 MMM 0000 - 00 MMM 000");

    title = new TextView(context);
    title.setTextSize(15);
    title.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    addView(title, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.START | Gravity.CENTER_VERTICAL, 16, 0, textMargin, 0));

    back = new TextView(context);
    back.setTextSize(15);
    back.setTypeface(Typeface.DEFAULT_BOLD);
    back.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
    addView(back, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.START | Gravity.CENTER_VERTICAL, 8, 0, 8, 0));

    dates = new TextView(context);
    dates.setTextSize(13);
    dates.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    dates.setGravity(Gravity.END | Gravity.CENTER_VERTICAL);
    addView(dates, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.END | Gravity.CENTER_VERTICAL, 16, 0, 16, 0));

    datesTmp = new TextView(context);
    datesTmp.setTextSize(13);
    datesTmp.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    datesTmp.setGravity(Gravity.END | Gravity.CENTER_VERTICAL);
    addView(datesTmp, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.END | Gravity.CENTER_VERTICAL, 16, 0, 16, 0));
    datesTmp.setVisibility(View.GONE);


    back.setVisibility(View.GONE);
    back.setText(LocaleController.getString("ZoomOut", R.string.ZoomOut));
    zoomIcon = ContextCompat.getDrawable(getContext(), R.drawable.stats_zoom);
    back.setCompoundDrawablesWithIntrinsicBounds(zoomIcon, null, null, null);
    back.setCompoundDrawablePadding(AndroidUtilities.dp(4));
    back.setPadding(AndroidUtilities.dp(8), AndroidUtilities.dp(4), AndroidUtilities.dp(8), AndroidUtilities.dp(4));
    back.setBackground(Theme.getRoundRectSelectorDrawable(Theme.getColor(Theme.key_featuredStickers_removeButtonText)));

    datesTmp.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
        datesTmp.setPivotX(datesTmp.getMeasuredWidth() * 0.7f);
        dates.setPivotX(dates.getMeasuredWidth() * 0.7f);
    });
    recolor();
}
 
源代码18 项目: BigApp_Discuz_Android   文件: ActionSheet4IOS.java
@SuppressLint("NewApi")
private void setBackground(Context mContext,
		ActionSheetConfig actionSheetConfig, TextView tv, int count,
		int index) {
	if (!TextUtils.isEmpty(actionSheetConfig.title)
			|| actionSheetConfig.titleLayout != null) {
		// 有tips

		if (index == -1) {
			tv.setBackground(mContext.getResources().getDrawable(
					R.drawable.actionsheet_ios_style_top_selector));
		} else {
			if (index == count) {
				// 最后一条 圆角底部背景
				tv.setBackground(mContext.getResources().getDrawable(
						R.drawable.actionsheet_ios_style_bottom_selector));
			} else {
				// 中间 直角底部背景
				tv.setBackground(mContext.getResources().getDrawable(
						R.drawable.actionsheet_ios_style_middle_selector));
			}
		}

	} else {
		if (count == 1) {
			// actionsheet item只有一个
			tv.setBackground(mContext.getResources().getDrawable(
					R.drawable.actionsheet_ios_style_single_selector));
		} else {
			// actionsheet item有两个(包含两个)以上
			if (index == 1) {
				// 第一条 圆角头部背景
				tv.setBackground(mContext.getResources().getDrawable(
						R.drawable.actionsheet_ios_style_top_selector));

			} else if (index == count) {
				// 最后一条 圆角底部背景
				tv.setBackground(mContext.getResources().getDrawable(
						R.drawable.actionsheet_ios_style_bottom_selector));
			} else {
				// 中间 直角底部背景
				tv.setBackground(mContext.getResources().getDrawable(
						R.drawable.actionsheet_ios_style_middle_selector));
			}
		}
	}
}
 
public RecyclerView.ViewHolder createEmptyViewHolder(ViewGroup parent) {

        LinearLayout linearLayout = new LinearLayout(parent.getContext());
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(lp);

        ImageView imageView = new ImageView(parent.getContext());
        imageView.setImageResource(getEmptyImageResource());
        LinearLayout.LayoutParams ivLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ivLp.gravity = Gravity.CENTER_HORIZONTAL;
        ivLp.topMargin = getTopMargin();
        imageView.setLayoutParams(ivLp);
        linearLayout.addView(imageView);

        TextView textView = new TextView(parent.getContext());
        textView.setText(getEmptyStringResource());
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
        textView.setTextColor(ContextCompat.getColor(parent.getContext(), R.color.color_9E9E9E));
        LinearLayout.LayoutParams tvLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        tvLp.gravity = Gravity.CENTER_HORIZONTAL;
        tvLp.topMargin = DisplayHelper.dpToPx(10);
        textView.setLayoutParams(tvLp);
        linearLayout.addView(textView);

        if (getEmptyActionVisibility() == View.VISIBLE) {
            TextView actionView = new TextView(parent.getContext());
            LinearLayout.LayoutParams actionLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, DisplayHelper.dpToPx(50));
            actionLp.gravity = Gravity.CENTER_HORIZONTAL;
            actionLp.topMargin = DisplayHelper.dpToPx(50);
            actionView.setLayoutParams(actionLp);
            actionView.setText(R.string.add_address);
            actionView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
            actionView.setTextColor(ContextCompat.getColor(parent.getContext(), R.color.color_FFFFFF));
            actionView.setBackground(BackgroundHelper.getButtonBackground(parent.getContext(), R.color.color_002C6D));
            actionView.setGravity(Gravity.CENTER);
            actionView.setPadding(DisplayHelper.dpToPx(40), 0, DisplayHelper.dpToPx(40), 0);
            actionView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onEmptyClick();
                }
            });
            linearLayout.addView(actionView);
        }

        return new StatusViewHolder(linearLayout);
    }
 
源代码20 项目: Dashchan   文件: ClickableToast.java
private ClickableToast(Holder holder) {
	this.holder = holder;
	Context context = holder.context;
	float density = ResourceUtils.obtainDensity(context);
	int innerPadding = (int) (8f * density);
	LayoutInflater inflater = LayoutInflater.from(context);
	View toast1 = inflater.inflate(LAYOUT_ID, null);
	View toast2 = inflater.inflate(LAYOUT_ID, null);
	TextView message1 = toast1.findViewById(android.R.id.message);
	TextView message2 = toast2.findViewById(android.R.id.message);
	View backgroundSource = null;
	Drawable backgroundDrawable = toast1.getBackground();
	if (backgroundDrawable == null) {
		backgroundDrawable = message1.getBackground();
		if (backgroundDrawable == null) {
			View messageParent = (View) message1.getParent();
			if (messageParent != null) {
				backgroundDrawable = messageParent.getBackground();
				backgroundSource = messageParent;
			}
		} else {
			backgroundSource = message1;
		}
	} else {
		backgroundSource = toast1;
	}

	StringBuilder builder = new StringBuilder();
	for (int i = 0; i < 100; i++) builder.append('W'); // Make long text
	message1.setText(builder); // Avoid minimum widths
	int measureSize = (int) (context.getResources().getConfiguration().screenWidthDp * density + 0.5f);
	toast1.measure(View.MeasureSpec.makeMeasureSpec(measureSize, View.MeasureSpec.AT_MOST),
			View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
	toast1.layout(0, 0, toast1.getMeasuredWidth(), toast1.getMeasuredHeight());
	Rect backgroundSourceTotalPadding = getViewTotalPadding(toast1, backgroundSource);
	Rect messageTotalPadding = getViewTotalPadding(toast1, message1);
	messageTotalPadding.left -= backgroundSourceTotalPadding.left;
	messageTotalPadding.top -= backgroundSourceTotalPadding.top;
	messageTotalPadding.right -= backgroundSourceTotalPadding.right;
	messageTotalPadding.bottom -= backgroundSourceTotalPadding.bottom;
	int horizontalPadding = Math.max(messageTotalPadding.left, messageTotalPadding.right) +
			Math.max(message1.getPaddingLeft(), message1.getPaddingRight());
	int verticalPadding = Math.max(messageTotalPadding.top, messageTotalPadding.bottom) +
			Math.max(message1.getPaddingTop(), message1.getPaddingBottom());

	ViewUtils.removeFromParent(message1);
	ViewUtils.removeFromParent(message2);
	LinearLayout linearLayout = new LinearLayout(context);
	linearLayout.setOrientation(LinearLayout.HORIZONTAL);
	linearLayout.setDividerDrawable(new ToastDividerDrawable(0xccffffff, (int) (density + 0.5f)));
	linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
	linearLayout.setDividerPadding((int) (4f * density));
	linearLayout.setTag(this);
	linearLayout.addView(message1, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
	linearLayout.addView(message2, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
	((LinearLayout.LayoutParams) message1.getLayoutParams()).weight = 1f;
	linearLayout.setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);

	partialClickDrawable = new PartialClickDrawable(backgroundDrawable);
	message1.setBackground(null);
	message2.setBackground(null);
	linearLayout.setBackground(partialClickDrawable);
	linearLayout.setOnTouchListener(partialClickDrawable);
	message1.setPadding(0, 0, 0, 0);
	message2.setPadding(innerPadding, 0, 0, 0);
	message1.setSingleLine(true);
	message2.setSingleLine(true);
	message1.setEllipsize(TextUtils.TruncateAt.END);
	message2.setEllipsize(TextUtils.TruncateAt.END);
	container = linearLayout;
	message = message1;
	button = message2;

	windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}
 
 方法所在类
 同类方法