自定义Android标题栏TitleBar布局(转载)
2012-06-27 17:09
471 查看
自定义Android标题栏TitleBar布局
很多网友发现自己Android程序的标题栏TitleBar区域很单调,如果想个性化一些可以通过下面的方法来为自己软件的标题定制一个layout布局文件,比如浏览器的标题栏,它包含了网站的Favicon,自定义的进度条,和不确定的进度指示等等,
实现的方法自己控制吧。下面代码在onCreate中使用,同时顺序不要改变,否则将无法生效:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); //软件activity的布局 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); //titlebar为自己标题栏的布局
Layout下建立titlebar.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:orientation="horizontal" > <Button android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/back" android:textSize="18sp" android:textColor="#FF0000FF" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/share" android:textSize="18sp" android:textColor="#FFFFFF00" /> </LinearLayout>
这样虽然可以在一定程度上定制标题栏, 不过, 这里无法改变标题栏的高度和背景(背景设置之后会在两端有两个
非常难看的边框). 据说, 原因是android 固有的.
这里有修改方法:
原理是这样的. 直接像上述代码那样添加title仅仅是把一个子界面添加到原有的title上的, 并没有改变原来的属性,
比如 标题栏大小, 标题栏背景. 这些需要在theme 主题里面定义.
因此先定义一个style, 若修改背景请修改android:windowTitleBackgroundStyle
若修改标题栏高度,请修改android:windowTitleSize
例子: <?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="CustomWindowTitleBackground"> <item name="android:background">#565656</item> </style> <style name="test" parent="android:Theme"> <item name="android:windowTitleSize">50dp</item> <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> </style> </resources>
在程序的android manifest.xml中对应:
activity中添加属性 android:theme = "@style/test" 就可以了
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.guardian" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" > <activity android:name=".Guardian" android:label="@string/app_name" android:theme = "@style/test" //就在这里 > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="4" /> </manifest>
之后借助于设置自定义的标题栏xml文件,就可以自定义标题栏布局了
![](http://pic002.cnblogs.com/images/2012/328668/2012062717075790.jpg)
原文来自:http://www.blogjava.net/Green-nut/articles/332617.html
相关文章推荐
- (Android新手上路)actionbar完全自定义标题布局,告别繁琐的actionbar设置,看别人的然后自己琢磨出来的
- Android 标题栏(TItle Bar)自定义 美化
- AndroidGUI25:定制Activity的标题栏(Titlebar)(转载)
- Android自定义View之TitleBar,通用标题栏
- android学习——自定义标题栏titlebar
- Android:去掉页面原来的标题 titlebar
- Android 自定义标题栏Title Bar
- Android 设置Activity全屏显示,没有TitleBar;自定义TitleBar
- Android自定义titlebar中设置progressbar
- Android自定义TitleView标题开发实例
- 转:自定义Android标题栏TitleBar
- android 简析自定义布局、布局的执行流程(转载http://blog.sina.com.cn/s/blog_74c22b210100vfun.html)
- TitleBar 自定义布局
- Android自定义标题栏 TItle Bar
- android 去掉屏幕上的title bar(转载)
- mono for android 自定义titleBar Actionbar 顶部导航栏 修改 样式 学习
- 【Android自定义View实战】之自定义项目通用的标题栏CustomTitleBar
- android 自定义titlebar标题栏冲突问题
- Android 自定义标题栏 TitleBar
- Android开发中自定义string、color、style、drawable,title,布局页面(include)的资源使用方法.txt