我试图找出正确的方法来使用自定义字体的工具栏标题,并在工具栏中心(客户端需求)。
目前,我正在使用好的老ActionBar,我正在设置标题为空值,并使用setCustomView把我的自定义字体TextView和中心使用ActionBar. layoutparams。
有更好的办法吗?使用新的工具栏作为我的动作栏。
我试图找出正确的方法来使用自定义字体的工具栏标题,并在工具栏中心(客户端需求)。
目前,我正在使用好的老ActionBar,我正在设置标题为空值,并使用setCustomView把我的自定义字体TextView和中心使用ActionBar. layoutparams。
有更好的办法吗?使用新的工具栏作为我的动作栏。
当前回答
尝试在单独的视图中使用工具栏和标题。在右端取一个视图,并赋予它们等于工具栏权重的权重。这样你的小个子就会放在中间。
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:background="@color/white_color">
<LinearLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white_color">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:background="@color/white_color"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:layout_weight="0.2"
app:contentInsetStartWithNavigation="0dp"
app:navigationIcon="@color/greyTextColor">
</android.support.v7.widget.Toolbar>
<com.an.customfontview.CustomTextView
android:id="@+id/headingText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:gravity="center"
android:text="Heading"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/keyboard_number"
android:layout_gravity="center_horizontal|center_vertical"
app:textFontPath="fonts/regular.ttf" />
<ImageView
android:id="@+id/search_icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:visibility="visible"
android:layout_weight="0.2"
android:layout_gravity="center_horizontal|center_vertical"
android:src="@drawable/portfolio_icon"/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
其他回答
定义以下类:
public class CenteredToolbar extends Toolbar {
private TextView centeredTitleTextView;
public CenteredToolbar(Context context) {
super(context);
}
public CenteredToolbar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CenteredToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setTitle(@StringRes int resId) {
String s = getResources().getString(resId);
setTitle(s);
}
@Override
public void setTitle(CharSequence title) {
getCenteredTitleTextView().setText(title);
}
@Override
public CharSequence getTitle() {
return getCenteredTitleTextView().getText().toString();
}
public void setTypeface(Typeface font) {
getCenteredTitleTextView().setTypeface(font);
}
private TextView getCenteredTitleTextView() {
if (centeredTitleTextView == null) {
centeredTitleTextView = new TextView(getContext());
centeredTitleTextView.setTypeface(...);
centeredTitleTextView.setSingleLine();
centeredTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
centeredTitleTextView.setGravity(Gravity.CENTER);
centeredTitleTextView.setTextAppearance(getContext(), R.style.TextAppearance_AppCompat_Widget_ActionBar_Title);
Toolbar.LayoutParams lp = new Toolbar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
centeredTitleTextView.setLayoutParams(lp);
addView(centeredTitleTextView);
}
return centeredTitleTextView;
}
}
...然后使用它来代替常规工具栏,就像这样:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent">
<your.packagename.here.CenteredToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:title="@string/reset_password_page_title"/>
<!-- Other views -->
</RelativeLayout>
在Activity中你仍然需要这两行代码(和标准工具栏一样):
Toolbar toolbar = (Toolbar) findViewByid(R.id.toolbar); // note that your activity doesn't need to know that it is actually a custom Toolbar
setSupportActionBar(binding.toolbar);
就是这样!您不需要隐藏标准的左对齐标题,不需要重复相同的XML代码,等等,只需使用CenteredToolbar,如果它是默认的工具栏。您还可以通过编程方式设置您的自定义字体,因为您现在可以直接访问TextView。希望这能有所帮助。
因为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);
这是我用一个导航图标和一个文本视图所做的 现在您可以进行扩展,以便在任何需要的地方应用它。但你必须把它应用到每一项活动中
(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
}
}
材料组件,从版本1.4开始。如文档中所述,你可以使用MaterialToolbar。
只需在MaterialToolbar上添加属性app:titleCentered和/或app:subtitleCentered属性为true即可。
喜欢的东西:
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topAppBar"
app:titleCentered="true"
... />
使用Material3包撰写,你可以简单地使用CenterAlignedTopAppBar:
CenterAlignedTopAppBar(
title = { Text("Centered TopAppBar") },
navigationIcon = {
IconButton(onClick = { /* doSomething() */ }) {
Icon(
imageVector = Icons.Filled.Menu,
contentDescription = "Localized description"
)
}
}
)
如果您正在使用Compose和Material2包,没有内置组件,但您可以自定义TopAppBar内内容的布局,如本文所述。
工具栏标题是可设置样式的。你所做的任何定制都必须在主题中进行。我给你们举个例子。
工具栏布局:
<?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>