设置标题背景颜色

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

要改变文本颜色你setTitleColor(int color) ,有没有办法改变酒吧的背景颜色?

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

编辑

这里是上面的链接的内容的简要总结 – 这只是设置文本的颜色和标题栏的背景 – 没有resize,没有button,只是最简单的样本

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主题,只需要改变标题背景的背景颜色。 所以我们创build一个inheritance默认主题的主题,并将背景风格设置为我们自己的风格。

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

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

我声明我的标题栏在一个超类,从我的其他所有活动是儿童,只能改变一次栏的颜色。 我想也添加一个图标,并更改栏中的文字。 我已经做了一些testing,并设法改变其中一个或另一个,但不是在同一时间(使用setFeatureDrawable和setTitle)。 理想的解决scheme当然要遵循链接中给出的线程中的解释,但是当我在超类中声明时,由于setContentView和R.id.myCustomBar中的布局,我有一个问题,因为如果我记得我可以只调用一次setContentView …

编辑find我的答案:

对于像我这样喜欢和超类一起工作的人来说,因为在应用程序的任何地方都可以使用这个菜单非常棒,所以它在这里也是一样的。

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

 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声明为受保护的类variables)

然后,你的应用程序中的任何地方(例如,如果你的所有活动都是这个类的孩子),那么你只需要调用

 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> 

还有另一种方法来改变背景颜色,但是如果窗口的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)); } } 
 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); } } 

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

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

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

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

检查这一点 ,第63行及以下:

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

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

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

您甚至可以通过执行直接调整来select标题:

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

尝试使用下面的代码

 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/

通过使用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>