해결되었습니다. 예를 들어 내 사용자 지정 텍스트 뷰에서보세요 :
standardTextView.setTextAppearance(ExampleFragment.this.getActivity(), R.style.label_bold);
바인드합니다 :
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="StandardTextView">
<attr name="typeface" format="string" />
</declare-styleable>
</resources>
레이아웃 :
package com.example.ui.components;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
import com.example.core.ui.R;
public class StandardTextView extends TextView {
public StandardTextView(Context context) {
this(context, null);
}
public StandardTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public StandardTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setTypeFaceFromAttributes(attrs.getStyleAttribute());
}
@Override
public void setTextAppearance(Context context, int resid) {
super.setTextAppearance(context, resid);
setTypeFaceFromAttributes(resid);
}
private void setTypeFaceFromAttributes(int attributeSet) {
if (attributeSet == -1) {
return;
}
TypedArray a = null;
try {
a = getContext().obtainStyledAttributes(attributeSet, R.styleable.StandardTextView);
if (a.getString(R.styleable.StandardTextView_typeface) == null) {
return;
}
String typeFaceString = a.getString(R.styleable.StandardTextView_typeface);
setupTypeface(typeFaceString);
} finally {
if (a != null) {
a.recycle();
}
}
}
private void setupTypeface(String typeFace) {
if (typeFace == null || "".equals(typeFace)) {
return;
}
Resources resources = getResources();
if (resources == null) {
return;
}
AssetManager assetManager = resources.getAssets();
if (assetManager == null) {
return;
}
Typeface tf = Typeface.createFromAsset(assetManager, typeFace);
setTypeface(tf);
}
}
이 당신이 그것을 사용할 수있는 방법입니다
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/default_background"
android:orientation="vertical" >
<com.example.ui.components.StandardTextView
android:id="@+id/sure_swipe_create_text"
style="@style/label"
android:layout_width="match_parent"
android:layout_height="56dp"
android:gravity="center|top"
android:paddingTop="20dp"
android:text="@string/example" />
</RelativeLayout>