我想更改ListView中列表项的字体。我为此创建了一个自定义项目,因为我想让用户从该活动中查看数据库的基本信息。如何更改视图的字体?
MainActivity.java
//Method in that fetch the data to list view
private void displayListView() {
final DatabaseHandler db = new DatabaseHandler(this);
Cursor cursor = db.fetchClinicByName("");
// The desired columns to be bound
String[] columns = new String[] { Constants.CLINIC_ID, Constants.CLINIC_NAME, Constants.CLINIC_ADD };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.textviewId, R.id.tv_ClinicName, R.id.tv_ClinicAdd };
simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.attribute_clinic, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.lv_clinic);
listView.setAdapter(simpleCursorAdapter);
registerForContextMenu(listView);
attribute_clinic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clinic Name:"
android:textSize="20sp"
android:layout_marginRight="40dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_ClinicName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="19sp"
android:text="clinic"/>
</TableRow>
<TableRow
android:id="@+id/tableRow01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clinic Address:"
android:textSize="20sp"
android:layout_marginRight="20dp"
android:textStyle="bold"
android:paddingBottom="5dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="@+id/tv_ClinicAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="19sp"
android:text="clinicAdd"
android:paddingBottom="5dp"
android:layout_marginBottom="5dp" />
</TableRow>
</LinearLayout>
<TextView
android:id="@+id/textviewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="114dp"
android:layout_marginTop="16dp"
android:visibility="gone" />
答案 0 :(得分:1)
我还没有尝试过这个,但我相信这应该可行。
您需要覆盖SimpleCursorAdapter的setViewText()
。当您的字体位于assets / fonts文件夹中时,它会是这样的。
private void displayListView() {
final DatabaseHandler db = new DatabaseHandler(this);
Cursor cursor = db.fetchClinicByName("");
// The desired columns to be bound
String[] columns = new String[] { Constants.CLINIC_ID, Constants.CLINIC_NAME, Constants.CLINIC_ADD };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.textviewId, R.id.tv_ClinicName, R.id.tv_ClinicAdd };
simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.attribute_clinic,
cursor, columns, to, 0) {
@Override
public void setViewText(TextView v, String text) {
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/yourfont.ttf");
v.setTypeface(face);
v.setText(text);
}
};
ListView listView = (ListView) findViewById(R.id.lv_clinic);
listView.setAdapter(simpleCursorAdapter);
registerForContextMenu(listView);