Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 views
in Technique[技术] by (71.8m points)

android - how to reference ActionBar title View on Lollipop

I have an Activity which extends the AppCompatActivity and uses the support ActionBar. Compile SDK version is 23, test devices SDK are 21-22. I need to get the ActionBar's title TextView, however, the most known way of doing this

int resId = getResources().getIdentifier("action_bar_title", "id", "android");
return (TextView) findViewById(resId);

doesn't work starting with API 21. Is there a way to reference the title TextView on Lollipop?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With AppCompatActivity, You can retrieve the TextView that you want indirectly, because it seems that this TextView has no id, like this:

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

I used the Hierarchy Viewer tool to get the view tree. (Tool that you can find into Android Device Monitor under Hierarchy View perspective)

Action bar view tree


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...