下面列出了android.widget.TextView#getTypeface ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public StepTab(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater.from(getContext()).inflate(R.layout.ms_step_tab, this, true);
mSelectedColor = ContextCompat.getColor(context, R.color.ms_selectedColor);
mUnselectedColor = ContextCompat.getColor(context, R.color.ms_unselectedColor);
mErrorColor = ContextCompat.getColor(context, R.color.ms_errorColor);
mStepNumberTextView = (TextView) findViewById(R.id.ms_stepNumber);
mStepDoneIndicator = (ImageView) findViewById(R.id.ms_stepDoneIndicator);
mStepIconBackground = (ImageView) findViewById(R.id.ms_stepIconBackground);
mStepDivider = findViewById(R.id.ms_stepDivider);
mStepTitleTextView = (TextView) findViewById(R.id.ms_stepTitle);
mStepSubtitleTextView = (TextView) findViewById(R.id.ms_stepSubtitle);
mTitleColor = mStepTitleTextView.getCurrentTextColor();
mSubtitleColor = mStepSubtitleTextView.getCurrentTextColor();
Typeface typeface = mStepTitleTextView.getTypeface();
mNormalTypeface = Typeface.create(typeface, Typeface.NORMAL);
mBoldTypeface = Typeface.create(typeface, Typeface.BOLD);
Drawable avd = createCircleToWarningDrawable();
mStepIconBackground.setImageDrawable(avd);
}
/**
* <p>Replace the font of specified view and it's children with specified typeface</p>
*/
private void replaceFont(@NonNull View root, @NonNull Typeface typeface) {
if (root == null || typeface == null) {
return;
}
if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font
TextView textView = (TextView)root;
// Extract previous style of TextView
int style = Typeface.NORMAL;
if (textView.getTypeface() != null) {
style = textView.getTypeface().getStyle();
}
textView.setTypeface(typeface, style);
} else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); ++i) {
replaceFont(viewGroup.getChildAt(i), typeface);
}
} // else return
}
/**
* Static method for setting the typeface for the given View
* The font file should be placed in assets folder in the directory of fonts.
* Any view that has parent class of TextView can easily use this method.
*
* @param view typeface view
* @param fontName font name
*/
public static void setCustomFont(View view, String fontName) {
if (view instanceof TextView) {
Context context = view.getContext();
Typeface font = getInstance(context).get(fontName);
TextView textView = (TextView) view;
if (textView.getTypeface() != font)
textView.setTypeface(font);
}
}
public static void applyFont(TextView textView) {
if (textView.getTypeface() != null && textView.getTypeface().isBold()) {
textView.setTypeface(getTypeface(textView.getContext(), FontStyle.BOLD));
} else {
textView.setTypeface(getTypeface(textView.getContext(), FontStyle.REGULAR));
}
}
protected void supportTypeface(Context context, TextView view, AttributeSet attrs) {
if (view == null) {
return;
}
Typeface typeface = Typography.getTypeface(context, attrs, view instanceof Button ? R.attr.buttonStyle : 0, 0);
typeface = typeface != null ? typeface : Typography.getDefaultTypeface(context);
if (typeface != null && view.getTypeface() != typeface) {
view.setTypeface(typeface);
}
}
protected static float getFontDescent(TextView textView, Paint fontMeasurePaint) {
if (fontMeasurePaint.getTextSize() != textView.getTextSize()) {
fontMeasurePaint.setTextSize(textView.getTextSize());
}
if (fontMeasurePaint.getTypeface() != textView.getTypeface()) {
fontMeasurePaint.setTypeface(textView.getTypeface());
}
return fontMeasurePaint.descent();
}
/**
* Apply typeface to single view
*
* @param view to typeface typeface
* @param typefaceCollection typeface collection
*/
private static void applyForView(View view, TypefaceCollection typefaceCollection) {
if (view instanceof TextView) {
TextView textView = (TextView) view;
Typeface oldTypeface = textView.getTypeface();
final int style = oldTypeface == null ? Typeface.NORMAL : oldTypeface.getStyle();
textView.setTypeface(typefaceCollection.getTypeface(style));
textView.setPaintFlags(textView.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
Builder(int id, @Nullable ViewHierarchyElementAndroid parent, View fromView) {
// Bookkeeping
this.id = id;
this.parentId = (parent != null) ? parent.getId() : null;
this.drawingOrder = null;
// API 16+ properties
this.scrollable = AT_16 ? fromView.isScrollContainer() : null;
// API 11+ properties
this.backgroundDrawableColor =
(AT_11 && (fromView != null) && (fromView.getBackground() instanceof ColorDrawable))
? ((ColorDrawable) fromView.getBackground()).getColor()
: null;
// Base properties
this.visibleToUser = ViewAccessibilityUtils.isVisibleToUser(fromView);
this.className = fromView.getClass().getName();
this.accessibilityClassName = null;
this.packageName = fromView.getContext().getPackageName();
this.resourceName =
(fromView.getId() != View.NO_ID)
? ViewAccessibilityUtils.getResourceNameForView(fromView)
: null;
this.contentDescription = SpannableStringAndroid.valueOf(fromView.getContentDescription());
this.enabled = fromView.isEnabled();
if (fromView instanceof TextView) {
TextView textView = (TextView) fromView;
// Hint text takes precedence if no text is present.
CharSequence text = textView.getText();
if (TextUtils.isEmpty(text)) {
text = textView.getHint();
}
this.text = SpannableStringAndroid.valueOf(text);
this.textSize = textView.getTextSize();
this.textColor = textView.getCurrentTextColor();
this.typefaceStyle =
(textView.getTypeface() != null) ? textView.getTypeface().getStyle() : null;
} else {
this.text = null;
this.textSize = null;
this.textColor = null;
this.typefaceStyle = null;
}
this.importantForAccessibility = ViewAccessibilityUtils.isImportantForAccessibility(fromView);
this.clickable = fromView.isClickable();
this.longClickable = fromView.isLongClickable();
this.focusable = fromView.isFocusable();
this.editable = ViewAccessibilityUtils.isViewEditable(fromView);
this.canScrollForward =
(ViewCompat.canScrollVertically(fromView, 1)
|| ViewCompat.canScrollHorizontally(fromView, 1));
this.canScrollBackward =
(ViewCompat.canScrollVertically(fromView, -1)
|| ViewCompat.canScrollHorizontally(fromView, -1));
this.checkable = (fromView instanceof Checkable);
this.checked = (fromView instanceof Checkable) ? ((Checkable) fromView).isChecked() : null;
this.hasTouchDelegate = (fromView.getTouchDelegate() != null);
this.touchDelegateBounds = ImmutableList.of(); // Unavailable from the View API
this.boundsInScreen = getBoundsInScreen(fromView);
this.nonclippedHeight = fromView.getHeight();
this.nonclippedWidth = fromView.getWidth();
this.actionList = ImmutableList.of(); // Unavailable from the View API
}
@Override
public void populateView(View view, Cursor cursor, BaseExpandableListAdapter adapter, int flags)
{
long now = System.currentTimeMillis();
int position = cursor.getPosition();
// set list title
String groupTitle = getTitle(cursor, view.getContext());
TextView title = (TextView) view.findViewById(android.R.id.title);
if (title != null)
{
title.setText(groupTitle);
}
// set search time
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
if (text1 != null)
{
text1.setText(DateUtils.getRelativeTimeSpanString(
cursor.getLong(cursor.getColumnIndex(SearchHistoryDatabaseHelper.SearchHistoryColumns.TIMESTAMP)), now, DateUtils.MINUTE_IN_MILLIS));
}
// set list elements
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
int childrenCount = adapter.getChildrenCount(position);
if (text2 != null && ((ExpandableGroupDescriptorAdapter) adapter).childCursorLoaded(position))
{
Resources res = view.getContext().getResources();
text2.setText(res.getQuantityString(R.plurals.number_of_tasks, childrenCount, childrenCount));
}
// show/hide divider
View divider = view.findViewById(R.id.divider);
if (divider != null)
{
divider.setVisibility((flags & FLAG_IS_EXPANDED) != 0 && childrenCount > 0 ? View.VISIBLE : View.GONE);
}
View colorbar1 = view.findViewById(R.id.colorbar1);
View colorbar2 = view.findViewById(R.id.colorbar2);
if (colorbar1 != null)
{
colorbar1.setVisibility(View.GONE);
}
if (colorbar2 != null)
{
colorbar2.setVisibility(View.GONE);
}
View removeSearch = view.findViewById(R.id.quick_add_task);
if (removeSearch != null)
{
((ImageView) removeSearch).setImageResource(R.drawable.content_remove);
removeSearch.setOnClickListener(removeListener);
GroupTag tag = (GroupTag) removeSearch.getTag();
Long groupId = cursor.getLong(cursor.getColumnIndex(SearchHistoryColumns._ID));
if (tag == null || tag.groupId != groupId)
{
removeSearch.setTag(new GroupTag(groupTitle, groupId));
}
}
if ((flags & FLAG_IS_EXPANDED) != 0)
{
if (removeSearch != null)
{
removeSearch.setVisibility(View.VISIBLE);
}
if (text2 != null)
{
text2.setVisibility(View.GONE);
}
}
else
{
if (removeSearch != null)
{
removeSearch.setVisibility(View.GONE);
}
if (text2 != null)
{
text2.setVisibility(View.VISIBLE);
}
}
// TODO: swap styles instead of modifying the font style
boolean isHistoric = cursor.getInt(cursor.getColumnIndex(SearchHistoryColumns.HISTORIC)) > 0;
Typeface oldtypeface = title.getTypeface();
title.setTypeface(oldtypeface, swapStyle(isHistoric, oldtypeface));
// set history icon
ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
icon.setImageResource(R.drawable.ic_history);
icon.setVisibility(isHistoric ? View.VISIBLE : View.INVISIBLE);
}