下面列出了android.widget.TextView#setCompoundDrawablesRelative ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void updateStatus(CharSequence text, @AttrRes int attr, Drawable icon) {
if (mViewHolder != null) {
CardView statusCard = (CardView) ((ViewGroup) mViewHolder.itemView).getChildAt(0);
TextView status = statusCard.findViewById(android.R.id.text1);
if (icon != null) {
icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
status.setCompoundDrawablesRelative(icon, null, null, null);
} else {
status.setCompoundDrawablesRelative(null, null, null, null);
}
status.setText(text);
int[] attrs = {attr};
TypedArray a = getContext().obtainStyledAttributes(attrs);
int color = a.getColor(0, 0);
a.recycle();
statusCard.setCardBackgroundColor(color);
}
}
@Override
public View getView(int position, View convertView, ViewGroup container) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_intent_check, container, false);
}
ActivityInfo ai = mInfos.get(position);
TextView title = (TextView) convertView;
title.setText(ai.label);
int iconSize = Resources.getSystem().getDimensionPixelSize(android.R.dimen.app_icon_size);
ai.icon.setBounds(0, 0, iconSize, iconSize);
title.setCompoundDrawablesRelative(ai.icon, null, null, null);
if (title instanceof Checkable) {
Checkable checker = (Checkable) title;
checker.setChecked(appPackages.contains(ai.componentName.getPackageName()));
}
return convertView;
}
@Override
public View getView(int position, View convertView, ViewGroup container) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_intent_check, container, false);
}
ActivityInfo ai = mInfos.get(position);
TextView title = (TextView) convertView;
title.setText(ai.label);
int iconSize = Resources.getSystem().getDimensionPixelSize(android.R.dimen.app_icon_size);
ai.icon.setBounds(0, 0, iconSize, iconSize);
title.setCompoundDrawablesRelative(ai.icon, null, null, null);
if (title instanceof Checkable) {
Checkable checker = (Checkable) title;
checker.setChecked(appPackages.contains(ai.componentName.getPackageName()));
}
return convertView;
}
/**
* @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);
}
}
public static void loadSnapshotsIntoList(
ViewGroup valuesList, Label label, AppAccount appAccount) {
Context context = valuesList.getContext();
SnapshotLabelValue snapshotLabelValue = label.getSnapshotLabelValue();
valuesList.setVisibility(View.VISIBLE);
// Make sure it has the correct number of views, re-using as many as possible.
int childCount = valuesList.getChildCount();
int snapshotsCount = snapshotLabelValue.getSnapshotsCount();
if (childCount < snapshotsCount) {
for (int i = 0; i < snapshotsCount - childCount; i++) {
LayoutInflater.from(context).inflate(R.layout.snapshot_value_details, valuesList);
}
} else if (childCount > snapshotsCount) {
valuesList.removeViews(0, childCount - snapshotsCount);
}
SensorAppearanceProvider sensorAppearanceProvider =
AppSingleton.getInstance(context).getSensorAppearanceProvider(appAccount);
String valueFormat = context.getResources().getString(R.string.data_with_units);
for (int i = 0; i < snapshotsCount; i++) {
GoosciSnapshotValue.SnapshotLabelValue.SensorSnapshot snapshot =
snapshotLabelValue.getSnapshots(i);
ViewGroup snapshotLayout = (ViewGroup) valuesList.getChildAt(i);
GoosciSensorAppearance.BasicSensorAppearance appearance =
snapshot.getSensor().getRememberedAppearance();
TextView sensorName = (TextView) snapshotLayout.findViewById(R.id.sensor_name);
sensorName.setCompoundDrawablesRelative(null, null, null, null);
sensorName.setText(appearance.getName());
String value =
BuiltInSensorAppearance.formatValue(
snapshot.getValue(), appearance.getPointsAfterDecimal());
((TextView) snapshotLayout.findViewById(R.id.sensor_value))
.setText(String.format(valueFormat, value, appearance.getUnits()));
loadLargeDrawable(appearance, sensorAppearanceProvider, snapshotLayout, snapshot.getValue());
}
}
/**
* @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);
}
}
public static void changeColorForDrawable(Context context, TextView textView, int color, PorterDuff.Mode mode) {
if (textView == null) { return; }
Drawable[] compoundDrawablesRelative = textView.getCompoundDrawablesRelative();
Drawable[] newDrawablesRelative = changeColorForDrawable(context, color, mode, compoundDrawablesRelative);
textView.setCompoundDrawablesRelative(newDrawablesRelative[0], newDrawablesRelative[1], newDrawablesRelative[2], newDrawablesRelative[3]);
}