如何将Android中的标题栏自定义

来源:互联网 发布:东北大学网络教育 编辑:程序博客网 时间:2024/06/05 11:48

在Android中,标题栏是默认的,但是有些时候我们想要将它变成我们想要的样子。

如何来改变标题栏的高度?

根据网上一些所说的,4.0系统很容易会出现一个自定义标题栏和其他标题栏冲突的错误,那么在4.0以上的版本中如何修改呢?

首先,我们可以在/res/values-v11和/res/values-14中的styles.xml中,修改parent中的值为android:Theme;然后将我们所定义的style放在这两个文件里面。

<style name="CustomWindowTitleBackground">       <item name="android:background">#ffffff</item>//改变标题栏的颜色</style>    <style name="titleBarSelf" parent="AppBaseTheme">        <item name="android:windowTitleSize">50dp</item>//改变标题栏的高度         <item name="android:windowNoTitle">false</item>//是否隐藏标题栏     <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>  </style>

这是自定义的style,然后我们在配置文件中,将android:theme中的值改成上述自定义中的值

 <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/titleBarSelf"//就是这里,titleBarSelf是与上述sytle中name值是一样的。         >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>
这样,我们达到了修改标题栏的效果,想要更加深入的理解这些,我还得更加深入的学习。

第二,修改标题栏的内容

我们首先在Activity中的onCreate中加入这几句代码,注意顺序。

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);// 这句话意思是<span style="font-family: Arial; font-size: 14px; line-height: 26px;">启用窗体的扩展特性。参数是Window类中定义的常量。</span>setContentView(R.layout.main);//Activity的布局文件getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title); //自定义的标题布局栏
然后在layout中创建一个自己想要的标题栏的名字为title样式。

到现在我们成功的修改了标题栏和自定义了自己想要的标题栏的内容。

网上所参考的一些信息:

http://blog.csdn.net/heng615975867/article/details/8735550  这是requestWindowFeature()的应用。

http://www.oschina.net/question/931057_122529?fromerr=hsVYoCgd  这是

关于报错You cannot combine custom titles with other title features

的解决方法。

http://www.blogjava.net/Green-nut/articles/332617.html  自定义标题栏



0 0
原创粉丝点击