[英]How to change my action bar text color and font size in android
i am new to android how to change my app name text color and font size ,my default text color was black i want change it to White color and increase my font size please any one help me 我是Android的新手如何更改我的应用程序名称文本颜色和字体大小,我的默认文本颜色是黑色我想将其更改为白色并增加我的字体大小请任何人帮助我
style.xml style.xml
my default text color was black i want change it to White color 我的默认文字颜色是黑色,我想将其改为白色
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">@style/MyActionBarTheme</item>
<item name="android:actionBarTabTextStyle">@style/ActionBarTabText</item>
<item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
<item name="android:actionBarTabStyle">@style/tabStyle</item>
<item name="android:actionBarDivider">@null</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#029C7A</item>
</style>
<style name="ActionBarTabText" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:textColor">#CFCFC4</item>
<item name="android:gravity">center</item>
</style>
<style name="MyActionBarTitleText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#FFFFFF</item>
</style>
<style name="tabStyle" parent="@android:style/Widget.Holo.Light.ActionBar.TabView">
<item name="android:tabStripEnabled">false</item>
</style>
</resources>
Using AppCompat Theme, it can be done in this way. 使用AppCompat Theme,可以通过这种方式完成。 Define a custom style in styles.xml as follows:
在styles.xml中定义自定义样式,如下所示:
<style name="CustomToolbarTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="actionMenuTextColor">add_colour_your_choice</item>
<item name="android:textColorSecondary">add_colour_your_choice</item>
<item name="android:textSize">30sp</item>
</style>
Then in the layout of the activity where you wish to change the action bar colour add a toolbar: 然后在活动的布局中,您希望更改操作栏颜色添加工具栏:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/white"
android:elevation="0.5dp"
android:theme="@style/CustomToolbarTheme"/>
And lastly in the java file of the activity, add the following lines to onCreate after setContentLayout
: 最后在活动的java文件中,在
setContentLayout
之后setContentLayout
添加到onCreate:
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Note: For this to work, you will have to add: 注意:要使其正常工作,您必须添加:
compile 'com.android.support:appcompat-v7:22.2.1'
to your build.gradle(module:app) under dependencies. 到依赖项下的build.gradle(module:app)。 (There are two dependencies, one under buildscipt and one at the end. ADD the line to the one AT THE END
(有两个依赖项,一个在buildscipt下,一个在最后。将行添加到一个AT THE END
I have created my own custom view and added it to action bar : 我创建了自己的自定义视图并将其添加到操作栏:
Please take a look on below code : 请看下面的代码:
How to set custom view to ActionBar ? 如何设置ActionBar的自定义视图?
public static ActionBar setCustomActionBar(Activity activity, ActionBar actionBar, String title) {
if (actionBar != null) {
ViewGroup actionBarLayout = (ViewGroup) activity.getLayoutInflater().inflate(R.layout.custom_actionbar, null);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
actionBar.setBackgroundDrawable(new ColorDrawable(activity.getResources().getColor(R.color.actionbar_grey)));
TextView actionBarTitle = (TextView) actionBarLayout.findViewById(R.id.text_actionbar_title);
Typeface airplaneTypeface = Typeface.createFromAsset(activity.getAssets(), "fonts/Exo-Regular.otf");
actionBarTitle.setText(title);
actionBarTitle.setTypeface(airplaneTypeface);
// SpannableString s = new SpannableString(title);
// s.setSpan(new TypefaceSpan(activity, "fonts/airplane.ttf"), 0, s.length(),
// Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// actionBar.setTitle(s);
}
return actionBar;
}
custom_actionbar.xml file : custom_actionbar.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/actionbar_grey"
android:padding="@dimen/actionbar_padding">
<ImageView
android:id="@+id/img_up_indicator"
android:layout_width="@dimen/actionbar_icon_width"
android:layout_height="@dimen/actionbar_icon_height"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text_actionbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/img_up_indicator"
android:layout_toRightOf="@+id/img_up_indicator"
android:paddingLeft="@dimen/actionbar_title_left_padding"
android:paddingStart="@dimen/actionbar_title_left_padding"
android:text="@string/app_name"
android:textColor="@color/logo_blue"
android:textSize="@dimen/actionbar_title_text_size" />
</RelativeLayout>
How to use method : 如何使用方法:
setCustomActionBar(this, getActionBar(), getResources().getString(R.string.title_actionbar_home));
This is a sample that shows how to create custom view and set it to actionbar. 这是一个示例,显示如何创建自定义视图并将其设置为操作栏。
You can modify it as per your requirement. 您可以根据自己的要求进行修改。
Note : Here I used my resources that I did not provided you can modify as you wish. 注意 :在这里,我使用了我未提供的资源,您可以根据需要进行修改。
Thanks.!! 谢谢。!!
try to add this 尝试添加这个
actionBar.setTitle(Html.fromHtml("<font color='#ff0000'>ActionBarTitle </font>"));
for font size 用于字体大小
<style name="TextAppearance.Medium">
<item name="android:textSize">18sp</item>
</style>
<style name="TextAppearance.Small">
<item name="android:textSize">14sp</item>
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.