【问题标题】:Set font for ListView, Android为 ListView,Android 设置字体
【发布时间】:2014-01-02 08:35:02
【问题描述】:

我花了一整天的时间寻找答案,找到了很多,但都不是我需要的。 所以,我有一个非常简单的 ListView 菜单和一个带有 TextView 的项目文件。我想为菜单中的项目设置 Segoe-Print 字体,我知道,我需要适配器,但我做不到。救命!!!

菜单.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">

    <LinearLayout android:id="@+id/menu_top"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/menu_top">
       </LinearLayout>

<ListView 
          android:id="@+id/ListView_Menu"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="@drawable/fon_android"
          android:divider="@drawable/menu_line"
          android:textStyle="bold" >
</ListView>
</LinearLayout>

menu_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_prop"
android:layout_width="fill_parent"
android:textSize="19px"
android:text="test string"
android:layout_gravity="left"
android:layout_height="wrap_content"
android:shadowRadius="5"
android:textColor="@color/menu_color"
android:shadowDy="3"
android:shadowDx="3" />

Main_Menu.java 的一部分:

public class Main_menu extends Activity {
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
        String[] items = { 
                getResources().getString(R.string.system),
                getResources().getString(R.string.convert),
                getResources().getString(R.string.forks),
                getResources().getString(R.string.margin)};
        ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,R.layout.menu_item, items);
    menuList.setAdapter(adapt);
}

CustomAdapter.java:

public class CustomAdapter extends ArrayAdapter<String> {

       public CustomAdapter(Context context, int resource, String[] objects) {
    super(context, resource, objects);
    // TODO Auto-generated constructor stub
}

    public View getView (int position, View convertView, ViewGroup parent) {
              View view = super.getView(position, convertView, parent);
              TextView textView = (TextView) view.findViewById(R.id.menu_prop);
              Typeface font = Typeface.createFromAsset(getAssets(), "segoe_print.ttf"); 
              textView.setTypeface(font);
            return textView;
       }

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

字体没有变化...

【问题讨论】:

    标签: java android listview fonts


    【解决方案1】:

    在 xml 中尝试:

    android:fontFamily="somefont.ttf"
    

    从 android 4.1+ 开始,以下字体可用:http://robotofont.com/

    android:fontFamily="sans-serif"  
    android:fontFamily="sans-serif-thin"        
    android:fontFamily="sans-serif-light"     
    android:fontFamily="sans-serif-condensed" 
    

    您还可以通过编程方式设置字体:

    TextView t = (TextView) findViewById(R.id.name);
    Typeface font = Typeface.createFromAsset(getAssets(),"font/somefont.ttf");
    t.setTypeface(font);
    

    将字体文件放入文件夹中...

    【讨论】:

    • 谢谢,但是 - 不,不,它不起作用。我已经在文件夹“assets”中有一个带有字体的文件。但是您的代码 - 它用于 TextView。我需要 ListView 的代码。它应该使用一个特殊的适配器,但是这个适配器“看起来”如何——我不知道。
    • 你应该创建自定义适配器
    • @user2877972 您不需要自定义适配器来更改字体。在列表项上设置TextView 属性就足够了。 (TextViews 在ListView 内。您没有在包含ListView 上设置属性,而是在实际的TextView 上设置属性。)
    【解决方案2】:

    这是自定义适配器的代码:

    private class CostumiseAdapter extends ArrayAdapter<item>{
    
    public CostumiseAdapter(ArrayList<item> items) {
     super(getActivity(), 0, items);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
     if(convertView == null){
    convertView = getActivity().getLayoutInflater().inflate(R.layout.scroll_fragment, null);
    }
    
    Item item = getItem(position);
    
    TextView text = (TextView) convertView.findViewById(R.id.text_view);
    text.setText(item.mString);
    
    RadioButton b1 = (RadioButton) convertView.findViewById(R.id.radio_b1);
    b1.setChecked(item.mB1);
    
    RadioButton b2 = (RadioButton) convertView.findViewById(R.id.radio_b2);
    b2.setChecked(item.mB2);
    
    CheckBox cB = (CheckBox) convertView.findViewById(R.id.check_box);
    cB.setChecked(item.mB2);
    
    return convertView;
    }
    }
    

    我从 http://androide-examples.blogspot.com/2013/11/android-scroll-list.html 获取完整示例。

    在这个例子中有一些语义错误。所以你必须将一些项目修复为项目(大写)......

    【讨论】:

    • @user2877972 这对您有帮助吗?
    • 看起来好像我绕过紧闭的门,我只需要靠边停车,但在尝试时不要推。今天我会仔细阅读文档-适配器机制还没有理解。
    【解决方案3】:

    您需要创建一个自定义适配器来设置自定义字体。这是最小的代码:

      public class CustomAdapter extends ArrayAdapter<String> {
       //Contructor here
    
           public View getView (int position, View convertView, ViewGroup parent) {
                  View view = super.getView(position, convertView, parent);
                  TextView textView = (TextView) view.findViewById(R.id.myTextView);
                 // Here you set the typeface as you do for the TextView 
                  textView.setTypeFace(....);
           }
      }
    

    这应该为您的 Listview 提供自定义字体。

    【讨论】:

    • 出了点问题...请看第一篇文章 - 我编辑了它。
    【解决方案4】:

    我找到了目的地! 好吧,首先我为适配器创建另一个类:

    public class Typefaces {
        private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
        public static Typeface get(Context c, String name) {
        synchronized (cache) {
        if (!cache.containsKey(name)) {
        String path = "fonts/"+name;
        try {
        Typeface t = Typeface.createFromAsset(c.getAssets(), path);
        cache.put(name, t);
        } catch (Exception e) {
        e.printStackTrace();
        }
        }
        return cache.get(name);
        }
        }
        }
    

    然后在主适配器中我添加了两个字符串:

    Typeface tf = Typefaces.get(getContext(), "tahoma.ttf");
            holder.txtTitle.setTypeface(tf);
    

    就是这样!它正在工作! )) 谢谢大家的帮助和解答。

    【讨论】:

      【解决方案5】:
      view.txtViewTitle.setTypeface(Typeface.createFromAsset(parent
                      .getContext().getAssets(), "fonts/DroidSerif.ttf"));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-17
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        • 2016-03-24
        相关资源
        最近更新 更多