Android ListView 中的自定义字体

发布于 2024-10-10 03:26:13 字数 330 浏览 4 评论 0原文

我在整个应用程序中使用自定义字体(顺便说一句,我令人沮丧地发现您必须以编程方式手动应用到每个控件!),并且我需要将其应用到列表视图。问题是我看不到在哪里将列表字体中使用的文本视图设置为我的自定义字体(因为我从未实例化它 - 这都是由适配器处理的)。

我理想的情况是能够使用这样的适配器:

new ArrayAdapter(Context context, TextView textView, List<T> objects)

这样我就可以在填充我的列表之前执行 textView.setTypeface 。有谁知道是否有办法按照这些方式做一些事情?

I'm using a custom font throughout my application (which, incidentally, I've frustratingly found out that you have to apply programmatically by hand to EVERY control!), and I need to apply it to a listview. The problem is that I can't see where I'd set the textview used in the list's font to my custom font (as I never instantiate it - that's all taken care of by the adapter).

What I'd ideally like is to be able to use an adapter like this:

new ArrayAdapter(Context context, TextView textView, List<T> objects)

That way I could do: textView.setTypeface before populating my list. Does anyone know if there's a way to do something along these lines?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

〆凄凉。 2024-10-17 03:26:13

如果您不想创建新类,则可以在创建适配器时重写 getView 方法,这是一个带有标题和副标题的简单适配器的示例:

Typeface typeBold = Typeface.createFromAsset(getAssets(),"fonts/helveticabold.ttf");
Typeface typeNormal = Typeface.createFromAsset(getAssets(), "fonts/helvetica.ttf");

SimpleAdapter adapter = new SimpleAdapter(this, items,R.layout.yourLvLayout, new String[]{"title",
    "subtitle" }, new int[] { R.id.rowTitle,
    R.id.rowSubtitle }){
            @Override
        public View getView(int pos, View convertView, ViewGroup parent){
            View v = convertView;
            if(v== null){
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v=vi.inflate(R.layout.yourLvLayout, null);
            }
            TextView tv = (TextView)v.findViewById(R.id.rowTitle);
            tv.setText(items.get(pos).get("title"));
            tv.setTypeface(typeBold);
            TextView tvs = (TextView)v.findViewById(R.id.rowSubtitle);
            tvs.setText(items.get(pos).get("subtitle"));
            tvs.setTypeface(typeNormal);
            return v;
        }


};
listView.setAdapter(adapter);

其中 items 是您的地图 ArrayList

希望有所帮助

If you don't want to create a new class you can override the getView method when creating your Adapter, this is an example of a simpleAdapter with title and subtitle:

Typeface typeBold = Typeface.createFromAsset(getAssets(),"fonts/helveticabold.ttf");
Typeface typeNormal = Typeface.createFromAsset(getAssets(), "fonts/helvetica.ttf");

SimpleAdapter adapter = new SimpleAdapter(this, items,R.layout.yourLvLayout, new String[]{"title",
    "subtitle" }, new int[] { R.id.rowTitle,
    R.id.rowSubtitle }){
            @Override
        public View getView(int pos, View convertView, ViewGroup parent){
            View v = convertView;
            if(v== null){
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v=vi.inflate(R.layout.yourLvLayout, null);
            }
            TextView tv = (TextView)v.findViewById(R.id.rowTitle);
            tv.setText(items.get(pos).get("title"));
            tv.setTypeface(typeBold);
            TextView tvs = (TextView)v.findViewById(R.id.rowSubtitle);
            tvs.setText(items.get(pos).get("subtitle"));
            tvs.setTypeface(typeNormal);
            return v;
        }


};
listView.setAdapter(adapter);

where items is your ArrayList of Maps

hope that helps

千柳 2024-10-17 03:26:13

您不能这样做,因为您传递给 ArrayAdapter 的文本视图资源在每次使用时都会膨胀。

您需要创建自己的适配器并提供自己的视图。

您的适配器的一个示例可以是

public class MyAdapter extends BaseAdapter {

private List<Object>        objects; // obviously don't use object, use whatever you really want
private final Context   context;

public CamAdapter(Context context, List<Object> objects) {
    this.context = context;
    this.objects = objects;
}

@Override
public int getCount() {
    return objects.size();
}

@Override
public Object getItem(int position) {
    return objects.get(position);
}

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

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

    Object obj = objects.get(position);

    TextView tv = new TextView(context);
    tv.setText(obj.toString()); // use whatever method you want for the label
    // set whatever typeface you want here as well
    return tv;
}

}

然后您可以将其设置为这样

ListView lv = new ListView(this);
lv.setAdapter(new MyAdapter(objs));

希望这应该能让您继续下去。

You can't do it that way because the text view resource you pass to the ArrayAdapter is inflated each time it is used.

You need to create your own adapter and provide your own view.

An example for your adapter could be

public class MyAdapter extends BaseAdapter {

private List<Object>        objects; // obviously don't use object, use whatever you really want
private final Context   context;

public CamAdapter(Context context, List<Object> objects) {
    this.context = context;
    this.objects = objects;
}

@Override
public int getCount() {
    return objects.size();
}

@Override
public Object getItem(int position) {
    return objects.get(position);
}

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

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

    Object obj = objects.get(position);

    TextView tv = new TextView(context);
    tv.setText(obj.toString()); // use whatever method you want for the label
    // set whatever typeface you want here as well
    return tv;
}

}

And then you could set that as such

ListView lv = new ListView(this);
lv.setAdapter(new MyAdapter(objs));

Hopefully that should get you going.

