便宜VPS主机精选
提供服务器主机评测信息

Android setTextolor全控件适用性详解:哪些控件能改色?

setTextColor 是 Android 中用于设置文本颜色的方法,它主要应用于 TextView 及其子类(如 ButtonEditText 等)控件。对于其他类型的控件,如 ImageViewProgressBar 等,setTextColor 方法是不适用的。

如果你需要为其他类型的控件设置颜色,你需要使用相应的方法。例如:

  • 对于 ImageView,你可以使用 setColorFilter 方法设置颜色遮罩:

    ImageView imageView = findViewById(R.id.imageView);
    imageView.setColorFilter(ContextCompat.getColor(context, R.color.your_color));
    
  • 对于 ProgressBar,你可以使用 setProgressTintListsetIndeterminateTintList 方法设置进度条的颜色:

    ProgressBar progressBar = findViewById(R.id.progressBar);
    ColorStateList progressTintList = ContextCompat.getColorStateList(context, R.color.your_progress_color);
    progressBar.setProgressTintList(progressTintList);
    
    ColorStateList indeterminateTintList = ContextCompat.getColorStateList(context, R.color.your_indeterminate_color);
    progressBar.setIndeterminateTintList(indeterminateTintList);
    
未经允许不得转载:便宜VPS测评 » Android setTextolor全控件适用性详解:哪些控件能改色?