findViewById returns null for action_bar_title

2019-02-26 00:15发布

I tried to set custom font to ActionBar title: How to Set a Custom Font in the ActionBar Title?. So in Activity 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);

but findViewById returns null. Why?

I'm using support library:

import android.support.v7.app.ActionBarActivity;

2条回答
再贱就再见
2楼-- · 2019-02-26 00:27

It seems that's broken in Lollipop. Don't know if there's a best alternative but you can do something like this, if you're using Toolbar:

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;
    }
}

And on your activity:

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);
    }
}
查看更多
叼着烟拽天下
3楼-- · 2019-02-26 00:45

I had a similar problem while trying to set a content description for action bar title, using the support libs. The answer here works for me: how to reference ActionBar title View on Lollipop

You can retrieve the TextView that you want indirectly, because the title TextView has no resource id, like this:

Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
TextView textView = (TextView) toolbar.getChildAt(0);
查看更多
登录 后发表回答