设置标题背景颜色

时间:2010-02-12 12:21:58

标签: android title titlebar

在我的Android应用程序中,我希望标准/基本标题栏能够改变颜色。

要更改setTitleColor(int color)的文字颜色,有没有办法更改条形图的背景颜色?

13 个答案:

答案 0 :(得分:186)

这个thread将帮助您开始在xml文件中构建自己的标题栏并在活动中使用它

修改

以下是上述链接内容的简短摘要 - 这是只是来设置文本的颜色和标题栏的背景 - 没有调整大小,没有按钮,只是最简单样品

res / layout / mytitle.xml - 这是代表​​标题栏的视图

<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/myTitle"
  android:text="This is my new title"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:textColor="@color/titletextcolor"
   />

res / values / themes.xml - 我们希望保留默认的Android主题,只需要更改标题背景的背景颜色。因此,我们创建一个继承默认主题的主题,并将背景样式设置为我们自己的样式。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="customTheme" parent="android:Theme"> 
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>   
    </style> 
</resources>

res / values / styles.xml - 这是我们设置主题以使用我们想要的标题背景颜色的地方

<?xml version="1.0" encoding="utf-8"?>
<resources> 
   <style name="WindowTitleBackground">     
        <item name="android:background">@color/titlebackgroundcolor</item>                   
    </style>
</resources>

res / values / colors.xml - 在此处设置您想要的颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>   
    <color name="titlebackgroundcolor">#3232CD</color>
    <color name="titletextcolor">#FFFF00</color>
</resources>

AndroidMANIFEST.xml 中,在应用程序(针对整个应用程序)或活动(仅针对此活动)标记中设置主题属性

<activity android:name=".CustomTitleBar" android:theme="@style/customTheme" ...

来自Activity(名为CustomTitleBar):

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

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);

}

答案 1 :(得分:18)

感谢您提供这个明确的解释,但是我想通过询问一个链接的问题(不要真的想做一个新帖子,因为这个是我的问题的基础)来为你的答案添加更多内容。

我在超类中声明我的标题栏,我的所有其他活动都是孩子,必须只更改一次栏的颜色。我还想添加一个图标并更改栏中的文字。我做了一些测试,并设法改变其中一个或另一个,但不是同时改变两个(使用setFeatureDrawable和setTitle)。 理想的解决方案当然是遵循链接中给出的线程中的解释,但是当我在超类中声明时,由于setContentView和R.id.myCustomBar中的布局,我有一个问题,因为如果我记得好,我只能调用一次setContentView ......

修改 找到我的答案:

对于那些像我一样喜欢使用超级课程的人,因为它非常适合在应用程序中随处可用菜单,它在这里的工作原理相同。

只需将其添加到您的超类:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.customtitlebar);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
    customTitleText = (TextView) findViewById(R.id.customtitlebar);

(您必须将textview声明为受保护的类变量)

然后,这就是你的应用程序的所有功能(例如,如果你所有的活动都是这个类的孩子),你只需要打电话

customTitleText.setText("Whatever you want in title");

并且您的标题栏将被编辑。

我的案例中关联的XML是(R.layout.customtitlebar):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/background">
<ImageView android:layout_width="25px" android:layout_height="25px"
    android:src="@drawable/icontitlebar"></ImageView>
<TextView android:id="@+id/customtitlebar"
    android:layout_width="wrap_content" android:layout_height="fill_parent"
    android:text="" android:textColor="@color/textcolor" android:textStyle="bold"
    android:background="@color/background" android:padding="3px" />
</LinearLayout>

答案 2 :(得分:11)

还有另一种方法可以更改背景颜色,但如果Window的View层次结构及其标题发生更改,则它可能会在未来的Android版本上失败。但是,在这种情况下,代码不会崩溃,只是错过了设置想要的颜色。

在您的活动中,例如onCreate,请执行:

View titleView = getWindow().findViewById(android.R.id.title);
if (titleView != null) {
  ViewParent parent = titleView.getParent();
  if (parent != null && (parent instanceof View)) {
    View parentView = (View)parent;
    parentView.setBackgroundColor(Color.rgb(0x88, 0x33, 0x33));
  }
}

答案 3 :(得分:6)

protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View titleView = getWindow().findViewById(android.R.id.title);
    if (titleView != null) {
      ViewParent parent = titleView.getParent();
      if (parent != null && (parent instanceof View)) {
        View parentView = (View)parent;
        parentView.setBackgroundColor(Color.RED);
      }
    }

在上面的代码中,您可以尝试使用标题而不是标题栏 这将影响应用程序中的所有活动

答案 4 :(得分:6)

此代码有助于在Android中以编程方式更改标题栏的背景。将颜色更改为您想要的任何颜色。

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1c2833")));

}

答案 5 :(得分:2)

我想不。 您可以创建无标题活动,并在活动布局中创建自己的标题栏。

检查this,第63行及以下:

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1)

设置customview而不是默认标题视图。

答案 6 :(得分:2)

查看SDK的platforms/android-2.1/data/res/layout/screen.xml。它似乎在那里定义了一个标题。您可以经常检查这样的布局并借用             style="?android:attr/windowTitleStyle" 然后,您可以在自己的TextView中使用和覆盖这些样式。

您甚至可以选择标题进行直接调整:

TextView title = (TextView)findViewById(android.R.id.title);

答案 7 :(得分:2)

尝试使用以下代码

View titleView = getWindow().findViewById(android.R.id.title);
    if (titleView != null) {
      ViewParent parent = titleView.getParent();
      if (parent != null && (parent instanceof View)) {
        View parentView = (View)parent;
        parentView.setBackgroundColor(Color.RED);
      }
    }

也使用此链接非常有用:http://nathanael.hevenet.com/android-dev-changing-the-title-bar-background/

答案 8 :(得分:1)

使用Google提供的v7 appcompat支持库,可以更轻松地更改标题栏的颜色。

请参阅此链接,了解如何设置此支持库:https://developer.android.com/tools/support-library/setup.html

完成后,将以下行添加到res / values / styles.xml文件就足够了:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/ActionBar</item>
</style>

<!-- Actionbar Theme -->
<style name="ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/titlebackgroundcolor</item>
</style>

(假设你的res / values / colors.xml中定义了“titlebackgroundcolor”,例如:

<color name="titlebackgroundcolor">#0000AA</color>

答案 9 :(得分:1)

自Android 5.0(API级别21)以来,事情似乎变得更好/更容易。

我认为你所寻找的是这样的:

<style name="AppTheme" parent="AppBaseTheme">
    <!-- Top-top notification/status bar color: -->
    <!--<item name="colorPrimaryDark">#000000</item>-->
    <!-- App bar color: -->
    <item name="colorPrimary">#0000FF</item>
</style>

请参阅此处以供参考:

https://developer.android.com/training/material/theme.html#ColorPalette

答案 10 :(得分:1)

我通过在“ res”->“ values”->“ colors.xml”中更改样式颜色值来完成此操作 enter image description here

这将更改整个项目的颜色,这对我来说很好。

enter image description here

答案 11 :(得分:0)

您可以使用它。

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

答案 12 :(得分:0)

将此代码粘贴到setContentView之后或放入onCreate

如果您有颜色代码,请使用此;

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#408ed4")));

如果要从颜色库中获取特定代码,请使用此;

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.WHITE));