如何将自定义ListViews字体更改为自定义字体?

时间:2016-03-01 00:47:14

标签: java android listview fonts

这是我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#E0E0E0"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ccb.lldm.lldmhimnario.Cantos">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:elevation="25dp"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#303F9F"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:layout_width="wrap_content"
            android:textColor="#E0E0E0"
            android:text="Cantos"
            android:id="@+id/toolbarCantos"
            android:textSize="25sp"
            android:layout_height="wrap_content" />

    </android.support.v7.widget.Toolbar>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ListView"
    android:layout_below="@+id/toolbar"
    android:layout_alignParentBottom="true">

</ListView>


</RelativeLayout>

这是我的列表视图。

这是我的自定义列表视图xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#E0E0E0"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:text="Canto: "
    android:background="@drawable/text_ripple"
    android:textColor="#303F9F"
    android:id="@+id/textCanto"
    android:textSize="25sp"
    android:layout_height="45dp" />

   </LinearLayout>

这是我的java文件:

public class Cantos extends AppCompatActivity {

ListView lv;
Context context;

ArrayList cantoList;
public static String[] cantos = {"1: Abre Tu Oido", "2: A Cristo Quiero", "3: Acerquese Mi Clamor", "4: A Cristo Yo Alabare",
        "5: Acude Dios", "6: Adelante", "7: A Dios Canto", "8: Adios Para Siempre", "9: Ahora Senor", "10: A Jesucristo Ven",
        "11: Alabad A Dios"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cantos);
    initTypeface();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(null);

    context = this;
    lv = (ListView)findViewById(R.id.ListView);
    lv.setAdapter(new CustomAdapter(this, cantos));

}

private void initTypeface() {

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/AftaSerifThin-Regular.otf");
    TextView text = (TextView) findViewById(R.id.toolbarCantos);
    text.setTypeface(myTypeface);

}

}

这是我的最后一个Java文件:

public class CustomAdapter extends BaseAdapter {

String[] result;
Context context;
private static LayoutInflater inflater = null;


public CustomAdapter(Cantos cantos, String[] cantos1) {

    result = cantos1;
    context = cantos;
    inflater = (LayoutInflater)context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    return result.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

public class Holder
{
    TextView tv;
}

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

    Holder holder = new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.cantos_list, null);
    holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
    holder.tv.setText(result[position]);


    return rowView;
}
}

我是Android的新手,我已经掌握了它,但我需要这方面的帮助。提前谢谢!

2 个答案:

答案 0 :(得分:0)

如果要为ListView项自定义字体,则应尝试在getView()中将字体设置为 CustomAdaptor.java 类; 看看这段代码:

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

    Holder holder = new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.cantos_list, null);
    holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
    holder.tv.setText(result[position]);

    Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf");
    holder.tv.setTypeface(myTypeface);

    return rowView;
}

答案 1 :(得分:0)

您可以随时更改适配器的getView()中的TypeFace,但如果您要在代码的不同部分多次使用该字体,则可以随时扩展TextView,您可以参考:

Using custom font in android TextView using xml

这样,只要您需要使用不同的字体,就可以使用它而不是布局文件中的常规TextView。