Android findViewById为action\u bar\u title返回null

Android findViewById为action\u bar\u title返回null,android,android-actionbar,android-support-library,Android,Android Actionbar,Android Support Library,我尝试将自定义字体设置为ActionBar标题:。因此,在活动onCreate中: int titleId = getResources().getIdentifier("action_bar_title", "id", "android"); TextView yourTextView = (TextView) findViewById(titleId); yourTextView.setTextColor(getResources().getColor(R.color.black)); yo

我尝试将自定义字体设置为ActionBar标题:。因此,在活动
onCreate
中:

int titleId = getResources().getIdentifier("action_bar_title", "id", "android");
TextView yourTextView = (TextView) findViewById(titleId);
yourTextView.setTextColor(getResources().getColor(R.color.black));
yourTextView.setTypeface(face);
但是
findViewById
返回
null
。为什么?

我正在使用支持库:

import android.support.v7.app.ActionBarActivity;

看起来棒棒糖坏了。不知道是否有最好的选择,但如果您使用工具栏,您可以这样做:

import android.content.Context;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import org.apache.commons.lang3.reflect.FieldUtils;

public class MyToolbar extends Toolbar {

    protected TextView mmTitleTextView;

    public MyToolbar(Context context) {
        super(context);
    }

    public MyToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyToolbar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    // API

    public TextView getTitleTextView() {
        if (mmTitleTextView == null) {
            try {
                mmTitleTextView = (TextView) FieldUtils.readField(this, "mTitleTextView", true);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return mmTitleTextView;
    }
}
关于你的活动:

private MyToolbar mActionBarToolbar;
protected Toolbar getActionBarToolbar() {
    if (mActionBarToolbar == null) {
        mActionBarToolbar = (FWToolbar) findViewById(R.id.toolbar_actionbar);
        if (mActionBarToolbar != null) {
            setSupportActionBar(mActionBarToolbar);
        }
    }
    return mActionBarToolbar;
}
@Override
public void setContentView(int layoutResID) {
    super.setContentView(layoutResID);

    getActionBarToolbar();
}
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    if (mActionBarToolbar != null) {
        Typeface typeface = Typeface.createFromAsset(c.getAssets(), "fonts/font.ttf");
        mActionBarToolbar.getTitleTextView().setTypeface(typeface);
    }
}

我在尝试使用支持库为操作栏标题设置内容描述时遇到了类似的问题。这里的答案对我很有用:

您可以间接检索所需的TextView,因为标题TextView没有资源id,如下所示:

Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
TextView textView = (TextView) toolbar.getChildAt(0);

尝试使用getActionBar().findViewById()@Hareshchelana你猜呢?ActionBar类没有方法findViewById,而且我正在使用支持库。当仅使用findViewById时,请参考当前的活动布局,并尝试更改actionview TextView,以便可以使用getActionBar()引用将其作为findViewById。