修改 Android 标题栏(Title Bar)

stack over flow

 

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

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> 

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

TextView title(TextView) = findViewById(android.R.id.title); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值