简体   繁体   English

如何在 android 的操作栏中更改标题的 fonts

[英]How to change the fonts of Title in Action bar in android

I want to change the Fonts of my title in the action bar.我想在操作栏中更改我的标题的 Fonts。 I have set the title programatically, in xml it was not showing any effect.我以编程方式设置了标题,在 xml 中没有显示任何效果。 Now I want to change the font of that title.现在我想更改该标题的字体。 I saw some post related to this, but It was not having the problem which I am facing.我看到了一些与此相关的帖子,但它没有遇到我面临的问题。

Here is my code.这是我的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //SETTING ITS LAYOUT
    setContentView(R.layout.activity_start);
Typeface typeface = getResources().getFont(R.font.dancingscriptregular);


    //SETTING TITLE OF THE APP IN BLUE COULOR
    getSupportActionBar().setTitle(Html.fromHtml("<font color='#1F9FD9'>Expense Manager <font>"));

  ...

 }

Now what next?, how to use the typeface to set the font of my title.现在下一步是什么?,如何使用字体来设置我的标题的字体。

Please Help!请帮忙!

In androidMenifest.xml在 androidMenifest.xml

<activity
        android:parentActivityName=".StartActivity"
        android:name=".ExpenseActivity"
        android:fitsSystemWindows="true"
        android:label="@string/expenseActivity" <!--This Line is not working-->
        android:screenOrientation="portrait"
        android:textColor="@color/batteryChargedBlue"
        android:theme="@style/ExpenseTheme" />
    <activity

ExpenseTheme费用主题

<style name="ExpenseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/white</item>
    <item name="colorPrimaryDark">@color/batteryChargedBlue</item>
    <item name="colorAccent">@color/greenBlue</item>
    <item name="android:navigationBarColor">@color/batteryChargedBlue</item>

</style>

activity_start.xml Code activity_start.xml 代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".StartActivity">

<LinearLayout
    android:id="@+id/part_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_above="@+id/part_2"
    android:gravity="center"
    >

    <TextView
        android:id="@+id/welcome"
        android:gravity="center"
        android:layout_marginBottom="20dp"
        android:textColor="@color/greenBlue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/caviar_dreams_bold"
        android:text="Welcome !!!"
        android:textSize="55dp"


        />

    <TextView
        android:id="@+id/info"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:textColor="@color/greenBlue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/dancingscriptregular"
        android:text="@string/info"
        android:textSize="35dp"
        />

    <EditText
        android:id="@+id/imaEt"
        android:layout_marginTop="60dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/enterIMA"
        android:background="@drawable/border_and_lines_tp"
        android:padding="15dp"
        android:textColorHint="@color/greenBlue"
        android:fontFamily="@font/caviar_dreams_bold"
        android:layout_gravity="center"
        android:inputType="numberDecimal"
        />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/imaBtn"
        android:layout_width="110dp"
        android:layout_height="55dp"
        android:background="@color/batteryChargedBlue"
        android:theme="@style/ButtonTheme"
        android:text="Enter"
        android:textSize="15sp"
        android:gravity="center"
        android:layout_marginTop="15dp"
        android:onClick="setInitialMonthlyAmount"
        app:cornerRadius="5dp"
        android:textColor="@color/white"/>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/imaSkip"
        android:layout_width="75dp"
        android:layout_height="50dp"
        android:background="@color/batteryChargedBlue"
        android:theme="@style/AppTheme"
        android:text="Skip"
        android:textSize="15sp"
        android:gravity="center"
        android:layout_marginTop="15dp"
        android:onClick="setOnSkipClicked"
        app:cornerRadius="5dp"
        android:textColor="@color/white"/>

    <!-- <Button
         android:id="@+id/imaBtn"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:layout_marginTop="15dp"
         android:background="@drawable/borders_and_lines"
         android:onClick="setInitialMonthlyAmount"
         android:text="Enter"
         android:textColor="#FFF" /> -->

    <!-- <Button
         android:id="@+id/imaSkip"
         android:layout_gravity="center"
         android:layout_marginTop="15dp"
         android:text="Continue >>"
         android:textColor="#FFF"
         android:onClick="setOnSkipClicked"
         android:background="@drawable/borders_and_lines"
         android:layout_width="100dp"
         android:layout_height="30dp"
         /> -->



</LinearLayout>

