Android自定义标题栏

本文介绍如何在Android应用中自定义标题栏,包括隐藏默认标题、创建自定义标题栏布局及将其添加到主布局的方法。

创建一个Android工程,标题栏默认显示的是工程名,下面介绍如何自定义标题栏,我创建工程的MainActivity继承AppCompatActivity,效果图如下


首先在AndroidManifest.xml文件中把默认的标题隐藏,设置

android:theme="@style/Theme.AppCompat.Light.NoActionBar" >

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.nii.orcale" >  
  4.   
  5.     <application  
  6.         android:allowBackup="true"  
  7.         android:icon="@mipmap/ic_launcher"  
  8.         android:label="@string/app_name"  
  9.         android:supportsRtl="true"  
  10.         android:theme="@style/Theme.AppCompat.Light.NoActionBar" >  
  11.         <activity android:name=".MainActivity" >  
  12.             <intent-filter>  
  13.                 <action android:name="android.intent.action.MAIN" />  
  14.   
  15.                 <category android:name="android.intent.category.LAUNCHER" />  
  16.             </intent-filter>  
  17.         </activity>  
  18.     </application>  
  19.   
  20. </manifest>  

再创建一个标题的布局文件,就叫做title_toolbar.xml,内容如下

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.                 android:id="@+id/layout_titlebar"  
  4.                 android:layout_width="match_parent"  
  5.                 android:layout_height="52dp"  
  6.   
  7.                 android:background="#ed4255">  
  8.   
  9.     <TextView  
  10.             android:id="@+id/text_title"  
  11.             android:layout_width="match_parent"  
  12.             android:layout_height="match_parent"  
  13.             android:ellipsize="marquee"  
  14.             android:gravity="center_horizontal|center"  
  15.             android:singleLine="true"  
  16.             android:text="@string/title"  
  17.             android:textColor="#ffffffff"  
  18.             android:textSize="20dp"/>  
  19.   
  20. </RelativeLayout>  

最后把这个布局加入到主的布局文件中

<include layout="@layout/title_toolbar"></include>

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical"  
  8.     tools:context="com.nii.orcale.MainActivity">  
  9.   
  10.     <include layout="@layout/title_toolbar"></include>  
  11.   
  12.     <ListView  
  13.             android:id="@+id/listView"  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="wrap_content" >  
  16.     </ListView>  
  17. </LinearLayout>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dmfrm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值