gpt4 book ai didi

android - 改变textview的字体

转载 作者:搜寻专家 更新时间:2023-11-01 09:05:11 24 4
gpt4 key购买 nike

我有一个 ListView,我只想更改字体。我已经将我的字体下载到 assets/fonts 文件夹中。

如果我理解正确,我应该重写适配器..

这是我的代码:

onCreate....


Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/english.ttf");
font1 = Typeface.create(font1, Typeface.BOLD);

TextView customText = (TextView)findViewById(R.id.ListTextView);
customText.setTypeface(font1);



HashMap<String,String> contentItem = new HashMap<String,String>()
{
{
put(NAME_FIELD,"dudi");
}
};
content.add(contentItem);



SimpleAdapter adapter = new SimpleAdapter(this,
content,
R.layout.show_the_list,
new String[]{NAME_FIELD},
new int[]{R.id.ListTextView} );




ListView list1 = (ListView) findViewById(R.id.list);

list1.setAdapter(adapter);

xml文件:

<TextView
android:id="@+id/ListTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dip"
android:textColor="@color/White" >
</TextView>

如果有人能告诉我需要添加或更改的代码,那就太好了!谢谢。

最佳答案

实际上,当你使用R.id.ListTextView作为 SimpleAdapter 中的参数,然后适配器直接访问它以创建 View ,因此它每次都是新的,而不是您在 onCreate() 中定义的内容 Activity 的,所以要么使用 Custom TextView 并在 XML 中向其添加字体,然后将其用作 SimpleAdapter 中的参数,要么编写 CustomAdapter 并在getView()它使用 View inflater 重新定义 TextView 并在此方法中设置字体字体..

关于android - 改变textview的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12436529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com