gpt4 book ai didi

android - 将字体设置为android中的listview项目

转载 作者:行者123 更新时间:2023-11-30 02:16:06 25 4
gpt4 key购买 nike

我需要为 ListView 项添加自定义字体。我已经使用像这样的适配器将值添加到列表中

SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), newList, R.layout.club_list2, from, to);

这里我想为 club_list2 布局中的 TextView 设置字体。这怎么可能?

最佳答案

如果你想从 xml 中设置你的自定义字体,你可以这样做

public class TypefaceTextView extends TextView {

private static Map<String, Typeface> mTypefaces;

public TypefaceTextView(final Context context) {
this(context, null);
}

public TypefaceTextView(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}

public TypefaceTextView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
if (mTypefaces == null) {
mTypefaces = new HashMap<String, Typeface>();
}

// prevent exception in Android Studio / ADT interface builder
if (this.isInEditMode()) {
return;
}

final TypedArray array = context.obtainStyledAttributes(attrs, styleable.TypefaceTextView);
if (array != null) {
final String typefaceAssetPath = array.getString(
R.styleable.TypefaceTextView_customTypeface);

if (typefaceAssetPath != null) {
Typeface typeface = null;

if (mTypefaces.containsKey(typefaceAssetPath)) {
typeface = mTypefaces.get(typefaceAssetPath);
} else {
AssetManager assets = context.getAssets();
typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
mTypefaces.put(typefaceAssetPath, typeface);
}

setTypeface(typeface);
}
array.recycle();
}
}

}

在 xml 中。

<com.example.TypefaceTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="30sp"
android:text="@string/hello_world"
geekui:customTypeface="fonts/yourfont.ttf" />

你的字体应该在 asset/fonts/文件夹中

在 values 中创建资源文件并将其设置为名称 attrs.xml

然后复制过去

<resources>

<declare-styleable name="TypefaceTextView">
<attr name="customTypeface" format="string" />
</declare-styleable>

</resources>

关于android - 将字体设置为android中的listview项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29314931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com