在简单适配器列表视图中更改字体样式
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);
像这样创建自定义文本视图class-
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" />
这样,您就可以添加任何您想要的字体样式了。
如何更改列表视图中的字体样式?我不知道我应该怎么做才能改变我的字体样式谢谢你的帮助。
如何更改列表视图中的字体样式?我不知道我应该怎么做才能改变我的字体样式谢谢你的帮助。
这是我的代码:
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);
像这样创建自定义文本视图class-
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" />
这样,您就可以添加任何您想要的字体样式了。