java - 以编程方式在android中更改textview颜色的新方法是什么
问题描述
XML
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
app:titleTextColor="#ffffff"
>
<TextView
android:id="@+id/name"
android:layout_width="275dp"
android:layout_height="match_parent" />
</android.support.v7.widget.Toolbar>
活动
公共类聊天扩展 AppCompatActivity {
private TextView name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
name = (TextView) findViewById(R.id.name);
TextView textView = new TextView(R.layout.activity_chat);
name.setText((getIntent().getStringExtra("Recievers_Name")));
name.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
我试过了
name.setTextColor(getResources().getColor(R.color.white));
name.setTextColor(color.WHITE);
name.setTextColor(Color.parseColor("#FFFFFF"));
但无法正常工作......请帮助...................................... .....................
解决方案
试试这个方法,它在所有 Api 级别上都可以正常工作。
public int _getColor(int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 23) {
return ContextCompat.getColor(this, id);
} else {
return getResources().getColor(id);
}
}
推荐阅读
- vue.js - Vue CLI 3 - 有没有办法查看 HTMLWebpackPlugin 的 templateParameters 函数内容?
- python-3.x - Networkx:为给定的一组节点创建一个完整的图
- python - UnpicklingError: 无效的加载键,'\xfe'
- azure - QnA maker - REST API 和预览页面之间的不同结果
- r - 有谁知道为什么要在 beta 中集成大量的 R 中断?
- c - 使用子矩阵和 MPI 并行化计算矩阵中出现的 C 代码
- android - 二进制 XML 文件第 18 行:膨胀类 android.support.v4.widget.view.ViewPager 时出错
- arrays - MongoDB - 对带有 GeoJSON 元素的数组的 AND 请求
- javascript - React:将状态从一个组件导入另一个组件?
- python - 分组多索引上的滚动平均值(扩展其窗口)