Android自定义标题栏

Android开发中,系统定义的标题栏往往满足不了我们的需求,这时候需要我们DIY自己的标题栏,不说多了直接上代码:

1.创建自定义标题栏布局文件:titleBar.xml
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是title" />


  </LinearLayout>





2.Activity中onCreate()按照下列顺序添加代码
  requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);//添加代码1
setContentView(R.layout.layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.titlebar);// 设置自定义标题栏  添加代码2





3.res/value/styles.xml 创建如下样式

4.AndroidManifest.xml Activity中添加样式,覆盖默认样式
   android:theme="@style/test" 


  <!-- 自定义titlebar -->
     <!-- titleBar背景颜色 -->
    <style name="CustomWindowTitleBackground">
        <item name="android:background">@drawable/back</item>
    </style>


    <!-- titleBar style -->
    <style name="test" parent="android:Theme">
        <item name="android:windowTitleSize">50dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>

这样四步就可以了




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值