あなたはCustomTypefacedButton
を使用している場所を見つけるので、私は私のblog
の上にそれを使用する方法をチェックアウトすることができませんでした
これは私が使用しているものであり、コード、利用可能なブログの説明 -
ファイル - 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
パスで資産フォルダ内の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>
これは、私はそれを使用している方法です -
<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" />
ログインXMLコードの行番号12とは何ですか? RelativeLayoutのlogin.xmlの高さの –
はwrap_contentでなければなりません。 親レイアウトとしてRelativeLayoutを使用する必要はなく、scrollViewの外側で何もしない場合は、ScrollViewを親レイアウトにしてください。 –
高さと幅の相対的なレイアウトです。 – user3563459