如何为已经创建的列表视图(下面的代码)实现 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;
}