实现自定义窗口title

来源:互联网 发布:淘宝快手直播号能买吗? 编辑:程序博客网 时间:2024/06/05 18:10
第一步,实现自定义标题栏,需要在onCreate方法里这样写
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
注意:requestWindowFeature方法要在setContentView方法之前,getWindow().setFeatureInit最好在setContentView方法之后


第二步,就是写好自己的布局文件,实现标题栏的自定义:title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/title_bg" >
<TextView android:id="@+id/title" 
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:textSize="30dip"
android:textColor="#000"
android:text="@string/app_label"
android:layout_gravity="center_vertical"/>
</LinearLayout>


第三步,写一个style:velue/style.xml
<resources>
    <style name="CustomWindowTitleBackground">  
        <item name="android:background">#CCFFFF</item>  
    </style>
    <style name="activityTitlebar" parent="android:Theme">  
        <item name="android:windowTitleSize">45dip</item>  
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>  
    </style> 
</resources>


第四步,把style加入到AndroidManifest.xml
<activity android:name=".DemoActivity"  
                  android:label="@string/app_name" android:theme="@style/activityTitlebar">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
</activity>