如何从ListView获取TextView来设置字体? Android的

时间:2014-03-30 11:29:24

标签: android listview textview

我正在处理Android应用的列表视图。请告诉我如何将我的字体用于列表中的TextView

Typeface typeface;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

    String fontPath = "fonts/28.ttf";
    typeface = Typeface.createFromAsset(getAssets(), fontPath); 

    ListView menuList = (ListView) findViewById(R.id.ListView_Menu);

    String[] items = {
            getResources().getString(R.string.menu_item_play),
            getResources().getString(R.string.menu_item_settings),
            getResources().getString(R.string.menu_item_help),
            getResources().getString(R.string.menu_item_exit)
    };



    ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items);
    menuList.setAdapter(adapt);

    //View v1=(View)menuList.getItemAtPosition(0);

1 个答案:

答案 0 :(得分:2)

  1. 创建一个包含带有id的TextView的row.xml,例如:文本

  2. 创建自定义适配器并覆盖getView()方法,以便在行中找到textview并设置其字体。

  3. 这是我躺在的代码中的一个例子:

    @Override
        public View getView ( int position, View convertView, ViewGroup parent )
        {
            View resultView = convertView;
            TextView t = null;
    
            if ( resultView == null )
            {
                //row layout defined in xml containing a textview with id: row.
                LayoutInfalter inflater = LayotuInflater.from(context);
                resultView = inflater.inflate ( R.layout.row, null );
    
    
                t = resultView.findViewById(R.id.text);
                t.setTypeface(/*typeface here*/)
    
                resultView.setTag ( holder );
            }else
            {
                t = (TextView) resultView.findViewById(R.id.text);
            }
            t.setText(/*text for the item at this position in the list*/)
    
            return resultView;
        }