【问题标题】:Change font in my listView在我的 listView 中更改字体
【发布时间】:2014-08-18 17:41:27
【问题描述】:

所以我的这个 listView 让我很头疼。它运行良好,但我似乎无法更改 lV 中文本的字体。

这是我的代码:

public class MyActivity3 extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my3);
    Button m = (Button) findViewById(R.id.button3);
    tv = (TextView) findViewById(R.id.textViewcat);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    tv.setTypeface(typeface);


    String listArray[] = new String[] { "India", "England", "Canada",
            "New zealand", "South Africa", "Pakistan", "West indies" };
    int icon[] = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher };

    ListView listView = (ListView) findViewById(R.id.listView);
    List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

    for (int i = 0; i <= listArray.length - 1; i++) {

        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("title", listArray[i]);
        hm.put("icon", Integer.toString(icon[i]));
        aList.add(hm);
    }

    String[] sfrm = { "title", "icon" };
    int[] sto = { R.id.title, R.id.list_image};

    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
            R.layout.row_layout, sfrm, sto);



    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                                int position, long id) {

            switch (position) {

                case 0:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 4:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 5:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 6:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;

            }

        }
    });
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.animation8, R.anim.animation7);
    }
}

row_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >


<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:padding="3dip" >

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/ic_launcher" />
</LinearLayout>



<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Country name"
    android:textColor="#FFFFFF"
    android:textSize="30sp"
    android:typeface="sans" />

</RelativeLayout>

这就是我的代码,你可以看到有一个 id 为“title”的文本视图。如何更改标题的字体?如果您根据我的代码回答问题,我真的很感激,因为我已经尝试了所有其他解决方案,但没有成功

【问题讨论】:

标签: android listview


【解决方案1】:

这里是问题的详细解释。

http://javatechig.com/android/using-external-fonts-in-android-view

基本上对你来说,因为它在一个列表视图中,我建议你创建自定义文本视图。将字体文件放在 assets 文件夹中很重要。

说您尝试了所有其他来源都没有成功是错误的,因为上面的链接看起来像第二个谷歌搜索结果并且工作正常。

【讨论】:

    【解决方案2】:

    嗨,克里斯蒂亚诺,

    我想我找到了您更改字体的问题。

    At your code:
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    tv.setTypeface(typeface);
    
    Change like below and try out:
    Typeface typeface = Typeface.createFromAsset(getAssets(), "customfont.ttf");
    tv.setTypeface(typeface);
    

    请将 ttf 文件名更改为任何小写字母且字母之间没有空格的名称,然后试一试即可。

    【讨论】:

    • 这是另一个 textView,字体在该电视上工作正常。我需要的是在 row_layout.xml 中声明为自定义字体的 textView
    • 然后使用 baseadapter 而不是简单的适配器,以便您可以在 getView() 方法中进行自定义。
    • 类 customAdapter 扩展 BaseAdapter{ @override Public void getView(){ 。 . . TextView text=(textView)findViewById(R.id.title); text.setTypeface(tv); } }
    【解决方案3】:

    您必须创建一个自定义适配器,并在 getView 方法中,将字体放到您的视图中:

    public class ListAdapter extends ArrayAdapter<Item> {
    
    public ListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }
    
    public ListAdapter(Context context, int resource, List<Item> items) {
        super(context, resource, items);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        View v = convertView;
    
        if (v == null) {
    
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.itemlistrow, null);
    
        }
        TextView tv = (TextView) v.findViewById(R.id.tv_name);
        tv.setTypeface(typeFace);
        ...
    }
    

    【讨论】:

    • 为您的 listView 创建您自己的自定义适配器。您不会使用 SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.row_layout, sfrm, sto);不再是 CustomAdapter 适配器 = 新的 CustomAdapter...(这里我将我的 CustomAdapter 命名为 ListAdapter)。这是一个教程:androidexample.com/…
    【解决方案4】:

    试试这个方法,希望能帮助你解决问题。

    public class MyActivity3 extends Activity {
        private TextView tv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my3);
            Button m = (Button) findViewById(R.id.button3);
            tv = (TextView) findViewById(R.id.textViewcat);
            Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
            tv.setTypeface(typeface);
    
    
            String listArray[] = new String[] { "India", "England", "Canada",
                    "New zealand", "South Africa", "Pakistan", "West indies" };
            int icon[] = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,
                    R.drawable.ic_launcher, R.drawable.ic_launcher,
                    R.drawable.ic_launcher, R.drawable.ic_launcher,
                    R.drawable.ic_launcher };
    
            ListView listView = (ListView) findViewById(R.id.listView);
            ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
    
            for (int i = 0; i <= listArray.length - 1; i++) {
    
                HashMap<String, String> hm = new HashMap<String, String>();
                hm.put("title", listArray[i]);
                hm.put("icon", Integer.toString(icon[i]));
                aList.add(hm);
            }
    //
    //        String[] sfrm = { "title", "icon" };
    //        int[] sto = { R.id.title, R.id.list_image};
    //
    //        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
    //                R.layout.row_layout, sfrm, sto);
    
    
            listView.setAdapter(new CustomAdapter(this,aList));
    
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> arg0, View view,
                                        int position, long id) {
    
                    switch (position) {
    
                        case 0:
                            Toast.makeText(getApplicationContext(), "",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case 1:
                            Toast.makeText(getApplicationContext(), "",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case 2:
                            Toast.makeText(getApplicationContext(), "",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case 3:
                            Toast.makeText(getApplicationContext(), "",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case 4:
                            Toast.makeText(getApplicationContext(), "",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case 5:
                            Toast.makeText(getApplicationContext(), "",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case 6:
                            Toast.makeText(getApplicationContext(), "",
                                    Toast.LENGTH_SHORT).show();
                            break;
    
                    }
    
                }
            });
        }
    
        @Override
        public void onBackPressed() {
            super.onBackPressed();
            overridePendingTransition(R.anim.animation8, R.anim.animation7);
        }
    
        class CustomAdapter extends BaseAdapter{
            private Context context;
            private ArrayList<HashMap<String,String>> data;
            public CustomAdapter(Context context,ArrayList<HashMap<String,String>> data){
                this.context=context;
                this.data=data;
            }
            @Override
            public int getCount() {
                return data.size();
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public Object getItem(int position) {
                return data.get(position);
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder;
                if(convertView==null){
                    viewHolder =new ViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.row_layout,null);
                    viewHolder.list_image = (ImageView) convertView.findViewById(R.id.list_image);
                    viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                    Typeface typeface = Typeface.createFromAsset(getAssets(), "yourcustomfontname.ttf");
                    viewHolder.title.setTypeface(typeface);
                    convertView.setTag(viewHolder);
                }else{
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                viewHolder.title.setText(data.get(position).get("title"));
                viewHolder.list_image.setImageResource(Integer.parseInt(data.get(position).get("icon")));
                return null;
            }
    
            class ViewHolder{
                ImageView list_image;
                TextView title;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      • 2011-01-28
      • 2019-03-03
      相关资源
      最近更新 更多