如何将自定义字体应用于我的ListView项目?

时间:2015-08-12 20:33:24

标签: android android-layout android-listview android-fonts

这是我的主要java代码:

public class MainActivity extends ActionBarActivity {
    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);
        ListView listView = (ListView) findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);
    }
}

我的 activity_main.xml 文件:

<LinearLayout 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"
    android:orientation="vertical"
    android:background="#ffff"
    tools:context=".ListActivity" >

    <ListView
        android:id="@+id/mobile_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

我的 activity_listview.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"
    android:textColor="#0060FF">
</TextView>

1 个答案:

答案 0 :(得分:0)

 1.First make Custom ArrayAdapter
 2.In custom ArrayAdaper override getView()
 3.inflate TextView Layout in it.
 4. Assign Your Custom Font which is in Your asset Folder
    Typeface font1=Typeface.createFromAsset(getAssets(), "/fonts/UNDERCOV.ttf");

    button_1=(Button)findViewById(R.id.button1);
    button_1.setTypeface(font1);