<LinearLayout
    android:id="@+id/part_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <TextView
        android:id="@+id/help"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/help"
        android:textColor="@color/greenBlue"
        android:fontFamily="@font/dancingscriptregular"
        android:textStyle="bold"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        android:gravity="right"
        android:textSize="20dp"
        android:clickable="true"/>

</LinearLayout>
</RelativeLayout>

This my actual layout and I have achieved by using the following file, which I have posted.这是我的实际布局,我通过使用我发布的以下文件来实现。 now I just want to change the font of the black circled text.现在我只想更改黑色圆圈文本的字体。

You have different options.你有不同的选择。
In your activity you can use something like in your layout:在您的活动中,您可以在布局中使用类似的东西:

   <com.google.android.material.appbar.AppBarLayout>

        <com.google.android.material.appbar.MaterialToolbar
            style="@style/MyToolbar"
            ...
            />

    </com.google.android.material.appbar.AppBarLayout>

Then you can define a custom style:然后您可以定义自定义样式:

  <!-- Toolbar -->
  <style name="MyToolbar" parent="Widget.MaterialComponents.Toolbar">
    <item name="titleTextAppearance">@style/My_TextAppearance_Toolbar</item>    
  </style>

  <style name="My_TextAppearance_Toolbar" parent="@style/TextAppearance.MaterialComponents.Headline6">
    <item name="fontFamily">....</item>
    <item name="android:fontFamily">....</item>
    <item name="android:textStyle">bold|italic</item>
  </style>

在此处输入图像描述

Otherwise you can also use the Toolbar API :否则,您也可以使用Toolbar API

   toolbar.setTitle("Material Title");
   toolbar.setTitleTextAppearance(this,R.style.My_TextAppearance_Toolbar);

Another option is to use app:titleTextAppearance attribute in your layout:另一种选择是在布局中使用app:titleTextAppearance属性:

  <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:titleTextAppearance="@style/My_TextAppearance_Toolbar"
            .../>

Just use these code:只需使用以下代码:

if you are using "Holo Theme" then use this:如果您使用的是“全息主题”,请使用:

    int titleId = getResources().getIdentifier("action_bar_title", "id",
            "android");
    TextView yourTextView = (TextView) findViewById(titleId);
    yourTextView.setTextColor(getResources().getColor(R.color.black));
   Typeface yourTypeface = Typeface.createFromAsset(getAssets(), "fonts/your_font.ttf");
   //or get from resource
   yourTextView.setTypeface(yourTypeface);

if you are using "Material Theme" then use this:如果您使用的是“材料主题”,请使用:

Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
TextView textView1 = (TextView) toolbar.getChildAt(0);//title
TextView textView2 = (TextView) toolbar.getChildAt(1);//subtitle
textView1.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
textView2.setTextColor(getResources().getColor(R.color.colorAccent));
   Typeface yourTypeface1 = Typeface.createFromAsset(getAssets(), "fonts/your_font1.ttf");
   Typeface yourTypeface2 = Typeface.createFromAsset(getAssets(), "fonts/your_font2.ttf");
   //or get from resource
   textView1.setTypeface(yourTypeface1);
   textView2.setTypeface(yourTypeface2);

it works and it is very simple!它有效,而且非常简单! screen shot截屏

The best way is to create a custom toolbar.最好的方法是创建自定义工具栏。 Stay with me...跟我在一起...

in xml layout(activity_start.xml), you add code below to xml file:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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"
    tools:context=".activity.YourActivity"
    android:orientation="vertical"
    android:paddingBottom="5dp">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar" (**id of toolbar**)
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" (**color of background**)
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways"
            android:layoutDirection="rtl"> (**direction-rtl-ltr**)

            <android.support.v7.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Start Activity" (**text for display on toolbar**)
                android:textSize="23dp" (**text size**)
                android:fontFamily="@font/yekan" (**font of text :D**)
                android:textColor="#ffffff" (**text color**)
                (**And any other code you like for textview :)*)
            />

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

    </android.support.design.widget.AppBarLayout>
 </LinearLayout>

This way you can have an actionbar with a font without writing code in the Java file.这样您就可以拥有一个带有字体的操作栏,而无需在 Java 文件中编写代码。

Add code below to java file: And to use the toolbar in Java code you can get it with code below:将以下代码添加到 java 文件中: 要使用 Java 代码中的工具栏,您可以使用以下代码获取它:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

EDIT: If you encounter an error while using AppBarLayout , Create BasicActivity Then follow the steps above编辑:如果您在使用AppBarLayout时遇到错误,请创建BasicActivity然后按照上述步骤操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM