如何获得工具栏的标题文本颜色?

时间:2019-07-19 10:27:47

标签: android kotlin

在我当前的项目中,我正在尝试将工具栏的颜色及其“标题文本”从当前颜色转换为新颜色。但是我在工具栏中找不到toolbar.getTitleTextColor()之类的函数,但是有一个可以设置颜色的函数。

这是我的布局

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:alpha="0.7"
    android:id="@+id/bottombarAppbar"
    android:layout_height="wrap_content">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/bottombarToolbar"
        app:navigationIcon="@drawable/ic_hide"
        app:title="Categories"
        app:titleTextColor="#fafafa"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>

下面的语句由于未定义而导致错误。

bottombarToolbar.getTitleTextColor()

我有什么想念吗?

1 个答案:

答案 0 :(得分:3)

这就是您想要的:

public int getTitleTextColor(Toolbar toolbar) {
            if (toolbar != null) {
                for (int i = 0; i < toolbar.getChildCount(); i++) {
                    View child = toolbar.getChildAt(i);
                    if (child instanceof TextView) {
                        return ((TextView) child).getCurrentTextColor();
                    }
                }
            }
            return 0;
}
相关问题