0

如何为已经创建的列表视图(下面的代码)实现 getView() (如何创建 getview() )。这样我就可以更改 textVIew 项目的字体和进一步的属性 stylig

MainActivity.java

public class MainActivity extends Activity {

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

ListView listView1 = (ListView) findViewById(R.id.listView1);

String[] items = { "some", "fancy", "items", "to", "show" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.listitem, items);

 listView1.setAdapter(adapter);
 }

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
 tools:context=".MainActivity" >

<ListView 
  android:id="@+id/listView1" 
  android:layout_height="fill_parent"
  android:layout_width="fill_parent" />

 </RelativeLayout>

列表视图.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:textSize="20sp"
android:padding="22dp"
android:gravity="center_horizontal"
/>

更改 MainActivity.java

  public class MainActivity extends Activity {
   Context context;
    ArrayAdapter<String> adapter;
     String[]  items = { "some", "fance", "rock", "shot", "trance" };
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView1 = (ListView) findViewById(R.id.listView1);




    adapter = new ArrayAdapter<String>(this,
                R.layout.genre_list, items);

    listView1.setAdapter(adapter);}
    public View getView(int position, View convertView, ViewGroup parent) {

    View row;

    if (null == convertView) {
         LayoutInflater mInflater = (LayoutInflater) context
                 .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    row = mInflater.inflate(R.layout.genre_list, null);

    } else {

    row = convertView;

    }

    TextView tv = (TextView)row.findViewById(android.R.id.title);
    Typeface font = Typeface.createFromAsset(getAssets(),"roboto.ttf");  
    Toast.makeText(getApplicationContext(), "font fetched!",  
         Toast.L  ENGTH_LONG).show();
    tv.setText(items[position]);
    tv.setTypeface(font);
    return row;

    }
4

2 回答 2

0

getView()是适配器类中的一个方法,如 ArrayAdapter 和 SimpleCursorAdapter。您必须扩展一个适配器类,在您的情况下为 ArrayAdapter,才能访问getView().

这个由 Android 首席程序员 Romain Guy 撰写的Google Talk详细讨论了扩展适配器的最佳实践。


也就是说,您可以通过 XML 更改字体和许多其他样式属性,您不一定需要创建自定义适配器。

于 2013-01-16T18:23:02.373 回答
0

要更改 TextView 的字体,您需要创建自定义 ArrayAdapter。但是你可以通过重写默认的 getView 方法来做到这一点:

Typeface font = Typeface.createFromAsset(getAssets(),
                                 "yourfontname.ttf");

listView1.setListAdapter(new 
              ArrayAdapter<string>(this,R.layout.listitem, items) {

@Override

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

View row;

if (null == convertView) {

row = mInflater.inflate(R.layout.listitem, null);

} else {

row = convertView;

}

TextView tv = (TextView)row.findViewById(android.R.id.text1);

tv.setText(items[position]);
tv.setTypeface(font);
return row;

}

});
于 2013-01-16T18:24:05.393 回答