下面列出了android.widget.CheckedTextView#setCheckMarkDrawable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void setTint(@NonNull CheckedTextView textView, @ColorInt int color) {
ColorStateList sl = new ColorStateList(new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
}, new int[]{
ThemeHelper.resolveColor(textView.getContext(), R.attr.colorControlNormal),
color
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textView.setCheckMarkTintList(sl);
} else {
Drawable d = DrawableCompat.wrap(ContextCompat.getDrawable(textView.getContext(), R.drawable.abc_btn_radio_material));
DrawableCompat.setTintList(d, sl);
textView.setCheckMarkDrawable(d);
}
}
public static void setTint(@NonNull CheckedTextView textView, @ColorInt int color) {
ColorStateList sl = new ColorStateList(new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
}, new int[]{
ThemeHelper.resolveColor(textView.getContext(), R.attr.colorControlNormal),
color
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textView.setCheckMarkTintList(sl);
} else {
Drawable d = DrawableCompat.wrap(ContextCompat.getDrawable(textView.getContext(), R.drawable.abc_btn_check_material));
DrawableCompat.setTintList(d, sl);
textView.setCheckMarkDrawable(d);
}
}
/**
* {@inheritDoc}
*/
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
View row = inflater.inflate(R.layout.image_list_item, parent, false);
ImageView imageView = row.findViewById(R.id.image);
imageView.setImageResource(resourceIds[position]);
CheckedTextView checkedTextView = row.findViewById(R.id.check);
checkedTextView.setText(getItem(position));
checkedTextView.setCheckMarkDrawable(Resources.getSystem().getDrawable(mRadioDrawableId));
if (position == index) {
checkedTextView.setChecked(true);
}
return row;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View itemView = super.getView(position, convertView, parent);
if (itemView == null) {
return null;
}
final CheckedTextView checkedTextView = (CheckedTextView)itemView;
if (hasCheckbox(position)) {
checkedTextView.setEnabled(isEnabled(position));
} else {
checkedTextView.setCheckMarkDrawable(null);
checkedTextView.setTextColor(getResources().getColor(R.color.accent));
}
return checkedTextView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (position < 0 || position >= getCount()) return null;
convertView = super.getView(position, convertView, parent);
((TextView) convertView).setText(mItems.get(position).getLabel());
// Currently select_dialog_(single|multi)choice uses CheckedTextViews.
// If that changes, the class cast will no longer be valid.
// The WebView build cannot rely on this being the case, so
// we must check.
if (convertView instanceof CheckedTextView) {
// <optgroup> elements do not have check marks. If an item previously used as an
// <optgroup> gets reused for a non-<optgroup> element, we need to get the check mark
// back. Inflating a new View from XML can be slow, for both the inflation part and GC
// afterwards. Even creating a new Drawable can be tricky, considering getting the
// check/radio type and theme right.
// Saving the previously removed Drawables and reuse them when needed is faster,
// and the memory implication should be fine.
CheckedTextView view = (CheckedTextView) convertView;
if (mItems.get(position).getType() == PopupItemType.GROUP) {
if (view.getCheckMarkDrawable() != null) {
view.setTag(view.getCheckMarkDrawable());
view.setCheckMarkDrawable(null);
}
} else {
if (view.getCheckMarkDrawable() == null) {
view.setCheckMarkDrawable((Drawable) view.getTag());
}
}
}
// Draw the disabled element in a disabled state.
convertView.setEnabled(mItems.get(position).getType() != PopupItemType.DISABLED);
return convertView;
}
@Override
public boolean updateStyle(List<PXRuleSet> ruleSets, List<PXStylerContext> contexts) {
if (!super.updateStyle(ruleSets, contexts)) {
return false;
}
// Style the check mark. We will construct the
// drawable from the context states, and then set the constructed
// drawable as the check mark using the
// CheckedTextView#setCheckMarkDrawable call. Note that this is
// different than the View#setBackground(Drawable) call that is handled
// in the default View adapter.
CheckedTextView view = (CheckedTextView) contexts.get(0).getStyleable();
Drawable currentDrawable = getCheckMarkDrawable(view);
Map<int[], Drawable> existingStates = PXDrawableUtil.getExistingStates(currentDrawable);
Drawable newDrawable = null;
if (MapUtil.isEmpty(existingStates)) {
// create a new StateListDrawable for the icon,
// using the checked text view's adapter
// as the source of possible drawable states.
if (contexts.size() == 1) {
newDrawable = contexts.get(0).getBackgroundImage();
} else {
newDrawable = PXDrawableUtil.createNewStateListDrawable(
PXStyleAdapter.getStyleAdapter(view), ruleSets, contexts);
}
} else {
// create a drawable that will hold a merge of the existing states
// and the new states. Use the checked text view's
// adapter as the source of possible drawable states.
newDrawable = PXDrawableUtil.createDrawable(PXStyleAdapter.getStyleAdapter(view),
existingStates, ruleSets, contexts);
}
view.setCheckMarkDrawable(newDrawable);
return true;
}