Не получается сделать кастомный TextView
Создал класс CustomTextView
public class CustomTextView extends android.support.v7.widget.AppCompatTextView{
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
UiUtil.setCustomFont(this, context, attrs,
R.styleable.ru_myapp_fairytalesforchildren_utils_CustomTextView,
R.styleable.ru_myapp_fairytalesforchildren_utils_CustomTextView_customFont);
}
public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
UiUtil.setCustomFont(this, context, attrs,
R.styleable.ru_myapp_fairytalesforchildren_utils_CustomTextView,
R.styleable.ru_myapp_fairytalesforchildren_utils_CustomTextView_customFont);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
getHandler().removeCallbacksAndMessages(null);
}
}
Класс UiUtil
public class UiUtil {
public static final String TAG = "UiUtil";
public static void setCustomFont(View textViewOrButton, Context ctx, AttributeSet attrs, int[] attributeSet, int fontId) {
TypedArray a = ctx.obtainStyledAttributes(attrs, attributeSet);
String customFont = a.getString(fontId);
setCustomFont(textViewOrButton, ctx, customFont);
a.recycle();
}
private static boolean setCustomFont(View textView, Context ctx, String asset) {
if (TextUtils.isEmpty(asset))
return false;
Typeface tf = null;
try {
tf = getFont(ctx, asset);
if (textView instanceof TextView) {
((TextView) textView).setTypeface(tf);
}
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: " + asset, e);
return false;
}
return true;
}
private static final Hashtable<String, SoftReference<Typeface>> fontCache = new Hashtable<String, SoftReference<Typeface>>();
public static Typeface getFont(Context c, String name) {
synchronized (fontCache) {
if (fontCache.get(name) != null) {
SoftReference<Typeface> ref = fontCache.get(name);
if (ref.get() != null) {
return ref.get();
}
}
Typeface typeface = Typeface.createFromAsset(
c.getAssets(),
"fonts/" + name
);
fontCache.put(name, new SoftReference<Typeface>(typeface));
return typeface;
}
}
}
Создал атрибут в res/values/attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ru.myapp.fairytalesforchildren.utils.CustomTextView">
<attr name="customFont" format="string" />
</declare-styleable>
</resources>
Использую в разметке так
<ru.myapp.fairytalesforchildren.utils.CustomTextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:maxLines="2"
android:text="@string/item_title"
android:textColor="@android:color/white"
android:textSize="14sp"
android:textStyle="bold"
app:font="LaMonaKidsBaseLayerTwo.ttf"/>
Выдает ошибку
Information:Gradle tasks [clean, :app:assembleDebug] C:\Android\Projects\Fairytalesforchildren\app\src\main\res\layout\item.xml Error:error: 'LaMonaKidsBaseLayerTwo.ttf' is incompatible with attribute ru.myapp.fairytalesforchildren:font (attr) reference [weak]. Error:error: 'LaMonaKidsBaseLayerTwo.ttf' is incompatible with attribute ru.myapp.fairytalesforchildren:font (attr) reference [weak]. Error:'LaMonaKidsBaseLayerTwo.ttf' is incompatible with attribute ru.myapp.fairytalesforchildren:font (attr) reference [weak]. Error:'LaMonaKidsBaseLayerTwo.ttf' is incompatible with attribute ru.myapp.fairytalesforchildren:font (attr) reference [weak]. Error:failed linking file resources. Error:java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details Error:com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details Error:Execution failed for task ':app:processDebugResources'.
Failed to execute aapt Information:BUILD FAILED in 5s Information:9 errors Information:0 warnings Information:See complete output in console
Не могу понять в чем причина. Помогите исправить пожалуйста