在Android中将字体设置为ListView项

尼维达(Nivedha S)

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

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

在这里,我想在club_list2布局中为textviews设置字体。这怎么可能?

鱼40

如果要从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 /文件夹中

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

然后复制过去

<resources>

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

</resources>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android中将默认应用程序字体设置为自定义字体?

在matplotlib中将xlabel的字体设置为Arial

如何在Xamarin.Forms中将CommandParameter设置为ListView项本身

如何在Android TextView中将字体样式设置为粗体,斜体和下划线?

在Android Studio中将Roboto设置为我的应用程序的全局字体

将ListView项设置为选中

如何基于Android中的内容将Listview项设置为不可点击

在Notepad ++中将字体大小设置为13

在CSS中将字体家族设置为特定符号

在Odoo Qweb Reports中将字体设置为整页

设置Android的Listview项的标识符

在控制台中将所选字体设置为默认字体

如何在Arch Linux中将终端字体设置为默认字体

在Android中将视频设置为SET WALLPAPER?

在 Android 中将 RecyclerView 设置为 FrameLayout 问题

将字体颜色,字体样式和背景颜色设置为ListView.Item.Caption

Android为自定义TextView小部件设置字体

Android:将TextView的字体设置为NORMAL会导致崩溃

如何在情节提要中将自定义字体设置为UILabel

在angular-cli 8中将字体设置为5真棒7

如何在Swift中将文本字体设置为System Thin?

如何在css文件中将字体的url设置为mvc项目?

如何在iOS 8的启动屏幕中将字体设置为标签

如何在iOS中将特定大小的自定义字体设置为Web View?

如何在WPF中将Segoe MDL字体图标设置为图像源?

在Android中设置字体

将字体设置为alertdialog

Android依赖项设置为不支持的compileOnly / provided

在Android中将徽标设置为工具栏背景