本文介绍了findViewById返回action_bar_title空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置自定义的字体为动作条的标题:How设置自定义字体的动作条标题?。因此,在活动的onCreate

  INT titleId = getResources()则getIdentifier(action_bar_title,ID,机器人)。
TextView的yourTextView =(TextView中)findViewById(titleId);
yourTextView.setTextColor(getResources()的getColor(R.color.black));
yourTextView.setTypeface(面);
 

findViewById 返回。为什么呢?

我使用的是支持库:

 进口android.support.v7.app.ActionBarActivity;
 

解决方案

看来,在棒棒堂打破。不知道是否有一个最好的选择,但你可以做这样的事情,如果你使用的工具栏:

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

进口org.apache.commons.lang3.reflect.FieldUtils;

公共类MyToolbar扩展工具栏{

    受保护的TextView mmTitleTextView;

    公共MyToolbar(上下文的背景下){
        超(上下文);
    }

    公共MyToolbar(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }

    公共MyToolbar(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
    }

    // API

    公众的TextView getTitleTextView(){
        如果(mmTitleTextView == NULL){
            尝试 {
                mmTitleTextView =(TextView中)FieldUtils.readField(这一点,mTitleTextView,真正的);
            }赶上(抛出:IllegalArgumentException E){
                e.printStackTrace();
            }赶上(IllegalAccessException E){
                e.printStackTrace();
            }
        }

        返回mmTitleTextView;
    }
}
 

和您的活动:

 私人MyToolbar mActionBarToolbar;
受保护的工具栏getActionBarToolbar(){
    如果(mActionBarToolbar == NULL){
        mActionBarToolbar =(FWToolbar)findViewById(R.id.toolbar_actionbar);
        如果(mActionBarToolbar!= NULL){
            setSupportActionBar(mActionBarToolbar);
        }
    }
    返回mActionBarToolbar;
}
@覆盖
公共无效的setContentView(INT layoutResID){
    super.setContentView(layoutResID);

    getActionBarToolbar();
}
保护无效onPostCreate(包savedInstanceState){
    super.onPostCreate(savedInstanceState);

    如果(mActionBarToolbar!= NULL){
        字样字体= Typeface.createFromAsset(c.getAssets(),字体/ font.ttf);
        。mActionBarToolbar.getTitleTextView()setTypeface(字体);
    }
}
 

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;
解决方案

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

这篇关于findViewById返回action_bar_title空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 08:46