简体   繁体   English

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

[英]How to get TextView from ListView to set font? Android

I am working on a list view for an Android App. 我正在研究Android应用程序的列表视图。 Please tell me how to use my font for the TextView s from List? 请告诉我如何使用我的字体从列表中的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. Create a row.xml that contains a TextView with an id eg text 创建一个包含带有id(例如text)的TextView的row.xml

  2. Create a custom Adapter and override the getView() method such that you find the textview in the row and set its typeface. 创建自定义适配器并覆盖getView()方法,以便在行中找到textview并设置其字体。

This is an example from code I had lying around: 这是我躺在的代码中的一个例子:

@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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM