我跟随下面的链接中给出的教程:Android的可扩展列表视图项的字体样式
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
我怎么可以给自定义字体(字体)在list_group.xml和list_item.xml textviews。
我已将Arial.ttf文件复制到资产文件夹中。
请帮帮我。
我跟随下面的链接中给出的教程:Android的可扩展列表视图项的字体样式
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
我怎么可以给自定义字体(字体)在list_group.xml和list_item.xml textviews。
我已将Arial.ttf文件复制到资产文件夹中。
请帮帮我。
您可以创建自定义的TextView因为我已经在你的XML重写TextView的类,然后,而不是使用的TextView在这里完成你使用CustomTextView:
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CustomTextView(Context context) {
super(context);
}
private void init(Context context, AttributeSet attrs) {
}
@Override
public void setTypeface(Typeface tf, int style) {
if (!isInEditMode()) {
Typeface normalTypeFace = Typeface.createFromAsset(getContext()
.getAssets(), "Esans.ttf");
Typeface boldTypeFace = Typeface.createFromAsset(getContext()
.getAssets(), "EsansBol.ttf");
Typeface lightTypeFace = Typeface.createFromAsset(getContext()
.getAssets(), "EsansLig.ttf");
Typeface mediumTypeFace = Typeface.createFromAsset(getContext()
.getAssets(), "EsansMed.ttf");
//Modifying styles to support the different styles of custom font
switch (style) {
case Typeface.NORMAL:
super.setTypeface(normalTypeFace);
break;
case Typeface.BOLD:
super.setTypeface(boldTypeFace);
break;
case Typeface.ITALIC:
super.setTypeface(lightTypeFace);
break;
case Typeface.BOLD_ITALIC:
super.setTypeface(mediumTypeFace);
break;
default:
super.setTypeface(normalTypeFace);
break;
}
}
}
}
你可以试试这在XML添加字体在XML
public class TextViewPlus extends TextView {
private static final String TAG = "TextView";
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
<com.icl.examples.TextViewPlus
android:id="@+id/textViewPlus1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="@string/info_text"
android:textColor="@color/White"
android:textStyle="bold"
android:textSize="50sp"
foo:customFont="Bamini.ttf">
</com.icl.examples.TextViewPlus>
将它添加到资源,水库 - >价值观attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewPlus">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
Hi Boopathi ...对不起,对于后期回复。 我想分配arail字体,所以我必须在xml中提供foo:customFont =“Arial.ttf”,对不对? – user3021918
Boopathi ...我试过了上面的代码,但是我在
@ user3021918 com.icl.examples是Package Name * * TextViewPlus ** Class。so change ** packageName ** to you on you TextViewPlus is present – Boopathi
反正有没有在Java代码中 – user3021918
改变字体在提到的例子,为什么你要这么做时,这是一个简单的方法呢?但是,你可以,yourTextView.setTypeFace(TypeFace.createFromAsset(Assetmanager mgr,String path); –
Pontus ....你可以请编辑你的代码,我只想使用arial.ttf,并请上传我如何使用自定义textview in xml。 – user3021918