我正在尝试运行活动。不幸的是,我们正在低于错误。似乎存在一些膨胀布局问题。请帮助我。
提前致谢。
基本活动xml:http://pastie.org/10848446
基础活动java:http://pastie.org/10848447
登录活动xml:http://pastie.org/10848450
登录活动java:http://pastie.org/10848454
自定义字体类型:http://pastie.org/10848460
答案 0 :(得分:0)
检查这些课程Error inflating class com.app.nbhc.utilities.CustomTypefacedButton
答案 1 :(得分:0)
在您的布局文件中找不到您使用CustomTypefacedButton
的位置,因此请查看我在上使用它的方式
这就是我使用的,只是代码,博客上的解释 -
档案 - attrs.xml
<declare-styleable name="TextFont">
<attr name="customFont" format="string" />
</declare-styleable>
班级 - FontCache.java
public class FontCache {
private static Hashtable<String, Typeface> fontCache = new Hashtable<>();
/**
* Gets the typeface from Asset folder
* @param name path to the font within asset folder
* @param context context of the view
* @return
*/
public static Typeface get(String name, Context context) {
Typeface tf = fontCache.get(name);
if (tf == null) {
try {
tf = Typeface.createFromAsset(context.getAssets(), name);
} catch (Exception e) {
return null;
}
fontCache.put(name, tf);
}
return tf;
}
}
/**
* Sets a font on a TextView
*
* @param textview view for which font mentioned needs to be changed
* @param font path of the font where it has been saved (Generally within asset folder)
* @param context Context where view has been used
*/
public static void setCustomFont(TextView textview, String font, Context context) {
if (font == null) {
return;
}
Typeface typeface = FontCache.get(font, context);
if (typeface != null) {
textview.setTypeface(typeface);
}
}
班级 - CustomFontHelper.java
public class CustomFontHelper {
/**
* Sets a font on a textview based on the custom com.my.package:font attribute
* If the custom font attribute isn't found in the attributes nothing happens
*
* @param textview
* @param context
* @param attrs
*/
public static void setCustomFont(TextView textview, Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextFont);
String font = typedArray.getString(R.styleable.TextFont_customFont);
setCustomFont(textview, font, context);
typedArray.recycle();
}
/**
* Sets a font on a TextView
*
* @param textview view for which font mentioned needs to be changed
* @param font path of the font where it has been saved (Generally within asset folder)
* @param context Context where view has been used
*/
public static void setCustomFont(TextView textview, String font, Context context) {
if (font == null) {
return;
}
Typeface typeface = FontCache.get(font, context);
if (typeface != null) {
textview.setTypeface(typeface);
}
}
/**
* Sets a font on a paint based on the custom com.my.package:font attribute
* If the custom font attribute isn't found in the attributes nothing happens
*
* @param paint
* @param context
* @param attrs
*/
public static void setCustomFont(Paint paint, Context context, AttributeSet attrs){
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextFont);
String font = typedArray.getString(R.styleable.TextFont_customFont);
setCustomFont(paint, font, context);
typedArray.recycle();
}
/**
* Changing font of Paint element
* @param paint text element of which font needs to be changed
* @param font
* @param context
*/
public static void setCustomFont(Paint paint, String font, Context context) {
if (font == null) {
return;
}
Typeface typeface = FontCache.get(font, context);
if (typeface != null) {
paint.setTypeface(typeface);
}
}
}
班级 - FontButton.java
public class FontButton extends Button {
public FontButton(Context context) {
super(context);
}
public FontButton(Context context, AttributeSet attrs) {
super(context, attrs);
CustomFontHelper.setCustomFont(this, context, attrs);
}
public FontButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
CustomFontHelper.setCustomFont(this, context, attrs);
}
}
在资产文件夹
中的string.xml
字体路径中
<string name="font_light">fonts/SourceSansPro-Regular.ttf</string>
<string name="font_semibold">fonts/SourceSansPro-Semibold.ttf</string>
<string name="font_regular">fonts/SourceSansPro-Regular.ttf</string>
<string name="font_bold">fonts/Oxygen-Bold.ttf</string>
在xml中,这就是我使用它的方式 -
<com.COMPANY.APP.app.custom.FontButton xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/lbl_welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/img_logo"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="YOUR SHOPPING COMPANION!"
android:textColor="@color/toolbar"
android:textSize="@dimen/font_12"
app:customFont="@string/font_semibold" />