![](/img/trans.png)
[英]How to change the font size of specific text in strings.xml android
[英]How to change font,size of text and set text position center ToolBar Android?
我需要更改字體大小、字體本身,並將工具欄中的文本居中。 我從底部導航活動工作室獲取了一個模板。
我正在嘗試在themes.xml中設置文本大小(我能夠在那里更改背景顏色和文本顏色)
<style name="CustomToolBarStyle" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="titleTextColor">@color/black</item>
<item name="android:background">@color/white</item>
我試圖在themes.xml中使用這個選項:
<item name="android:gravity">center</item>
它沒有幫助。 我嘗試按照以下說明進行操作: https: //stackoverflow.com/questions/26533510/android-toolbar-center-title-and-custom-font Throws android.support.v7.widget.Toolbar 出錯並崩潰。
對於 API 級別 21+,您可以使用工具欄的app:titleTextAppearance
屬性
<android.support.v7.widget.Toolbar
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="?actionBarSize"
android:id="@+id/toolbar"
app:titleTextAppearance="@style/yourstyle" />
並以自定義樣式覆蓋默認標題大小:
<style name="yourstyle" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">18sp</item>
<item name="android:fontFamily">yourFont</item>
</style>
對於居中,您應該遵循這個答案,如果這個答案和其他答案都不適用於同一篇文章https://stackoverflow.com/a/38175403/9917400 ,而不僅僅是在工具欄中使用線性布局。
您可以在工具欄內設置約束布局
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_collapseMode="pin">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:textColor="@color/blue"
android:textSize="16sp"
app:fontPath="fonts/xxx" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
如果您使用的是androidx
,請不要使用android.support.v7.widget.Toolbar
。
您必須使用androidx.appcompat.widget.Toolbar
。
在 res/layout 中創建toolbar.xml
。 把這段代碼:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="yourToolBarBackgroundColor"
>
<TextView
android:id="@+id/toolbar_title"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TestToolBar"
android:fontFamily="yourFont"
android:textSize="yourSizeOfText"
android:textColor="yourTextColor" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/activity_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
在 Themes.xml 中禁用 ActionBar:
<style name="Theme.YourProject" parent="Theme.MaterialComponents.DayNight.NoActionBar">
對於夜間主題,您必須在 theme.xml(night) 中做同樣的事情
然后在 MainActivity.java 中 onCreate:
setContentView(R.layout.toolbar);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.