如何为已创建的列表视图(下面的代码)实现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>
listview.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;
}
答案 0 :(得分:0)
getView()
是Adapter类中的一个方法,如ArrayAdapter和SimpleCursorAdapter。您必须扩展Adapter类(在您的情况下为ArrayAdapter)才能访问getView()
。
Android首席程序员Romain Guy的Google Talk讨论了非常详细地扩展适配器的最佳做法。
那说你可以通过XML更改字体和许多其他样式属性,你不一定需要创建自定义适配器。
答案 1 :(得分:0)
要更改TextView的字体,您需要创建Custom 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;
}
});