android 如何更改操作栏中标题的字体
How to change the fonts of Title in Action bar in android
我想在操作栏中更改标题的字体。我以编程方式设置了标题,在 xml 中它没有显示任何效果。现在我想更改该标题的字体。我看到了一些与此相关的 post,但它没有遇到我面临的问题。
这是我的代码。
@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 next what next?, 如何使用字体来设置我的标题的字体。
请帮忙!
在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
费用主题
<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代码
<?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.
只需使用这些代码:
如果您正在使用 "Holo Theme" 则使用此:
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);
如果您正在使用 "Material Theme" 则使用此:
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);
有效而且非常简单!
screen shot
最好的方法是创建自定义工具栏。
留在我身边...
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>
通过这种方式,您无需在 Java 文件中编写代码就可以获得带有字体的操作栏。
将以下代码添加到 java 文件:
要在 Java 代码中使用工具栏,您可以使用以下代码获取它:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
编辑:如果您在使用 AppBarLayout
时遇到错误,请创建 BasicActivity
然后按照上述步骤操作
你有不同的选择。
在您的 activity 中,您可以在布局中使用类似的东西:
<com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.appbar.MaterialToolbar
style="@style/MyToolbar"
...
/>
</com.google.android.material.appbar.AppBarLayout>
然后你可以自定义样式:
<!-- 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>
否则你也可以使用Toolbar
API:
toolbar.setTitle("Material Title");
toolbar.setTitleTextAppearance(this,R.style.My_TextAppearance_Toolbar);
另一种选择是在布局中使用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"
.../>
我想在操作栏中更改标题的字体。我以编程方式设置了标题,在 xml 中它没有显示任何效果。现在我想更改该标题的字体。我看到了一些与此相关的 post,但它没有遇到我面临的问题。
这是我的代码。
@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 next what next?, 如何使用字体来设置我的标题的字体。
请帮忙!
在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
费用主题
<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代码
<?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.
只需使用这些代码:
如果您正在使用 "Holo Theme" 则使用此:
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);
如果您正在使用 "Material Theme" 则使用此:
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);
有效而且非常简单! screen shot
最好的方法是创建自定义工具栏。 留在我身边...
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>
通过这种方式,您无需在 Java 文件中编写代码就可以获得带有字体的操作栏。
将以下代码添加到 java 文件: 要在 Java 代码中使用工具栏,您可以使用以下代码获取它:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
编辑:如果您在使用 AppBarLayout
时遇到错误,请创建 BasicActivity
然后按照上述步骤操作
你有不同的选择。
在您的 activity 中,您可以在布局中使用类似的东西:
<com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.appbar.MaterialToolbar
style="@style/MyToolbar"
...
/>
</com.google.android.material.appbar.AppBarLayout>
然后你可以自定义样式:
<!-- 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>
否则你也可以使用Toolbar
API:
toolbar.setTitle("Material Title");
toolbar.setTitleTextAppearance(this,R.style.My_TextAppearance_Toolbar);
另一种选择是在布局中使用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"
.../>