下面列出了android.widget.TextView#setHintTextColor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void apply(View view) {
if (view == null) {
return;
}
if (view instanceof TextView) {
TextView tv = (TextView) view;
if ((RES_TYPE_NAME_COLOR.equals(attrValueTypeName)) || (RES_TYPE_NAME_HINT_COLOR.equals(attrValueTypeName))) {
ColorStateList color =
AndroidSkin.getInstance()
.getSkinColorStateList(attrValueTypeName, attrValueRefName, attrValueRefId);
tv.setHintTextColor(color);
}
}
}
/**
* Sets the searchview's hint icon and text.
*
* @param searchView
* @param drawableResource
* @param hintText
*/
public static void setSearchHintIcon(SearchView searchView, int drawableResource, String hintText) {
try {
// Accessing the SearchAutoComplete
int queryTextViewId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
View autoComplete = searchView.findViewById(queryTextViewId);
//Class<?> clazz = Class.forName("android.widget.SearchView$SearchAutoComplete");
TextView searchBox = (TextView) searchView.findViewById(R.id.search_src_text);
SpannableStringBuilder stopHint = new SpannableStringBuilder(" ");
stopHint.append(hintText);
// Add the icon as an spannable
Drawable searchIcon = searchView.getContext().getResources().getDrawable(drawableResource);
Float rawTextSize = searchBox.getTextSize();
int textSize = (int) (rawTextSize * 1.25);
searchIcon.setBounds(0, 0, textSize, textSize);
stopHint.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the new hint text
searchBox.setHint(stopHint);
//searchBox.setTextColor(Color.WHITE);
searchBox.setHintTextColor(Color.LTGRAY);
} catch (Exception e) {
Log.e("SearchView", e.getMessage(), e);
}
}
/**
* Sets the searchview's hint icon and text.
*
* @param searchView
* @param drawableResource
* @param hintText
*/
public static void setSearchHintIcon(SearchView searchView, int drawableResource, String hintText) {
try {
// Accessing the SearchAutoComplete
int queryTextViewId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
View autoComplete = searchView.findViewById(queryTextViewId);
//Class<?> clazz = Class.forName("android.widget.SearchView$SearchAutoComplete");
TextView searchBox = (TextView) searchView.findViewById(R.id.search_src_text);
SpannableStringBuilder stopHint = new SpannableStringBuilder(" ");
stopHint.append(hintText);
// Add the icon as an spannable
Drawable searchIcon = searchView.getContext().getResources().getDrawable(drawableResource);
Float rawTextSize = searchBox.getTextSize();
int textSize = (int) (rawTextSize * 1.25);
searchIcon.setBounds(0, 0, textSize, textSize);
stopHint.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the new hint text
searchBox.setHint(stopHint);
//searchBox.setTextColor(Color.WHITE);
searchBox.setHintTextColor(Color.LTGRAY);
} catch (Exception e) {
Log.e("SearchView", e.getMessage(), e);
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
final Context context = getContext();
textView = (TextView) getChildAt(0);
textView.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
textView.setHintTextColor(lessSignificantColor);
textView.setHorizontalFadingEdgeEnabled(true);
textView.setSingleLine();
textView.setCompoundDrawablePadding(UIUtil.dip2pix(2));
setHint(0);
setValidateAmount(textView instanceof EditText);
textView.addTextChangedListener(textViewListener);
textView.setOnFocusChangeListener(textViewListener);
textView.setOnEditorActionListener(textViewListener);
contextButton = new View(context) {
@Override
protected void onMeasure(final int wMeasureSpec, final int hMeasureSpec) {
setMeasuredDimension(textView.getCompoundPaddingRight(),
textView.getMeasuredHeight());
}
};
final LayoutParams chooseViewParams = new LayoutParams(ViewGroup.LayoutParams
.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
chooseViewParams.gravity = Gravity.RIGHT;
contextButton.setLayoutParams(chooseViewParams);
this.addView(contextButton);
updateAppearance();
}
public static VividSearchView getSearchView(Context context) {
VividSearchView searchView = new VividSearchView(context);
searchView.setIconifiedByDefault(false);
searchView.setQueryHint(context.getString(R.string.search_hint));
searchView.setIconified(true);
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
View searchPlate = searchView.findViewById(searchPlateId);
if (searchPlate != null) {
// 修改搜索文字的颜色
int searchTextId = searchPlate.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
if (searchText != null) {
searchText.setTextColor(Color.WHITE);
searchText.setHintTextColor(Color.WHITE);
}
}
// 修改搜索hint图标,这里有bug,所以不得已搜索的空间又小了
int searchButtonId = searchView.getContext().getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView searchButton = (ImageView) searchView.findViewById(searchButtonId);
if (searchButton != null) {
searchButton.setImageResource(R.drawable.ic_search_hint);
}
// 修改清除图标
int clearId = searchView.getContext().getResources().getIdentifier("android:id/search_close_btn", null, null);
ImageView closeButton = (ImageView) searchView.findViewById(clearId);
if (closeButton != null) {
closeButton.setImageResource(R.drawable.ic_clear);
}
return searchView;
}
public static void applyTextAppearance(TextView v, int resId){
if(resId == 0)
return;
String fontFamily = null;
int typefaceIndex = -1;
int styleIndex = -1;
int shadowColor = 0;
float dx = 0, dy = 0, r = 0;
TypedArray appearance = v.getContext().obtainStyledAttributes(resId, R.styleable.TextAppearance);
if (appearance != null) {
int n = appearance.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = appearance.getIndex(i);
if (attr == R.styleable.TextAppearance_android_textColorHighlight) {
v.setHighlightColor(appearance.getColor(attr, 0));
} else if (attr == R.styleable.TextAppearance_android_textColor) {
v.setTextColor(appearance.getColorStateList(attr));
} else if (attr == R.styleable.TextAppearance_android_textColorHint) {
v.setHintTextColor(appearance.getColorStateList(attr));
} else if (attr == R.styleable.TextAppearance_android_textColorLink) {
v.setLinkTextColor(appearance.getColorStateList(attr));
} else if (attr == R.styleable.TextAppearance_android_textSize) {
v.setTextSize(TypedValue.COMPLEX_UNIT_PX, appearance.getDimensionPixelSize(attr, 0));
} else if (attr == R.styleable.TextAppearance_android_typeface) {
typefaceIndex = appearance.getInt(attr, -1);
} else if (attr == R.styleable.TextAppearance_android_fontFamily) {
fontFamily = appearance.getString(attr);
} else if (attr == R.styleable.TextAppearance_tv_fontFamily) {
fontFamily = appearance.getString(attr);
} else if (attr == R.styleable.TextAppearance_android_textStyle) {
styleIndex = appearance.getInt(attr, -1);
} else if (attr == R.styleable.TextAppearance_android_textAllCaps) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
v.setAllCaps(appearance.getBoolean(attr, false));
} else if (attr == R.styleable.TextAppearance_android_shadowColor) {
shadowColor = appearance.getInt(attr, 0);
} else if (attr == R.styleable.TextAppearance_android_shadowDx) {
dx = appearance.getFloat(attr, 0);
} else if (attr == R.styleable.TextAppearance_android_shadowDy) {
dy = appearance.getFloat(attr, 0);
} else if (attr == R.styleable.TextAppearance_android_shadowRadius) {
r = appearance.getFloat(attr, 0);
} else if (attr == R.styleable.TextAppearance_android_elegantTextHeight) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
v.setElegantTextHeight(appearance.getBoolean(attr, false));
} else if (attr == R.styleable.TextAppearance_android_letterSpacing) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
v.setLetterSpacing(appearance.getFloat(attr, 0));
} else if (attr == R.styleable.TextAppearance_android_fontFeatureSettings) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
v.setFontFeatureSettings(appearance.getString(attr));
}
}
appearance.recycle();
}
if (shadowColor != 0)
v.setShadowLayer(r, dx, dy, shadowColor);
Typeface tf = null;
if (fontFamily != null) {
tf = TypefaceUtil.load(v.getContext(), fontFamily, styleIndex);
if (tf != null)
v.setTypeface(tf);
}
if(tf != null) {
switch (typefaceIndex) {
case 1:
tf = Typeface.SANS_SERIF;
break;
case 2:
tf = Typeface.SERIF;
break;
case 3:
tf = Typeface.MONOSPACE;
break;
}
v.setTypeface(tf, styleIndex);
}
}
public static void applyTextAppearance(TextView v, int resId){
if(resId == 0)
return;
String fontFamily = null;
int typefaceIndex = -1;
int styleIndex = -1;
int shadowColor = 0;
float dx = 0, dy = 0, r = 0;
TypedArray appearance = v.getContext().obtainStyledAttributes(resId, R.styleable.TextAppearance);
if (appearance != null) {
int n = appearance.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = appearance.getIndex(i);
if (attr == R.styleable.TextAppearance_android_textColorHighlight) {
v.setHighlightColor(appearance.getColor(attr, 0));
} else if (attr == R.styleable.TextAppearance_android_textColor) {
v.setTextColor(appearance.getColorStateList(attr));
} else if (attr == R.styleable.TextAppearance_android_textColorHint) {
v.setHintTextColor(appearance.getColorStateList(attr));
} else if (attr == R.styleable.TextAppearance_android_textColorLink) {
v.setLinkTextColor(appearance.getColorStateList(attr));
} else if (attr == R.styleable.TextAppearance_android_textSize) {
v.setTextSize(TypedValue.COMPLEX_UNIT_PX, appearance.getDimensionPixelSize(attr, 0));
} else if (attr == R.styleable.TextAppearance_android_typeface) {
typefaceIndex = appearance.getInt(attr, -1);
} else if (attr == R.styleable.TextAppearance_android_fontFamily) {
fontFamily = appearance.getString(attr);
} else if (attr == R.styleable.TextAppearance_tv_fontFamily) {
fontFamily = appearance.getString(attr);
} else if (attr == R.styleable.TextAppearance_android_textStyle) {
styleIndex = appearance.getInt(attr, -1);
} else if (attr == R.styleable.TextAppearance_android_textAllCaps) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
v.setAllCaps(appearance.getBoolean(attr, false));
} else if (attr == R.styleable.TextAppearance_android_shadowColor) {
shadowColor = appearance.getInt(attr, 0);
} else if (attr == R.styleable.TextAppearance_android_shadowDx) {
dx = appearance.getFloat(attr, 0);
} else if (attr == R.styleable.TextAppearance_android_shadowDy) {
dy = appearance.getFloat(attr, 0);
} else if (attr == R.styleable.TextAppearance_android_shadowRadius) {
r = appearance.getFloat(attr, 0);
} else if (attr == R.styleable.TextAppearance_android_elegantTextHeight) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
v.setElegantTextHeight(appearance.getBoolean(attr, false));
} else if (attr == R.styleable.TextAppearance_android_letterSpacing) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
v.setLetterSpacing(appearance.getFloat(attr, 0));
} else if (attr == R.styleable.TextAppearance_android_fontFeatureSettings) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
v.setFontFeatureSettings(appearance.getString(attr));
}
}
appearance.recycle();
}
if (shadowColor != 0)
v.setShadowLayer(r, dx, dy, shadowColor);
Typeface tf = null;
if (fontFamily != null) {
tf = TypefaceUtil.load(v.getContext(), fontFamily, styleIndex);
if (tf != null)
v.setTypeface(tf);
}
if(tf != null) {
switch (typefaceIndex) {
case 1:
tf = Typeface.SANS_SERIF;
break;
case 2:
tf = Typeface.SERIF;
break;
case 3:
tf = Typeface.MONOSPACE;
break;
}
v.setTypeface(tf, styleIndex);
}
}
public static void doColorizing(View v, final ColorFilter colorFilter, int toolbarIconsColor){
/**
* The documentation for 'Known Direct Subclasses' of View on the developer site,
* tells us that there is only a few types to look for:
* ImageView, TextView and ViewGroup
* Most other types, that we might expect in a Toolbar, extends from one of these.
*
* http://developer.android.com/reference/android/view/View.html
*/
if(v instanceof ImageView) {
if (v.getId()!=R.id.backdrop) {
((ImageView) v).getDrawable().setAlpha(255);
((ImageView) v).getDrawable().setColorFilter(colorFilter);
}
} else if(v instanceof TextView) {
TextView tv = ((TextView)v);
tv.setTextColor(toolbarIconsColor);
tv.setHintTextColor(toolbarIconsColor);
int drawablesCount = tv.getCompoundDrawables().length;
for(int k = 0; k < drawablesCount; k++) {
if(tv.getCompoundDrawables()[k] != null) {
tv.getCompoundDrawables()[k].setColorFilter(colorFilter);
}
}
} else if (v instanceof ViewGroup){
for (int lli =0; lli< ((ViewGroup)v).getChildCount(); lli ++) {
doColorizing(((ViewGroup) v).getChildAt(lli), colorFilter, toolbarIconsColor);
}
} else {
Log.d("CustomToolbar", "Unknown class: " + v.getClass().getSimpleName());
}
}