我试图找出正确的方法来使用自定义字体的工具栏标题,并在工具栏中心(客户端需求)。
目前,我正在使用好的老ActionBar,我正在设置标题为空值,并使用setCustomView把我的自定义字体TextView和中心使用ActionBar. layoutparams。
有更好的办法吗?使用新的工具栏作为我的动作栏。
我试图找出正确的方法来使用自定义字体的工具栏标题,并在工具栏中心(客户端需求)。
目前,我正在使用好的老ActionBar,我正在设置标题为空值,并使用setCustomView把我的自定义字体TextView和中心使用ActionBar. layoutparams。
有更好的办法吗?使用新的工具栏作为我的动作栏。
当前回答
因为android.support.v7。appcompat 24.2工具栏有方法setTitleTextAppearance,你可以设置它的字体没有外部textview。
在styles.xml中创建新样式
<style name="RobotoBoldTextAppearance">
<item name="android:fontFamily">@font/roboto_condensed_bold</item>
</style>
并使用它
mToolbar.setTitleTextAppearance(this, R.style.RobotoBoldTextAppearance);
其他回答
Try
@Override
public void onBackPressed() {
if(getTitle().equals(getResources().getString(R.string.app_name))) {
super.onBackPressed();}
else {
//set visiblity
}
}
布局:
<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="match_parent"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
代码:
Toolbar mToolbar = parent.findViewById(R.id.toolbar_top);
TextView mToolbarCustomTitle = parent.findViewById(R.id.toolbar_title);
//setup width of custom title to match in parent toolbar
mToolbar.postDelayed(new Runnable()
{
@Override
public void run ()
{
int maxWidth = mToolbar.getWidth();
int titleWidth = mToolbarCustomTitle.getWidth();
int iconWidth = maxWidth - titleWidth;
if (iconWidth > 0)
{
//icons (drawer, menu) are on left and right side
int width = maxWidth - iconWidth * 2;
mToolbarCustomTitle.setMinimumWidth(width);
mToolbarCustomTitle.getLayoutParams().width = width;
}
}
}, 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
}
}
工具栏标题是可设置样式的。你所做的任何定制都必须在主题中进行。我给你们举个例子。
工具栏布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
style="@style/ToolBarStyle.Event"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="@dimen/abc_action_bar_default_height_material" />
风格:
<style name="ToolBarStyle" parent="ToolBarStyle.Base"/>
<style name="ToolBarStyle.Base" parent="">
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
<style name="ToolBarStyle.Event" parent="ToolBarStyle">
<item name="titleTextAppearance">@style/TextAppearance.Widget.Event.Toolbar.Title</item>
</style>
<style name="TextAppearance.Widget.Event.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<!--Any text styling can be done here-->
<item name="android:textStyle">normal</item>
<item name="android:textSize">@dimen/event_title_text_size</item>
</style>
设置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" />