本文介绍了android更改listview字体和颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了很多不同的方法来实现这一点,但我不确定哪种方法最适合我的场景.

I've found loads of different ways of accomplishing this but I'm not sure what's the best for my scenario.

这是我用于列表视图的 Java 代码:

This is my Java code for the listview:

ListView lv;
lv = (ListView) findViewById(R.id.favList);

这是列表的xml代码:

This is the xml code for the list:

<ListView
        android:id="@+id/favList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="20dp"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:listSelector="@android:color/transparent" >
    </ListView>

对于文本视图,我会添加:

For a text view I would add:

final Typeface fontList = Typeface.createFromAsset(assets, "optima-extra-black.ttf");
lv.setTypeface(fontList);

但这不适用于列表视图.在这种情况下如何更改字体?

But this doesn't work for listviews.How do I change my font in this case?

好的,我快到了...我需要访问我的资产,但我不能从我的自定义适配器中访问.我尝试使用 final AssetManager assets = this.getAssets(); 但这不会让我更进一步..

Oke I'm almost there...I need to access my assets but I can't from within my custom adapter.I tried using final AssetManager assets = this.getAssets(); but that won't get me any further..

如何解决这个问题?

    class Myadapter extends BaseAdapter {

    LayoutInflater lif;
    ImageView sideArrow;
    TextView tv;


    public Myadapter(Context ctx) {
        lif = (LayoutInflater) ctx
                .getSystemService(LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {

        return favarets.size();
    }

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

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

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

        View vi = convertView;
        if (convertView == null)
            vi = lif.inflate(R.layout.inflate, null);
        sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);

        tv = (TextView) vi.findViewById(R.id.textFav);
        tv.setText(favarets.get(position));
        final AssetManager assets = this.getAssets();
        final Typeface tvFont = Typeface.createFromAsset(assets, "OPTIMA.TTF");
        tv.setTypeface(tvFont);

        tv.setTextColor(Color.BLACK);

        return vi;

推荐答案

我找到了解决方案 :D

I found out the solution :D

    public class Myadapter extends BaseAdapter {

    AssetManager assetManager = getAssets();

    LayoutInflater lif;
    ImageView sideArrow;
    TextView tv;


    public Myadapter(Context ctx) {
        lif = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE);


    }

    @Override
    public int getCount() {

        return favarets.size();
    }

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

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


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

        View vi = convertView;
        if (convertView == null)
            vi = lif.inflate(R.layout.inflate, null);
        sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);


        tv = (TextView) vi.findViewById(R.id.textFav);
        tv.setText(favarets.get(position));

        final Typeface tvFont = Typeface.createFromAsset(assetManager, "OPTIMA.TTF");
        tv.setTypeface(tvFont);
        tv.setTextColor(Color.BLACK);

        return vi;
    }

}

这篇关于android更改listview字体和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:44