以编程方式更改Gingerbread中的Actionbar标题颜色

Ste*_*hen 1 android android-actionbar

我需要以编程方式更改操作栏标题文本颜色.

我使用以下代码以稍微不同的方式设置颜色,具体取决于SDK版本

int textColor = getResources().getColor(android.R.color.white);
    if (Build.VERSION.SDK_INT >= 14) {
        // Version 4+
        int titleId = getResources().getIdentifier("action_bar_title", "id", "android");
        TextView abTitle = (TextView) findViewById(titleId);
        if (abTitle != null) {
            abTitle.setTextColor(textColor);
        }
    } else {
        // Other versions
        TextView abTitle = (TextView) getWindow().findViewById(android.R.id.title);
        if (abTitle != null) {
            abTitle.setTextColor(textColor);
        }
    }
Run Code Online (Sandbox Code Playgroud)

它适用于SDK值为14或更高的设备.但是,对于Gingerbread设备,abTitle始终为null,因此颜色不会设置.

anyony有什么建议吗?

Bla*_*elt 6

if/else错了,应该是这样的

 int titleId;
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      titleId = getResources().getIdentifier("action_bar_title", "id", "android");
  } else {
      titleId = R.id.action_bar_title;
  }
Run Code Online (Sandbox Code Playgroud)

然后你应该能够直接通过它 findViewById