实现自定义窗口title

第一步,实现自定义标题栏,需要在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> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值