设置标题背景颜色

发布于 2024-08-21 20:53:53 字数 112 浏览 7 评论 0原文

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

要更改文本颜色,您有setTitleColor(int color),有没有办法更改栏的背景颜色?

In my android application I want the standard/basic title bar to change color.

To change the text color you have setTitleColor(int color), is there a way to change the background color of the bar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

何以畏孤独 2024-08-28 20:53:53

这个线程将帮助您开始在 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);

}

This thread will get you started with building your own title bar in a xml file and using it in your activities

Edit

Here is a brief summary of the content of the link above - This is just to set the color of the text and the background of the title bar - no resizing, no buttons, just the simpliest sample

res/layout/mytitle.xml - This is the view that will represent the title bar

<?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 - We want to keep the default android theme and just need to change the background color of the title background. So we create a theme that inherits the default theme and set the background style to our own style.

<?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 - This is where we set the theme to use the color we want for the title background

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

res/values/colors.xml - Set here the color you want

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

In the AndroidMANIFEST.xml, set the theme attribute either in the application (for the whole application) or in the activity (only this activity) tags

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

From the Activity (called 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);

}
有深☉意 2024-08-28 20:53:53

感谢您的清晰解释,但是我想通过提出一个链接的问题来为您的答案添加更多内容(我真的不想写一篇新文章,因为这是我问题的基础)。

我在超类中声明我的标题栏,我的所有其他活动都是子级,只需更改一次标题栏的颜色。我还想添加一个图标并更改栏中的文本。我已经做了一些测试,并设法更改其中之一,但不能同时更改两者(使用 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>

Thanks for this clear explanation, however I would like to add a bit more to your answer by asking a linked question (don't really want to do a new post as this one is the basement on my question).

I'm declaring my titlebar in a Superclass from which, all my other activities are children, to have to change the color of the bar only once. I would like to also add an icon and change the text in the bar. I have done some testing, and managed to change either one or the other but not both at the same time (using setFeatureDrawable and setTitle).
The ideal solution would be of course to follow the explanation in the thread given in the link, but as i'm declaring in a superclass, i have an issue due to the layout in setContentView and the R.id.myCustomBar, because if i remember well i can call setContentView only once...

EDIT
Found my answer :

For those who, like me, like to work with superclasses because it's great for getting a menu available everywhere in an app, it works the same here.

Just add this to your superclass:

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

(you have to declare the textview as protected class variable)

And then the power of this is that, everywhere in you app (if for instance all your activities are children of this class), you just have to call

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

and your titlebar will be edited.

The XML associated in my case is (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>
九公里浅绿 2024-08-28 20:53:53

还有另一种方法可以更改背景颜色,但这是一种黑客行为,如果更改窗口的视图层次结构及其标题,则在未来版本的 Android 上可能会失败。但是,在这种情况下,代码不会崩溃,只是错过设置所需的颜色。

在您的 Activity 中,例如 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));
  }
}

There is another way to change the background color, however it is a hack and might fail on future versions of Android if the View hierarchy of the Window and its title is changed. However, the code won't crash, just miss setting the wanted color, in such a case.

In your Activity, like onCreate, do:

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));
  }
}
何以畏孤独 2024-08-28 20:53:53

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

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

}

This code helps to change the background of the title bar programmatically in Android. Change the color to any color you want.

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

}
予囚 2024-08-28 20:53:53
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);
      }
    }

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

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);
      }
    }

on above code you can try you can use title instead of titlebar
this will affect on all activity in your application

你好,陌生人 2024-08-28 20:53:53

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

检查 this,第 63 行及以下:

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

设置自定义视图而不是默认标题视图。

I suppose no.
You can create titleless activity and create your own title bar in activity layout.

Check this, Line 63 and below:

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

sets customview instead of default title view.

轻拂→两袖风尘 2024-08-28 20:53:53

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

您甚至可以通过执行以下操作来选择标题进行直接调整:

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

Take a peek in platforms/android-2.1/data/res/layout/screen.xml of the SDK. It seems to define a title there. You can frequently examine layouts like this and borrow the
style="?android:attr/windowTitleStyle"
styles which you can then use and override in your own TextViews.

You may be able to even select the title for direct tweaking by doing:

TextView title = (TextView)findViewById(android.R.id.title);
天赋异禀 2024-08-28 20:53:53

尝试使用以下代码

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/

Try with the following code

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);
      }
    }

also use this link its very useful : http://nathanael.hevenet.com/android-dev-changing-the-title-bar-background/

战皆罪 2024-08-28 20:53:53

有一个更简单的替代方法可以更改标题栏的颜色,即使用 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>

假设“titlebackgroundcolor”是在您的res/values/colors.xml,例如

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

:)

There is an easier alternative to change the color of the title bar, by using the v7 appcompat support library provided by Google.

See this link on how to to setup this support library: https://developer.android.com/tools/support-library/setup.html

Once you have done that, it's sufficient to add the following lines to your res/values/styles.xml file:

<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>

(assuming that "titlebackgroundcolor" is defined in your res/values/colors.xml, e.g.:

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

)

人│生佛魔见 2024-08-28 20:53:53

我通过更改“res”中的样式颜色值来完成此操作 -> “价值观”-> “颜色.xml”
输入图片这里的描述

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

输入图片此处描述

I have done this by changing style color values in "res" -> "values" -> "colors.xml"
enter image description here

This will change colors for entire project which is fine with me.

enter image description here

停顿的约定 2024-08-28 20:53:53

自 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

Things seem to have gotten better/easier since Android 5.0 (API level 21).

I think what you're looking for is something like this:

<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>

See here for reference:

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

眉黛浅 2024-08-28 20:53:53

请将此代码粘贴到 setContentView 之后或粘贴到 onCreate 中

如果您有颜色代码,

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

;如果您想要颜色库中的特定代码,请使用此;

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

Paste this code after setContentView or into onCreate

if you have a color code use this ;

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

if you want a specific code from Color library use this ;

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
别把无礼当个性 2024-08-28 20:53:53

你可以使用它。

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

you can use it.

  toolbar.setTitleTextColor(getResources().getColor(R.color.white));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文