【问题标题】:How do I set a custom font in a custom ListView如何在自定义 ListView 中设置自定义字体
【发布时间】:2014-09-07 00:52:15
【问题描述】:

我是 android 编程新手,我正在努力解决以下问题。 我正在使用自定义 ListView 来显示商店名称及其徽标。 我想将标准 textview 字体更改为自定义字体。

我用于普通文本视图的方法不起作用。 你能帮帮我吗?

以下代码是我的 ListView 适配器。

package com.mallspecials.shoppingmallspecialsau;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ShowTheStoreAdapter extends BaseAdapter {


       // Declare Variables
    Context context;
    String[] thestore;

    int[] logo;
    LayoutInflater inflater;

    public ShowTheStoreAdapter(Context context, String[] thestore,  int[] logo) {
        this.context = context;
        this.thestore = thestore;
        this.logo = logo;

    }

    private AssetManager getAssets() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getCount() {
        return thestore.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

 //Set Snell Roundhand Font for the StoreName
    Typeface font = Typeface.createFromAsset(getAssets(), "Snell Roundhand Bold Script.ttf");

        // Declare Variables
        TextView StoreName;
        ImageView imglogo;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_stores, parent, false);

            //Set the StoreName Text
            StoreName = (TextView) itemView.findViewById(R.id.thestore);
            StoreName.setTextColor(Color.RED);
            StoreName.setShadowLayer(3, -2, -5,(Color.YELLOW));
            StoreName.setTypeface(font);
            StoreName.setText(thestore[position]);

        // Locate the ImageView in listview_stores.xml
        imglogo = (ImageView) itemView.findViewById(R.id.storelogo);
        // Capture position and set to the ImageView
        imglogo.setImageResource(logo[position]);

        return itemView;
    }


}

下面是我的适配器视图实现的代码。

list = (ListView) findViewById(R.id.listview);
adapter = new ShowTheStoreAdapter(this, thestore, storelogo);
list.setAdapter(adapter);       

【问题讨论】:

  • “不工作”是什么意思?您是否收到可以分享的错误消息或可以描述的不良结果?
  • 对不起,斯科特,当我点击上一个活动时,手机屏幕上显示错误消息“不幸的是,“我的应用名称”已停止。
  • 如果我在不尝试设置自定义字体的情况下运行应用程序,它会完美运行。

标签: android listview android-listview


【解决方案1】:

在 BaseAdapter 的帮助下为您的 ListView 创建子视图。在该子视图中添加自定义文本视图。

https://github.com/johnkil/Android-RobotoTextView

【讨论】:

    【解决方案2】:

    列表视图本身不负责绘制项目,它使用适配器来创建列表项目。此适配器创建一个视图以在需要时显示列表项。要更改用于显示列表项的字体,您必须更改适配器以返回具有新字体的视图。这可以在 Adapter.getView 方法中完成。如果您当前使用的是标准适配器实现,则可能需要对其进行子类化(或完全替换它)。

    检查这个:How to change color and font on ListView

    【讨论】:

    • 当您说适配器实现时,您的意思是当我尝试从主要活动中设置项目时?我已经从我的主要活动中添加了代码来调用自定义适配器。
    • 我还看到你给我看的例子使用了一个数组适配器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 2012-11-22
    • 2011-04-15
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多