安卓工具栏标题居中和自定义字体

422

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

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

有更好的方法吗?可以使用新的Toolbar作为我的ActionBar。


MaterialToolbar支持居中标题栏,还支持更改字体,详见:https://material.io/components/app-bars-top/android#theming-the-top-app-bar - Maher Abuthraa
41个回答

1
您可以在工具栏中使用自定义的TextView,例如:
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title" />


</android.support.v7.widget.Toolbar>

所以,这将居中文本。如果您想向普通的工具栏添加自定义字体,请使用<style>

<style android:name="ToolbarFont">
    <item android:fontFamily = "@font/fontName" />
</style>

并将其添加到工具栏:

toolbar.setTitleTextAppearance(this, R.style.ToolbarFont);

对于工具栏中的文本视图,您可以使用fontFamily属性进行定义:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title"
        android:fontFamily="@font/fontFamily" />


</android.support.v7.widget.Toolbar>

1

我发现了一种无需额外的Java/Kotlin代码即可添加自定义工具栏的方法。

  • First: create a XML with your custom toolbar layout with AppBarLayout as the parent:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.AppBarLayout                     
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    
        <ImageView
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginEnd="@dimen/magin_default"
            android:src="@drawable/logo" />
    
    </android.support.v7.widget.Toolbar>
    

  • Second: Include the toolbar in your layout:

    <?xml version="1.0" encoding="utf-8"?>                
    <android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/blue"
        tools:context=".app.MainAcitivity"
        tools:layout_editor_absoluteY="81dp">
    
        <include
            layout="@layout/toolbar_inicio"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <!-- Put your layout here -->
    
    </android.support.constraint.ConstraintLayout>
    

1

我遇到了同样的问题,在MainActivity中进行以下修复

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

在Fragment中

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    if (view == null) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_example, container, false);
        init();
    }
    getActivity().setTitle("Choose Fragment");
    return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.example_menu, menu);
}

0

尝试

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

0

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

(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
            }

        }

0

我不知道是否有人遇到这个问题,但是使用额外的文本视图很糟糕,因为你必须自行管理标题,而使用反射则更糟糕,因为在添加progurad规则时ID会发生变化。最简单和准确的解决方法是使用材料工具栏,它具有居中标题的属性。

app:titleCentered

0

设置 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" />

11
不支持使用androidx.appcompat.widget.Toolbar - tmm1

0

只需应用此主题:

     <style name="MyToolbarStyle" parent="Widget.AppCompat.Toolbar">
    <item name="android:gravity">center_vertical</item>
    <item name="navigationIconTint">@color/white</item>
    <item name="tint">@color/white</item>
    <item name="iconTint">@color/white</item>
</style>

-1
  @Composable
    fun topAppBar() {
        CenterAlignedTopAppBar(
            title = {
                Text(
                    "Top App Bar"
                )
            },
            navigationIcon = {
                IconButton(onClick = { /*TODO*/ }) {
                    Icon(
                        imageVector = Icons.Default.ArrowBack,
                        contentDescription = "Back"
                    )
                }
            },
            actions = {
                IconButton(onClick = { /*TODO*/ }) {
                    Icon(
                        imageVector = Icons.Default.MoreVert,
                        contentDescription = "More",
                    )
                }
            },
            colors = TopAppBarDefaults.smallTopAppBarColors(
                containerColor = MaterialTheme.colorScheme.secondaryContainer
            )
        )
    }
}

-4
你只需要在工具栏布局中添加以下行:
app:contentInsetStart="0dp"

请检查我的新回答。希望能有所帮助。 - Diya Bhat

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