现在的位置: 首页 > 综合 > 正文

android自定义titlebar

2018年05月16日 ⁄ 综合 ⁄ 共 1690字 ⁄ 字号 评论关闭

首先,修改标题栏的宽度和背景,在style.xml中添加:

[html] view
plain
copy

  1. <style  
  2.    <item name="android:background">@drawable/title_bg</item>   
  3. </style>   
  4.   
  5. <style name="test" parent="android:Theme">   
  6.     <item name="android:windowTitleSize">40dp</item>   
  7.     <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>   
  8. </style>  


      然后修改AndroidMainfest.xml文件:

[html] view
plain
copy

  1. <activity android:name=".MainActivity"   
  2.                 android:theme="@style/test">   
  3.           <intent-filter>   
  4.               <action android:name="android.intent.action.MAIN" />   
  5.               <category android:name="android.intent.category.LAUNCHER" />   
  6.           </intent-filter>   
  7.       </activity>  

      接着修改MainActivity:

[html] view
plain
copy

  1. public void onCreate(Bundle savedInstanceState) {   
  2.        super.onCreate(savedInstanceState);   
  3.        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题   
  4.        setContentView(R.layout.main);   
  5.        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);//自定义布局赋值   
  6.    }  

最后在layout文件夹中添加title.xml文件,内容如下:

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3. android:orientation="horizontal" android:layout_width="fill_parent"   
  4. android:layout_height="fill_parent">   
  5.   
  6. <ImageView android:layout_width="wrap_content"   
  7. android:layout_centerVertical=’true’   
  8. android:layout_height="wrap_content"   
  9. android:src="@drawable/ic_launcher_email" />   
  10. <TextView android:layout_width="wrap_content"   
  11. android:layout_centerInParent="true"   
  12. android:layout_height="wrap_content"   
  13. android:text="自定义标题栏" />   
  14.   
  15. </RelativeLayout>  

OK,一个简单的自定义标题栏完成了。

抱歉!评论已关闭.