【问题标题】:Android toolbar center title and custom fontAndroid 工具栏中心标题和自定义字体
【发布时间】:2014-12-19 10:35:09
【问题描述】:

我正在尝试找出使用自定义字体作为工具栏标题的正确方法,并将其置于工具栏中(客户要求)。

目前,我正在使用旧的 ActionBar,并将标题设置为空值,并使用 setCustomView 放置我的自定义字体 TextView 并使用 ActionBar.LayoutParams 将其居中。

有没有更好的方法来做到这一点?使用新的 Toolbar 作为我的 ActionBar。

【问题讨论】:

标签: android android-layout android-toolbar android-theme android-styles


【解决方案1】:

试试

@Override
    public void onBackPressed() {
          if(getTitle().equals(getResources().getString(R.string.app_name))) {
            super.onBackPressed();}
          else {
//set visiblity
           }
}

【讨论】:

    【解决方案2】:

    设置 android:gravity="center" 对我有用

    没有任何样式。工具栏基本上是一个ViewGroup,您只需在其中设置元素的重力即可。

            <android.support.v7.widget.Toolbar
                android:id="@+id/htab_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="top"
                android:background="@color/partial_transparent"
                android:gravity="center"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    

    【讨论】:

    • 不适用于androidx.appcompat.widget.Toolbar
    【解决方案3】:

    这就是我用一个导航图标和一个文本视图所做的 现在您可以进行扩展以将其应用到您需要的任何地方。但你必须将它应用到每个活动中

    (toolbar[0] as AppCompatTextView).let {
                it.viewTreeObserver.addOnDrawListener {
                    it.layoutParams = it.layoutParams.apply {
                        width = Toolbar.LayoutParams.MATCH_PARENT
                        (this as ViewGroup.MarginLayoutParams).apply {
                            marginEnd = toolbar[1].width
                        }
                    }
                    it.textAlignment = View.TEXT_ALIGNMENT_CENTER
                }
    
            }
    

    【讨论】:

      【解决方案4】:

      使用com.google.android.material.appbar.MaterialToolbarapp:titleCentered="true" 标签

      <com.google.android.material.appbar.MaterialToolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          app:titleCentered="true" />
      

      【讨论】:

        【解决方案5】:

        您只需在工具栏布局中添加以下行:

        app:contentInsetStart="0dp"
        

        【讨论】:

        • 检查我的新答案。希望对您有所帮助。
        【解决方案6】:
        Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf"); // your custom font
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTypeface(face);
        setSupportActionBar(toolbar);
        

        其他教程:

        1. Android Toolbar Example
        2. Using Custom Font in Android
        3. ActionBar Tutorial with Example

        【讨论】:

        • android.support.v7.widget.Toolbar 类没有 setTypeface 方法。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多