簡體   English   中英

在simpleadapter listview中更改字體樣式

[英]Change fontstyle in simpleadapter listview

如何更改列表視圖中的字體樣式? 我不知道該怎么做才能更改我的字體樣式。謝謝您的幫助。

如何更改列表視圖中的字體樣式? 我不知道該怎么做才能更改我的字體樣式。謝謝您的幫助。

這是我的代碼:

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);   

創建一個像這樣的自定義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" />

這樣,您就可以添加所需的任何字體樣式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM