Android 设置标题背景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2251714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:10:19  来源:igfitidea点击:

Set title background color

androidtitletitlebar

提问by Thizzer

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

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

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

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

回答by ccheneson

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

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

Edit

编辑

Here is a brief summary of the content of the link above - This is justto 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

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

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

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- Set here the color you want

res/values/colors.xml- 在这里设置你想要的颜色

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

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

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

From the Activity (called CustomTitleBar) :

从活动(称为 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);

}

回答by Sephy

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

我在超类中声明我的标题栏,我的所有其他活动都是儿童,只需更改一次栏的颜色。我还想添加一个图标并更改栏中的文本。我进行了一些测试,并设法更改了其中一个,但不能同时更改两者(使用 setFeatureDrawable 和 setTitle)。理想的解决方案当然是遵循链接中给出的线程中的解释,但是正如我在超类中声明的那样,由于 setContentView 和 R.id.myCustomBar 中的布局,我遇到了问题,因为如果我记住我只能调用 setContentView 一次...

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

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

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

回答by mikeplate

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.

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

In your Activity, like onCreate, do:

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

回答by Edwinfad

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

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

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

}

回答by Pratishtha Goriya

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 titleinstead of titlebarthis will affect on all activity in your application

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

回答by skyman

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

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

Check this, Line 63 and below:

检查这个,第 63 行及以下:

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

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

sets customview instead of default title view.

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

回答by Steve Pomeroy

Take a peek in platforms/android-2.1/data/res/layout/screen.xmlof 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.

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

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

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

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

回答by Pratik Butani

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/

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

回答by stefan.m

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

通过使用 Google 提供的 v7 appcompat 支持库,有一种更简单的替代方法可以更改标题栏的颜色。

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

请参阅有关如何设置此支持库的链接: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:

完成后,将以下几行添加到 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>

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

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

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

)

)

回答by Loisaida Sam Sandberg

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

自 Android 5.0(API 级别 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

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