修改系统标题栏 高度 字体偏移 添加图片 点击事件

来源:互联网 发布:健身数据统计 编辑:程序博客网 时间:2024/06/05 14:55


修改系统标题栏 高度 字体偏移 添加图片 点击事件


具体的操作步骤是:

1、在res/values添加styles.xml文件。

     .在eclipse中,选择File -> New -> Other菜单

     .在弹出窗口中,选择Android/Android XML File,点击 Next

     .在添加XML窗口中,输入文件名"styles.xml",选中Values,

       输入文件夹路径"/res/values"

     .然后点击 Finish

 

2、在res/values添加themes.xml文件。

    操作方法同步骤1,只是文件名输入"themes.xml"

 

3、styles.xml文件的代码

   

[xhtml] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources xmlns:adnroid="http://schemas.android.com/apk/res/android">  
  3.   
  4. <style name="CustomWindowTitleBackground" parent="*android:WindowTitleBackground">  
  5.   <item name="android:background">@drawable/bg</item>  
  6. </style>  
  7.   
  8. <style name="CustomWindowTitle" parent="*android:WindowTitle">  
  9.   <item name="android:textAppearance">@style/CustomWindowTitleText</item>  
  10. </style>  
  11.   
  12. <style name="CustomWindowTitleText" parent="android:TextAppearance.WindowTitle">  
  13.   <item name="android:textColor">#00f</item>  
  14.   <item name="android:textSize">14sp</item>  
  15.   <item name="android:textStyle">bold</item>  
  16. </style>  
  17.   
  18. </resources>  

 

4、themes.xml文件的代码

[xhtml] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources xmlns:adnroid="http://schemas.android.com/apk/res/android">  
  3.   
  4. <style name="titleTheme" parent="android:Theme" >  
  5.   <!-- <item name="android:windowTitleSize">30dp</item>  -->  
  6.   <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>  
  7.   <item name="android:windowTitleStyle">@style/CustomWindowTitle</item>  
  8.   <item name="Android:windowTitleSize">60dp</item><!-- 标题栏高度 -->
  9. </style>  
  10.   
  11. </resources>  

 

5、修改工程的Manifest.xml文件

     .在Activity中加入android:theme="@style/titleTheme"即可

[xhtml] 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.android.customtitle"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".CustomTitle"  
  8.                   android:label="@string/app_name"  
  9.                   android:theme="@style/titleTheme">  
  10.             <intent-filter>  
  11.                 <action android:name="android.intent.action.MAIN" />  
  12.                 <category android:name="android.intent.category.LAUNCHER" />  
  13.             </intent-filter>  
  14.         </activity>  
  15.   
  16.     </application>  
  17.     <uses-sdk android:minSdkVersion="9" />  
  18.   
  19. </manifest>   

 

6、效果图

android自定义标题栏


7、改变文字的位置

经过上面的设置可以实现蓝色背景,高度40dp的标题栏,但是此时标题还没有居中。我曾经尝试在style中通过设置android:gravity=center等方式,但都没有成功。

后来在activity的代码中添加以下代码才实现了居中效果:

[java] view plain copy
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  

TextView textview1= (TextView) findViewById(android.R.id.title);view.setGravity(Gravity.CENTER);

//此处添加点击事件

//添加图片

  1. Drawable nav_up=getResources().getDrawable(R.drawable.button_nav_up);  
  2. nav_up.setBounds(00, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());  
  3. textview1.setCompoundDrawables(nullnull, nav_up, null); 

[java] view plain copy
  1.         setContentView(R.layout.activity_main);  
  2. ...  
  3. }  


1 0
原创粉丝点击