我正在尝试将标题作为TextView获取,但它没有起作用:
TextView title = (TextView) findViewById(R.id.action_bar_title);
当标题为空时,如何使用AppCompat获取标题并显示在TextView中?
TextView title = (TextView) findViewById(R.id.action_bar_title);
当标题为空时,如何使用AppCompat获取标题并显示在TextView中?
通常情况下,在我的情况下使用工具栏时,如果我要自定义标题,我会手动填充标题视图,然后在XML中设置其属性。工具栏的目的是防止发生这种情况,因此您可以更多地控制工具栏的外观。
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_height="@dimen/standard_vertical_increment"
android:layout_width="match_parent"
android:minHeight="@dimen/standard_vertical_increment"
android:background="@drawable/actionbar_background">
<TextView
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Red" />
</android.support.v7.widget.Toolbar>
Toolbar toolbar = findViewById(R.id.my_awesome_toolbar);
//Get rid of the title drawn by the toolbar automatically
toolbar.setTitle("");
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
toolbarTitle.setTextColor(Color.BLUE);
暴力破解似乎是一种解决方法:
private void findTextViewTitle() {
String title = "title";
ActionBar ab = getSupportActionBar();
ab.setTitle(title);
Window window = getWindow();
View decor = window.getDecorView();
ArrayList<View> views = new ArrayList<View>();
decor.findViewsWithText(views, title, View.FIND_VIEWS_WITH_TEXT);
for (View view : views) {
Log.d(TAG, "view " + view.toString());
}
TextView tvTitle = (TextView) decor.findViewById(views.get(0).getId());
tvTitle.setBackgroundColor(Color.RED);
}
以下是从ActionBar中获取TextView的方法。该方法确保即使ActionBar方向发生变化,也能选择TextView。如果您需要,还可以获取图标 - 只需检查child instanceof ImageView。
//If you just want to set the text
ActionBar actBar = getSupportActionBar();
if(actBar != null) {
actBar.setTitle(R.string.your_ab_title);
}
//If you want to customize more than the text
Toolbar ab = findViewById(R.id.action_bar);
if(ab != null){
for (int i= 0; i < ab.getChildCount(); i++){
if(ab.getChildAt(i) instanceof TextView) {
TextView title = (TextView) ab.getChildAt(i);
//You now have the title textView. Do something with it
title.setText("Your Custom AB Title");
}
}
}
工具栏中的textView是通过代码添加的,因此您无法通过视图ID找到它。
使用Kotlin,您可以通过以下方式找到标题textView。
toolbar.forEachIndexed { _, v ->
if (v is TextView && v.text == "Your title name") {
v.setOnClickListener {
// ...
}
return@forEachIndexed
}
}
嘿,感谢大家的帮助,在这里是我最终采用的实现方案。
// USAGE
Toolbar yourToolbar = (Toolbar) findViewById(R.id.your_toolbar);
TextView actionTitle = ActionBarTitle(toolbar);
actionTitle.setTypeface(font("Snake"));
// IMPLEMENTATION
private TextView ActionBarTitle(Toolbar toolbarForRead)
{
TextView title = null;
if (toolbarForRead != null)
{
for (int i= 0; i < toolbarForRead.getChildCount(); i++)
{
if (toolbarForRead.getChildAt(i) instanceof
TextView)
{
title = (TextView)
toolbarForRead.getChildAt(i);
return title;
}
}
}
return null;
}
private Typeface font(String fontName)
{
return Typeface.createFromAsset(this.getAssets(), "fonts/" + fontName + ".ttf");
}