如何在AppCompat中将Action Bar标题获取为TextView?

5
我正在尝试将标题作为TextView获取,但它没有起作用:
TextView title = (TextView) findViewById(R.id.action_bar_title);

当标题为空时,如何使用AppCompat获取标题并显示在TextView中?


1
你正在使用工具栏吗? - rahul.ramanujam
@r7v 不,我没有使用工具栏。 - krystian71115
@r7v 工具栏和操作栏之间有什么区别吗? - krystian71115
1
你的使用场景是什么? - rahul.ramanujam
6个回答

8

通常情况下,在我的情况下使用工具栏时,如果我要自定义标题,我会手动填充标题视图,然后在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);

来源于获取AppCompat v7 r21中ActionBar标题TextView


操作栏和工具栏有区别吗? - krystian71115
1
你可以在官方博客找到相关内容,涉及预装应用程序的材料设计。 - Shekhar
1
Android开发人员需要更多地控制ActionBar,而Toolbar正是为此而生。换句话说,ActionBar现在成为了一种特殊类型的Toolbar。 - Shekhar
1
你还应该查看Google设计规范 - Shekhar

4

暴力破解似乎是一种解决方法:

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

}

2

以下是从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");
        }
     }
}

1

我正在使用AppCompatActivity,这对我很有效

val titleId :Int = resources.getIdentifier("title","id",BuildConfig.APPLICATION_ID )
        val title :TextView = findViewById(titleId)

Layout Inspector


我正在使用 androidX 库,并且 ToolBar 中包含标题的 TextViewmID = -1 - isabsent

0

工具栏中的textView是通过代码添加的,因此您无法通过视图ID找到它。

解决方案

使用Kotlin,您可以通过以下方式找到标题textView。

toolbar.forEachIndexed { _, v ->
    if (v is TextView && v.text == "Your title name") {
        v.setOnClickListener {
            // ...
        }
        return@forEachIndexed
    }
}

0

嘿,感谢大家的帮助,在这里是我最终采用的实现方案。

Example Font Set

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接