下面列出了android.widget.TextView#setCompoundDrawables ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final DialogMenuItem item = dialogMenuItemList.get(position);
TextView menuItemView = new TextView(context);
menuItemView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
menuItemView.setSingleLine(true);
menuItemView.setTextColor(contentTextColor);
menuItemView.setTextSize(TypedValue.COMPLEX_UNIT_SP, contentTextSize);
menuItemView.setBackgroundDrawable(getButtonBackground(false, false, false));
if (item.icon != 0) {
Drawable drawable = context.getResources().getDrawable(item.icon);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
menuItemView.setCompoundDrawables(drawable, null, null, null);
menuItemView.setCompoundDrawablePadding(dp2px(5));
}
menuItemView.setPadding(dp2px(3), dp2px(3), dp2px(3), dp2px(3));
menuItemView.setText(item.itemName);
return menuItemView;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = mInflater.inflate(R.layout.wallpaper_picker_third_party_item, parent, false);
} else {
view = convertView;
}
WallpaperPickerActivity.setWallpaperItemPaddingToZero((FrameLayout) view);
ResolveInfo info = mThirdPartyWallpaperPickers.get(position).mResolveInfo;
TextView label = (TextView) view.findViewById(R.id.wallpaper_item_label);
label.setText(info.loadLabel(mPackageManager));
Drawable icon = info.loadIcon(mPackageManager);
icon.setBounds(new Rect(0, 0, mIconSize, mIconSize));
label.setCompoundDrawables(null, icon, null, null);
return view;
}
private void setNumberAsImage(boolean b, TextView number, int vtc) {
if (b) {
Drawable drawable;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable = activity.getDrawable(R.drawable.ic_volume_up_black_24dp);
if (drawable != null) {
drawable.setTint(vtc);
}
} else {
drawable = activity.getResources().getDrawable(R.drawable.ic_volume_up_black_24dp);
}
if (drawable != null) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
number.setCompoundDrawables(drawable, null, null, null);
number.setText("");
}
} else {
number.setTextColor(vtc);
number.setCompoundDrawables(null, null, null, null);
}
}
public ListHolder(View itemView) {
super(itemView);
leftText = (TextView) itemView.findViewById(R.id.left_text);
centerText = (TextView) itemView.findViewById(R.id.center_text);
rightText = (TextView) itemView.findViewById(R.id.right_text);
content = (FontTextView) itemView.findViewById(R.id.content);
bottomText = (TextView) itemView.findViewById(R.id.bottom_text);
thumb = (LoadImageView) itemView.findViewById(R.id.thumb);
reply = (MarqueeReplyView) itemView.findViewById(R.id.reply);
bottom = itemView.findViewById(R.id.bottom);
thumb.setOnClickListener(this);
Drawable drawable = DrawableManager.getDrawable(ListActivity.this, R.drawable.v_comment_multiple_outline_x16);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
bottomText.setCompoundDrawables(drawable, null, null, null);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = getInflater().inflate(R.layout.item_search_hot, parent, false);
}
TextView textHot = ViewHolder.get(convertView, R.id.text_keyword);
textHot.setText(getList().get(position));
Drawable drawable = getContext().getResources().getDrawable(resIds[position]);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
textHot.setCompoundDrawables(drawable, null, null, null);
textHot.setCompoundDrawablePadding(AppUtils.dip2Px(getContext(), 5));
textHot.setTextColor(getContext().getResources().getColor(colorIds[position]));
return convertView;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = mInflater.inflate(R.layout.wallpaper_picker_third_party_item, parent, false);
} else {
view = convertView;
}
ResolveInfo info = mThirdPartyWallpaperPickers.get(position).mResolveInfo;
TextView label = (TextView) view.findViewById(R.id.wallpaper_item_label);
label.setText(info.loadLabel(mPackageManager));
Drawable icon = info.loadIcon(mPackageManager);
icon.setBounds(new Rect(0, 0, mIconSize, mIconSize));
label.setCompoundDrawables(null, icon, null, null);
return view;
}
/**
* 设置数据集合伴随drawable-icon
* @param datas 数据
* @param drawable 图标
* @param size 图标尺寸
* @param direction 图标位于文字方位
*/
public void setDatasWithDrawableIcon(List<String> datas, Drawable drawable,int size, int direction){
this.mDatas = datas;
if (DisplayUtils.isEmpty(mDatas)){
return;
}
mViewFlipper.removeAllViews();
for (int i = 0; i < mDatas.size(); i++) {
TextView textView = new TextView(getContext());
setTextView(textView,i);
textView.setCompoundDrawablePadding(8);
float scale = getResources().getDisplayMetrics().density;// 屏幕密度 ;
int muchDp = (int) (size * scale + 0.5f);
drawable.setBounds(0, 0, muchDp, muchDp);
if (direction==Gravity.LEFT){
textView.setCompoundDrawables(drawable,null,null , null);//左边
}else if (direction==Gravity.TOP){
textView.setCompoundDrawables(null,drawable,null , null);//顶部
}else if (direction==Gravity.RIGHT){
textView.setCompoundDrawables(null,null, drawable, null);//右边
}else if (direction==Gravity.BOTTOM){
textView.setCompoundDrawables(null,null, null, drawable);//底部
}
LinearLayout linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);//水平方向
linearLayout.setGravity(mGravity);//子view显示位置跟随TextView
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.
LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(textView,param);
mViewFlipper.addView(linearLayout,i);//添加子view,并标识子view位置
}
}
@SuppressWarnings("deprecation")
@Nullable
@Override
public View onCreateView3(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.scene_label_list, container, false);
mRecyclerView = (EasyRecyclerView) ViewUtils.$$(view, R.id.recycler_view);
TextView tip = (TextView) ViewUtils.$$(view, R.id.tip);
mViewTransition = new ViewTransition(mRecyclerView, tip);
Context context = getContext2();
AssertUtils.assertNotNull(context);
Drawable drawable = DrawableManager.getVectorDrawable(context, R.drawable.big_search);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
tip.setCompoundDrawables(null, drawable, null, null);
tip.setText(R.string.no_quick_search);
// drag & drop manager
RecyclerViewDragDropManager dragDropManager = new RecyclerViewDragDropManager();
dragDropManager.setDraggingItemShadowDrawable(
(NinePatchDrawable) context.getResources().getDrawable(R.drawable.shadow_8dp));
RecyclerView.Adapter adapter = new QuickSearchAdapter();
adapter.setHasStableIds(true);
adapter = dragDropManager.createWrappedAdapter(adapter); // wrap for dragging
mAdapter = adapter;
final GeneralItemAnimator animator = new DraggableItemAnimator();
mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
mRecyclerView.setAdapter(adapter);
mRecyclerView.setItemAnimator(animator);
dragDropManager.attachRecyclerView(mRecyclerView);
updateView();
return view;
}
public static void setLeftDrawable(TextView textView, Drawable leftDrawable) {
setDrawableBounds(leftDrawable);
Drawable top = textView.getCompoundDrawables()[1];
Drawable right = textView.getCompoundDrawables()[2];
Drawable bottom = textView.getCompoundDrawables()[3];
textView.setCompoundDrawables(leftDrawable, top, right, bottom);
}
public TSnackbar setIconLeft(@DrawableRes int drawableRes, float sizeDp) {
final TextView tv = mView.getMessageView();
Drawable drawable = ContextCompat.getDrawable(mContext, drawableRes);
if (drawable != null) {
drawable = fitDrawable(drawable, (int) convertDpToPixel(sizeDp, mContext));
} else {
throw new IllegalArgumentException("resource_id is not a valid drawable!");
}
final Drawable[] compoundDrawables = tv.getCompoundDrawables();
tv.setCompoundDrawables(drawable, compoundDrawables[1], compoundDrawables[2], compoundDrawables[3]);
return this;
}
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cart_detail, container, false);
ItemInfo itemInfo = Constants.ITEMS_FOR_SALE[mItemId];
TextView itemName = (TextView) view.findViewById(R.id.text_item_name);
itemName.setText(itemInfo.name);
Drawable itemImage = getResources().getDrawable(itemInfo.imageResourceId);
int imageSize = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_size);
int actualWidth = itemImage.getIntrinsicWidth();
int actualHeight = itemImage.getIntrinsicHeight();
int scaledHeight = imageSize;
int scaledWidth = (int) (((float) actualWidth / actualHeight) * scaledHeight);
itemImage.setBounds(0, 0, scaledWidth, scaledHeight);
itemName.setCompoundDrawables(itemImage, null, null, null);
TextView itemPrice = (TextView) view.findViewById(R.id.text_item_price);
itemPrice.setText(Util.formatPrice(getActivity(), itemInfo.priceMicros));
TextView shippingCost = (TextView) view.findViewById(R.id.text_shipping_price);
TextView tax = (TextView) view.findViewById(R.id.text_tax_price);
TextView total = (TextView) view.findViewById(R.id.text_total_price);
if ((mItemId == Constants.PROMOTION_ITEM) && getApplication().isAddressValidForPromo()) {
shippingCost.setText(Util.formatPrice(getActivity(), 0L));
} else {
shippingCost.setText(Util.formatPrice(getActivity(), itemInfo.shippingPriceMicros));
}
tax.setText(Util.formatPrice(getActivity(), itemInfo.taxMicros));
total.setText(Util.formatPrice(getActivity(), itemInfo.getTotalPrice()));
return view;
}
@BindingAdapter(value = {"android:drawableLeft"})
public static void setDrawableLeft(TextView textView, int resID) {
Drawable leftDrawable = textView.getContext().getResources().getDrawable(resID);
leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
textView.setCompoundDrawables(
leftDrawable,
null,
null,
null);
}
/**
* @see android.widget.TextView#setCompoundDrawablesRelative(Drawable, Drawable, Drawable,
* Drawable)
*/
public static void setCompoundDrawablesRelative(TextView textView, Drawable start, Drawable top,
Drawable end, Drawable bottom) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
// On JB MR1, due to a platform bug, setCompoundDrawablesRelative() is a no-op if the
// view has ever been measured. As a workaround, use setCompoundDrawables() directly.
// See: http://crbug.com/368196 and http://crbug.com/361709
boolean isRtl = isLayoutRtl(textView);
textView.setCompoundDrawables(isRtl ? end : start, top, isRtl ? start : end, bottom);
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
textView.setCompoundDrawablesRelative(start, top, end, bottom);
} else {
textView.setCompoundDrawables(start, top, end, bottom);
}
}
private void setDrawableLeft(TextView textView, int resId) {
Drawable drawable = ContextCompat.getDrawable(this, resId);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
textView.setCompoundDrawables(drawable, null, null, null);
}
private void setTopDrawables(TextView textView,int resId){
Drawable drawable = getResources().getDrawable(resId);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小,否则不显示
textView.setCompoundDrawables(null,drawable,null,null);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// User super class to create the View
View v = super.getView(position, convertView, parent);
TextView tv = (TextView) v.findViewById(android.R.id.text1);
Object handler = getItem(position);
int iconSize = UIHelpers.dpToPx(32, getContext());
if (handler instanceof IOrganization) {
IOrganization org = (IOrganization) handler;
tv.setText(org.organizationName);
//todo load the org's icon from bundle or remote site
//Drawable icon = a.getResources().getDrawable(
// R.drawable.ic_share_iba);
//icon.setBounds(0, 0, iconSize, iconSize);
//tv.setCompoundDrawables(icon, null, null, null);
tv.setCompoundDrawablePadding(UIHelpers.dpToPx(10, getContext()));
} else if (handler instanceof HandlerIntent) {
HandlerIntent handlerIntent = (HandlerIntent) handler;
ResolveInfo info = handlerIntent.resolveInfo;
PackageManager pm = getContext().getPackageManager();
tv.setText(info.loadLabel(pm));
Drawable icon = info.loadIcon(pm);
icon.setBounds(0, 0, iconSize, iconSize);
// Put the image on the TextView
tv.setCompoundDrawables(icon, null, null, null);
tv.setCompoundDrawablePadding(UIHelpers.dpToPx(10, getContext()));
}
// Add margin between image and text (support various screen
// densities)
int dp5 = (int) (5 * a.getResources().getDisplayMetrics().density + 0.5f);
tv.setCompoundDrawablePadding(dp5);
return v;
}
@Nullable
@Override
public View onCreateView3(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.scene_history, container, false);
View content = ViewUtils.$$(view, R.id.content);
mRecyclerView = (EasyRecyclerView) ViewUtils.$$(content, R.id.recycler_view);
FastScroller fastScroller = (FastScroller) ViewUtils.$$(content, R.id.fast_scroller);
TextView tip = (TextView) ViewUtils.$$(view, R.id.tip);
mViewTransition = new ViewTransition(content, tip);
Context context = getContext2();
AssertUtils.assertNotNull(context);
Resources resources = context.getResources();
Drawable drawable = DrawableManager.getVectorDrawable(context, R.drawable.big_history);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
tip.setCompoundDrawables(null, drawable, null, null);
RecyclerViewTouchActionGuardManager guardManager = new RecyclerViewTouchActionGuardManager();
guardManager.setInterceptVerticalScrollingWhileAnimationRunning(true);
guardManager.setEnabled(true);
RecyclerViewSwipeManager swipeManager = new RecyclerViewSwipeManager();
mAdapter = new HistoryAdapter();
mAdapter.setHasStableIds(true);
mAdapter = swipeManager.createWrappedAdapter(mAdapter);
mRecyclerView.setAdapter(mAdapter);
final GeneralItemAnimator animator = new SwipeDismissItemAnimator();
animator.setSupportsChangeAnimations(false);
mRecyclerView.setItemAnimator(animator);
AutoStaggeredGridLayoutManager layoutManager = new AutoStaggeredGridLayoutManager(
0, StaggeredGridLayoutManager.VERTICAL);
layoutManager.setColumnSize(resources.getDimensionPixelOffset(Settings.getDetailSizeResId()));
layoutManager.setStrategy(AutoStaggeredGridLayoutManager.STRATEGY_MIN_SIZE);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setSelector(Ripple.generateRippleDrawable(context, !AttrResources.getAttrBoolean(context, R.attr.isLightTheme), new ColorDrawable(Color.TRANSPARENT)));
mRecyclerView.setDrawSelectorOnTop(true);
mRecyclerView.setClipToPadding(false);
mRecyclerView.setOnItemClickListener(this);
mRecyclerView.setOnItemLongClickListener(this);
int interval = resources.getDimensionPixelOffset(R.dimen.gallery_list_interval);
int paddingH = resources.getDimensionPixelOffset(R.dimen.gallery_list_margin_h);
int paddingV = resources.getDimensionPixelOffset(R.dimen.gallery_list_margin_v);
MarginItemDecoration decoration = new MarginItemDecoration(interval, paddingH, paddingV, paddingH, paddingV);
mRecyclerView.addItemDecoration(decoration);
decoration.applyPaddings(mRecyclerView);
guardManager.attachRecyclerView(mRecyclerView);
swipeManager.attachRecyclerView(mRecyclerView);
fastScroller.attachToRecyclerView(mRecyclerView);
HandlerDrawable handlerDrawable = new HandlerDrawable();
handlerDrawable.setColor(AttrResources.getAttrColor(context, R.attr.widgetColorThemeAccent));
fastScroller.setHandlerDrawable(handlerDrawable);
updateLazyList();
updateView(false);
return view;
}
public View showEmptyView(String mErrorToast, int mErrorRedId, boolean showProgress, final Handler.Callback mReload){
//如果找到了,说明布局里已经存在
if(mEmptyView==null){
mEmptyView=$(R.id.empty_layout);
if(mEmptyView==null){
mEmptyView= LayoutInflater.from(mContext).inflate(R.layout.layout_list_empty_view,null);
}
}
VISIBLE(mEmptyView);
TextView error= ViewHolder.get(mEmptyView,R.id.empty_toast);
if(!TextUtils.isEmpty(mErrorToast)){
error.setText(mErrorToast);
}
if(mErrorRedId>0){
ViewUtil.setDrawableToTextTop(mContext,mErrorRedId,error);
}else{
error.setCompoundDrawables(null,null,null,null);
}
ProgressBar progressBar=ViewHolder.get(mEmptyView,R.id.empty_load);
if(showProgress){
VISIBLE(progressBar);
}else{
GONE(progressBar);
}
//重载按钮
View view=ViewHolder.get(mEmptyView,R.id.empty_reload);
if(mReload!=null){
VISIBLE(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mReload.handleMessage(Message.obtain());
}
});
}else{
GONE(view);
}
return mEmptyView;
}
@Override
protected void initView(int layoutID) {
super.initView(layoutID);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (null != getSupportActionBar()) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
mCheckNearby = getView(R.id.check_nearby);
mEditSearch = getView(R.id.edit_search);
mGridHot = getView(R.id.grid_hot);
mListResult = getView(R.id.list_result);
mListHistory = getView(R.id.list_history);
mLayMyCity = getView(R.id.lay_my_city);
mTextCity = getView(R.id.text_city);
mRecycleCity = getView(R.id.recycler_city);
TextView textHeader = new TextView(this);
textHeader.setText("历史记录");
textHeader.setPadding(AppUtils.dip2Px(this, 10), AppUtils.dip2Px(this, 10), AppUtils.dip2Px(this, 10), AppUtils.dip2Px(this, 10));
textHeader.setTextColor(Color.parseColor("#999999"));
textHeader.setTextSize(12);
Drawable drawable = getResources().getDrawable(R.drawable.ic_history_18dp);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
textHeader.setCompoundDrawables(drawable, null, null, null);
textHeader.setCompoundDrawablePadding(AppUtils.dip2Px(this, 5));
mListHistory.addHeaderView(textHeader, null, false);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecycleCity.setLayoutManager(layoutManager);
mTextCity.setOnClickListener(this);
mEditSearch.setOnEditorActionListener(this);
mEditSearch.addTextChangedListener(this);
mListResult.setOnLoadMoreListener(this);
mListResult.setOnItemClickListener(this);
mListHistory.setOnItemClickListener(this);
mGridHot.setOnItemClickListener(this);
mCheckNearby.setOnClickListener(this);
}
/**
* 设置textview左侧图片
* @param context
* @param attention
* @param drawableId
*/
public static void setDrawableLeft(Context context ,TextView attention, int drawableId) {
Drawable drawable = context.getResources().getDrawable(drawableId);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
attention.setCompoundDrawables(drawable, null, null, null);
}