在简单适配器列表视图中更改字体样式

时间:2015-01-27 07:15:54

标签: android listview textview simpleadapter

如何更改listview中的fontstyle?我不知道如何改变我的fontstyle,谢谢你的帮助。

如何更改listview中的fontstyle?我不知道如何改变我的fontstyle,谢谢你的帮助。

这是我的代码:

public class ListViewFrame extends Activity {
 SharedPreferences sharedpreferences;

// Array of strings storing country names
String[] countries = new String[] { "Frame1", "Frame2", "Frame3", "Frame4", };

// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[] { R.drawable.framesone, R.drawable.framestwo,
        R.drawable.framesthree, R.drawable.framesfour, };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
    setContentView(R.layout.acmain); 
    sharedpreferences = this.getSharedPreferences(AppConstant.MyPREFERENCES,
            Context.MODE_PRIVATE);
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>(); 


    for(int i=0;i<4;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("txt", countries[i]);
        hm.put("flag", Integer.toString(flags[i]) );            
        aList.add(hm);        
    }
    // Keys used in Hashmap
    String[] from = { "flag","txt" };

    // Ids of views in listview_layout
    int[] to = { R.id.flag,R.id.txt}; 


    // Instantiating an adapter to store each items
    // R.layout.listview_layout defines the layout of each item
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
  //  setFontTextView(this,);

    // Getting a reference to listview of main.xml layout file
    ListView listView = ( ListView ) findViewById(R.id.listview);

    // Setting the adapter to the listView
    listView.setAdapter(adapter);   

1 个答案:

答案 0 :(得分:0)

创建一个这样的自定义textview类 -

public class CustomFontTextview extends TextView
{
    public CustomFontTextview(Context context) 
    {
        super(context);
        init();
    }

    public CustomFontTextview(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        init();
    }

    public CustomFontTextview(Context context, AttributeSet attrs, int defStyle) 
    {
        super(context, attrs, defStyle);
        init();
    }

    public void init() 
    {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/YourFontStyle.ttf");
        this.setTypeface(tf);
    }
}

然后,在每个项目的布局中,即在listview_layout中,进行此更改 -

<your.package.CustomFontTextview
    android:id="@+id/word_list_row_textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/margin_5"
    android:paddingEnd="@dimen/margin_5"
    android:paddingStart="@dimen/margin_10"
    android:paddingTop="@dimen/margin_5"
    android:text="@string/app_name"
    android:textColor="@android:color/black"
    android:textSize="@dimen/text_16" />

这样,您就可以添加任何所需的字体样式。