简体   繁体   English

在simpleadapter listview中更改字体样式

[英]Change fontstyle in simpleadapter listview

How can i change the fontstyle in my listview? 如何更改列表视图中的字体样式? i dont know what should i do to change my fontstyle thankyou for your help. 我不知道该怎么做才能更改我的字体样式。谢谢您的帮助。

How can i change the fontstyle in my listview? 如何更改列表视图中的字体样式? i dont know what should i do to change my fontstyle thankyou for your help. 我不知道该怎么做才能更改我的字体样式。谢谢您的帮助。

this is my code: 这是我的代码:

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

Create a custom textview class like this- 创建一个像这样的自定义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);
    }
}

then, in your layout of each item, ie in listview_layout , make this change- 然后,在每个项目的布局中,即在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" />

by this, you will be able to add any font style you want. 这样,您就可以添加所需的任何字体样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM