Android 以编程方式将颜色、字体设置为文本视图
我已经创建了一个列表视图,每个列表视图项目中有3个项目,使用两个单独的.xml 1) list_view.xmlAndroid 以编程方式将颜色、字体设置为文本视图,android,android-layout,Android,Android Layout,我已经创建了一个列表视图,每个列表视图项目中有3个项目,使用两个单独的.xml 1) list_view.xml <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ListView"> <
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ListView">
</ListView>
我怀疑这可能是因为我没有将setContentView设置为list_item.xml。
有办法访问这些元素吗?如果是,如何设置?按照下面的代码设置颜色
String text = "<font color=#8469af>By tapping Checkout, you agree to our </font> <font color=#ffffff>terms.</font>";
tvTerms.setText(Html.fromHtml(text));
String text=“点击Checkout,表示您同意我们的条款。”;
setText(Html.fromHtml(text));
当您尝试调用findviewbyid时,您尚未创建listitems,因此它返回空指针
如果要使用标准字体设置字体面,请使用
android:typeface="serif"
但是如果你坚持用编程的方式来做
首先从列表中获取列表项,然后
(TextView) list_item.findViewById(R.id.textView);
正如Talha指出的,在
this
上调用findViewById
时出现异常,您的id textView不存在,因此返回null
如果要以编程方式修改列表项,应实现自己的适配器并在方法中应用修改
TextView
具有设置颜色的方法:public void setTextColor(int color)
为什么要麻烦使用笨重的HTML?抱歉……但实际上这是为了为单个文本视图设置不同的颜色。您是否也可以发布异常日志?谢谢
String text = "<font color=#8469af>By tapping Checkout, you agree to our </font> <font color=#ffffff>terms.</font>";
tvTerms.setText(Html.fromHtml(text));
android:typeface="serif"
(TextView) list_item.findViewById(R.id.textView);