Toolbar的标题自定义颜色

来源:互联网 发布:出售qq淘宝网商城 编辑:程序博客网 时间:2024/05/22 16:05
<span style="background-color: rgb(255, 255, 255);">dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    compile 'com.android.support:appcompat-v7:22.1.1'    compile 'com.android.support:gridlayout-v7:22.1.1'    compile 'com.android.support:support-v4:22.1.1'    compile 'com.android.support:design:22.2.0'}</span>
  minSdkVersion 8        targetSdkVersion 21        versionCode 1        versionName "1.0"

1、自定义style

<resources>    <!-- Base application theme. -->    <style name="BaseTheme" parent="Theme.AppCompat.Light.DarkActionBar" />    <!-- Customize your theme here. -->    <style name="AppTheme" parent="Base.Theme.Design">    </style>    <style name="Base.Theme.Design" parent="Theme.AppCompat.Light.NoActionBar">        <!-- colorPrimary 定义 ActionBar 的背景色 -->        <item name="colorPrimary">#116B84</item>        <!-- colorPrimaryDark 定义状态栏的背景色 -->        <item name="colorPrimaryDark">@color/blue_title_bottom</item>        <!-- textColor 定义标题文字的颜色 -->       <item name="android:textColorPrimary">@color/window_background</item>        <!-- colorAccent 为 colorControlActivated 的默认值,该值用户显示控件的高亮状态 -->        <item name="colorAccent">#FF4081</item>        <item name="android:windowBackground">@color/window_background</item>    </style></resources>


2、在xml中使用

android.support.v7.widget.Toolbar
代码如下:

<android.support.v7.widget.Toolbar            android:id="@+id/tool_bar"            android:layout_width="match_parent"            android:layout_height="?attr/actionBarSize"            android:background="?attr/colorPrimaryDark"            android:textColor="?attr/android:textColorPrimary"            app:layout_scrollFlags="scroll|enterAlways" />
3、代码中使用tootbar

  toolbar = (Toolbar) findViewById(R.id.tool_bar);        if (toolbar != null) {            setSupportActionBar(toolbar);        }

4、切换不同的tiltlebar名称

  toolbar.setTitle(menuItem.getTitle().toString());

好了,运行就可以看到titlebar 的名称颜色发生变化

第一种方式是直接使用代码如下:

toolbar.setTitleTextColor(getResources().getColor(R.color.window_background));

也可以达到相同的效果!

//---------------------------------------------------------------------------------------------------------------------------//

接着看


我看这个主题有引用textColorPrimary的,但是当我改成

却没什么用,titlebar并没有变成我想要的效果,标题是黑色,没有变成我要的白色,,,,,,,,,,不知道为何?知道的,请告诉我哦!谢谢啊。

相反,当我这么写:


却发现,android:textColorPrimary和colorPrimaryDark的颜色是来自我定义在style.xml的样式,如下图所示:


给人的感觉就像是自定义的取代了默认的颜色哦!不知道自己理解得是否对!如果你知道请告诉我!谢谢哈。


http://blog.csdn.net/abren32/article/details/46723435

请问Android4.0的ActionBar左上角的返回键怎么监听

请问Android4.0的ActionBar左上角的返回键怎么监听,就代码actionBar = getActionBar();actionBar.setDisplayHomeAsUpEnabled(true);然后事件在:
   @Override    public boolean onOptionsItemSelected(MenuItem item)    {        // TODO Auto-generated method stub        if(item.getItemId() == android.R.id.home)        {            finish();            return true;        }        return super.onOptionsItemSelected(item);    }


0 0
原创粉丝点击