千寻… 2024-10-17 03:26:13

看起来构造函数是错误的,

将其更改为:

public MyAdapter (Context context, List<Object> objects) {
    this.context = context;
    this.objects = objects;
}

它对我来说效果很好。

Looks like the constructor is wrong

change it to:

public MyAdapter (Context context, List<Object> objects) {
    this.context = context;
    this.objects = objects;
}

it worked well for me.

雪化雨蝶 2024-10-17 03:26:13

对 arrayadapters 尝试这样::

Typeface typeNormal = Typeface.createFromAsset(getAssets(), "roboto_lite.ttf");

timearray = new ArrayAdapter<String>(DetailsActivity.this,R.layout.floorrow,R.id.txt, flor) {
    public View getView(int pos, View convertView, android.view.ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.floorrow, null);
        }
        TextView tv = (TextView)v.findViewById(R.id.txt);
        tv.setText(flor.get(pos));
        tv.setTypeface(typeNormal);
        return v;
    }; 
};

lv_building.setAdapter(timearray);

Try like this for arrayadapters::

Typeface typeNormal = Typeface.createFromAsset(getAssets(), "roboto_lite.ttf");

timearray = new ArrayAdapter<String>(DetailsActivity.this,R.layout.floorrow,R.id.txt, flor) {
    public View getView(int pos, View convertView, android.view.ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.floorrow, null);
        }
        TextView tv = (TextView)v.findViewById(R.id.txt);
        tv.setText(flor.get(pos));
        tv.setTypeface(typeNormal);
        return v;
    }; 
};

lv_building.setAdapter(timearray);
北陌 2024-10-17 03:26:13

除了 Moisés Olmedo 的回应之外 - 不创建新类的替代变体:

    tf = Typeface.createFromAsset(getAssets(), fontPath);

    recordsAdapter = new SimpleCursorAdapter(this, R.layout.item1, cursor, from, to);

    recordsAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if (columnIndex == 1) {
                final TextView tv = (TextView) view;
                tv.setTypeface(tf);
            }
            return false;
        }
    });

In addition to the response of Moisés Olmedo - an alternative variant without creating a new class:

    tf = Typeface.createFromAsset(getAssets(), fontPath);

    recordsAdapter = new SimpleCursorAdapter(this, R.layout.item1, cursor, from, to);

    recordsAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if (columnIndex == 1) {
                final TextView tv = (TextView) view;
                tv.setTypeface(tf);
            }
            return false;
        }
    });
驱逐舰岛风号 2024-10-17 03:26:13

首先将字体文件复制并粘贴到 asset/fonts 文件夹中。
然后识别文本视图。

    Typeface font=Typeface.createFromAsset(activity.getAssets(), "fonts/<font_file_name>.ttf");
    holder.text.setTypeface(font);
    holder.text.setText("your string variable here");

First copy and paste the font files into assets/fonts folder.
Then identify the textview.

    Typeface font=Typeface.createFromAsset(activity.getAssets(), "fonts/<font_file_name>.ttf");
    holder.text.setTypeface(font);
    holder.text.setText("your string variable here");
娜些时光,永不杰束 2024-10-17 03:26:13

您可以像下面的步骤一样设置基本适配器可能会帮助您

通过 TypeFace 方法定义您的基本适配器

public class LessonAdapter extends BaseAdapter {

    private Context mContext;

    public LessonAdapter(Context mContext, ArrayList<String> titles) {
        super();
        this.mContext = mContext;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        if (titles!=null)
            return titles.size();
        else
            return 0;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = convertView;
        try
        {
            if (v == null) {
                v = inflater.inflate(R.layout.lesson_item, null);
            }
                TextView title = (TextView) v.findViewById(R.id.title);

                Typeface tf = Typeface.createFromAsset(getAssets(),
                        "fonts/Rabiat_3.ttf");
                title.setTypeface(tf);

                title.setText(titles.get(position).toString());     

        }
        catch (Exception e) {
            Log.d("searchTest", e.getMessage());
        }

        return v;
    }

}

您可以通过在资产中添加文件夹“字体”来设置字体,然后在“字体”文件夹中添加您的字体

,然后设置您的适配器

adapter = new LessonAdapter(LessonsTitle.this, titles);
    setListAdapter(adapter);

You can Set base Adapter like follow Steps May be help you

Define your Base Adapter

public class LessonAdapter extends BaseAdapter {

    private Context mContext;

    public LessonAdapter(Context mContext, ArrayList<String> titles) {
        super();
        this.mContext = mContext;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        if (titles!=null)
            return titles.size();
        else
            return 0;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = convertView;
        try
        {
            if (v == null) {
                v = inflater.inflate(R.layout.lesson_item, null);
            }
                TextView title = (TextView) v.findViewById(R.id.title);

                Typeface tf = Typeface.createFromAsset(getAssets(),
                        "fonts/Rabiat_3.ttf");
                title.setTypeface(tf);

                title.setText(titles.get(position).toString());     

        }
        catch (Exception e) {
            Log.d("searchTest", e.getMessage());
        }

        return v;
    }

}

by TypeFace Method you can set your font by add folder 'Fonts' in Assets then add your font in 'Fonts' Folder

then to set your adapter

adapter = new LessonAdapter(LessonsTitle.this, titles);
    setListAdapter(adapter);
猫瑾少女 2024-10-17 03:26:13

holder.txt_name.setTypeface(Typeface.createFromAsset(activity.getAssets(), "font/nasimbold.ttf"));

holder.txt_name.setTypeface(Typeface.createFromAsset(activity.getAssets(), "font/nasimbold.ttf"));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文