一个接着一个的activity,写啊写,调啊调,后来,终于发觉,activity的标题栏好难看,好单调啊。咱们为了吸引用户的眼球,得搞点个性化的东西。
自定义标题栏的方法,网上一搜一大堆,我也稍微提一下,oncreate中加上如下代码就行:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(view);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
这个名为title的layout是这样子的,很简单,就是一个textview,然后有个背景色:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#66cccccc"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello"
/>
</LinearLayout>
好,运行看效果。看到了吧,发现问题了没,标题栏的背景色没有填充满是吧,这可真是杯具哟。padding、margin什么的都用上也不管用,怎么办呢。
看源码!
window初始化,加载标题的地方,咱也不知道在哪里,不过咱能以layout作为切入点。打开源码里面的layout文件夹,找跟标题栏相关的xml文件。里面有screen_title.xml和screen_custom_title.xml,这就是咱们要找的目标了。
既然是自定义标题,那我们就看screen_custom_title.xml,里面有一个title_container和一个content,组合成了标题栏,我们自定义标题所给出的view,都被content作为子view了,影响不了那个title_container和content,所以,任你怎么弄,它该留白的还是留白,你没招。
看title_container有个style是这样的:style="?android:attr/windowTitleBackgroundStyle"
content的foreground是这样的android:foreground="?android:attr/windowContentOverlay"
好,从这里我们就可以入手改了。
去values下面的themes.xml找到windowTitleBackgroundStyle这一项,这个应该在注释<!-- Window attributes -->的下面。
<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
然后去styles.xml下找到WindowTitleBackground项,
<style name="WindowTitleBackground">
<item name="android:background">@android:drawable/title_bar</item>
</style>
发现是一个drawable,xml的,里面定义了背景图片。ok,我们知道了,这个是定义titlebar的背景色。
然后,去values下面的themes.xml找到windowContentOverlay,也是属于window attributes。
<item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>
发现也是个drawable,ok,我们也知道了,这个是定义contentoverlay的背景的。
其实,通过研究我发现,不能填充满的原因是title_container的背景的原因,我们覆盖一下就行了。
首先,写个themes文件
<resources>
<style name="XTheme" parent="android:Theme">
<!-- Window attributes -->
<item name="android:windowTitleStyle">@style/XWindowTitle</item>
<item name="android:windowTitleBackgroundStyle">@style/StatusBarBackground</item>
<item name="android:windowContentOverlay">@null</item>
</style>
</resources>
然后写styles文件
<resources>
<style name="StatusBarBackground">
<item name="android:background">@drawable/shape</item>
</style>
<style name="XWindowTitle" parent="android:WindowTitle">
<item name="android:shadowColor">#BB000000</item>
<item name="android:shadowRadius">0</item>
</style>
</resources>
注意这个XWindowTitle要继承WindowTitle。
最后,在manifext中给自定义的activity申明主题。
<activity android:name=".Entry"
android:label="@string/app_name"
android:theme="@style/XTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
论坛原帖及源码
http://www.iteye.com/topic/760314
分享到:
相关推荐
本例是讲解怎样修改android标题栏的大小,颜色,等等
android标题栏随着RecyclerView滚动背景渐变
随着屏幕的滚动,状态栏和标题栏的颜色渐变
此demo是关于标题的背景色默认的是全透明的,也就是以背景图片来作为背景色。然后随着ListView的滑动标题的背景色会有全透明慢慢的变为和背景图一样的颜色。具体详情请前往博客查看...
里面有两个项目资源例子,是实现android 标题固定Listview的两种方式,请大家查阅,希望对大家有帮助。
android 标题栏滑动颜色随之变化
Android标题栏TitleBar全攻略 幻影使用
CustomTitleBar, 【通用标题栏】通用Android标题栏控件 A Common Titlebar For Android
Android标题栏渐变效果,上下拉动界面标题栏颜色渐变效果
android 标题栏自定义标签,可以实现 左中右,文字、图片显示。一句话调用标题栏,沉浸式显示
Android 标题栏置顶且下拉刷新,下滑自动加载(完美结合),绝对非常完美结合,已经应用到市场了,测试无误
Android 标题、状态栏的隐藏 Android 开发 Google 教程 SDK
Android标题栏高级.docx
Android自定义标题栏(并加入返回按钮)的demo
Android动画隐藏标题栏全屏
之前又看到网上有朋友做过相关案例,不过是通过滚动listview来监听实现标题栏渐变,但不是我想要的效果,我要的效果是通过滚动scrollview来实现标题栏颜色渐变效果,这才是我要的效果,1个积分意思一下O(∩_∩)O哈哈...
主要介绍了Android标题栏最右边添加按钮的实例的相关资料,希望通过本文大家能掌握如何操作,需要的朋友可以参考下
Android 手势检测上下滑动,上滑隐藏标题栏下滑显示标题栏,带动画效果、无卡顿、无空白。 适用各种情况,代码简单明了。
实现了Android系统标题栏在上下滑动时候监听并且渐变色
看IOS上的应用,应用中状态栏的颜色总能与应用标题栏颜色保持一致,用户体验很不错,对于这种效果怎么实现的呢?下面小编给大家分享android自定义状态栏颜色与应用标题栏颜色一致的实现方法,一起看看吧