Android Hacks:ListView为空时显示个性字体

ListView的状态要么显示很多条数据要么为空。为空时为了体现UX一般需要提供点友好信息,可以通过setEmptyView(View)方法为ListView设置一个视图当ListView为空时。显示提示信息使用系统的字体显得不够cool,这里还可以用到ttf字体来显示很Cool的字体。 关键代码就是这点:
Typeface face = Typeface.createFromAsset(getAssets(), "huakangwawati.ttf");
tvFillEmptyListView.setTypeface(face);
lvDemo.setEmptyView(tvFillEmptyListView);
下面是一个实例:
// 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"
    tools:context=".MainActivity" >

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

    <TextView
        android:paddingTop="15dp"
        android:gravity="center_horizontal"
        android:id="@+id/tv_fill_emptylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="还没有聊天记录哦"
        android:visibility="gone" />

</LinearLayout>
//MainActivity
package me.waye.demo;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private ListView lvDemo;
    private TextView tvFillEmptyListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lvDemo = (ListView) findViewById(R.id.lv_demo);
        tvFillEmptyListView = (TextView) findViewById(R.id.tv_fill_emptylist);
        Typeface face = Typeface.createFromAsset(getAssets(), "huakangwawati.ttf");
        tvFillEmptyListView.setTypeface(face);
        lvDemo.setEmptyView(tvFillEmptyListView);

        lvDemo.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] {}));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
上面用到的字体我放在了百度网盘上,需要的童鞋可以去这儿下: http://pan.baidu.com/s/1otQOx  

你可能感兴趣的:(android,ListView,typeface,hacks)