Android Activity定制需要的Title

Activity界面默认的Title是只有文字描述的,当我们想要做成类似微信中Tilte行带有导航和多功能效果的时候,我们就需要自己去定义需要的布局来加载它。
Activity的全屏显示和去掉Title,说白了就是改变Window窗口features
01     /** Flag for the "options panel" feature.  This is enabled by default. */
02     public static final int FEATURE_OPTIONS_PANEL = 0;
03     /** Flag for the "no title" feature, turning off the title at the top
04      *  of the screen. */
05     public static final int FEATURE_NO_TITLE = 1;
06     /** Flag for the progress indicator feature */
07     public static final int FEATURE_PROGRESS = 2;
08     /** Flag for having an icon on the left side of the title bar */
09     public static final int FEATURE_LEFT_ICON = 3;
10     /** Flag for having an icon on the right side of the title bar */
11     public static final int FEATURE_RIGHT_ICON = 4;
12     /** Flag for indeterminate progress */
13     public static final int FEATURE_INDETERMINATE_PROGRESS = 5;
14     /** Flag for the context menu.  This is enabled by default. */
15     public static final int FEATURE_CONTEXT_MENU = 6;
16     /** Flag for custom title. You cannot combine this feature with other title features. */
17     public static final int FEATURE_CUSTOM_TITLE = 7;
18     /**
19      * Flag for enabling the Action Bar.
20      * This is enabled by default for some devices. The Action Bar
21      * replaces the title bar and provides an alternate location
22      * for an on-screen menu button on some devices.
23      */
24     public static final int FEATURE_ACTION_BAR = 8;
25     /**
26      * Flag for requesting an Action Bar that overlays window content.
27      * Normally an Action Bar will sit in the space above window content, but if this
28      * feature is requested along with {@link #FEATURE_ACTION_BAR} it will be layered over
29      * the window content itself. This is useful if you would like your app to have more control
30      * over how the Action Bar is displayed, such as letting application content scroll beneath
31      * an Action Bar with a transparent background or otherwise displaying a transparent/translucent
32      * Action Bar over application content.
33      *
34      * <p>This mode is especially useful with {@link View#SYSTEM_UI_FLAG_FULLSCREEN
35      * View.SYSTEM_UI_FLAG_FULLSCREEN}, which allows you to seamlessly hide the
36      * action bar in conjunction with other screen decorations.
37      *
38      * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN}, when an
39      * ActionBar is in this mode it will adjust the insets provided to
40      * {@link View#fitSystemWindows(android.graphics.Rect) View.fitSystemWindows(Rect)}
41      * to include the content covered by the action bar, so you can do layout within
42      * that space.
43      */
44     public static final int FEATURE_ACTION_BAR_OVERLAY = 9;
45     /**
46      * Flag for specifying the behavior of action modes when an Action Bar is not present.
47      * If overlay is enabled, the action mode UI will be allowed to cover existing window content.
48      */
49     public static final int FEATURE_ACTION_MODE_OVERLAY = 10;

通过上面的源码我们可以看到,如果我要定制自己的Title,我们需要采用Window.FEATURE_CUSTOM_TITLE这个特征来实现。
需要注意的是,Window.FEATURE_CUSTOM_TITLE这个特征不能其他特征一起使用,还有就是必须在setContentView()方法之前使用(这个是所有Window特征改变的必须条件),不然无效


首先是布局文件
01 <?xml version="1.0" encoding="utf-8"?>
02 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:layout_width="fill_parent"
04     android:layout_height="wrap_content" >
05  
06     <ImageView
07         android:layout_width="wrap_content"
08         android:layout_height="wrap_content"
09         android:layout_alignParentLeft="true"
10         android:background="@drawable/ic_launcher" />
11  
12     <TextView
13         android:id="@+id/title"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:layout_centerInParent="true"
17         android:shadowColor="#000986"
18         android:shadowDx="1"
19         android:shadowDy="1"
20         android:shadowRadius="1"
21         android:textColor="@android:color/white"
22         android:textSize="20sp"
23         android:textStyle="bold" />
24  
25     <ImageView
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content"
28         android:layout_alignParentRight="true"
29         android:background="@drawable/ic_launcher" />
30  
31 </RelativeLayout>

01 <?xml version="1.0" encoding="utf-8"?>
02 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:layout_width="fill_parent"
04     android:layout_height="wrap_content" >
05  
06     <ImageView
07         android:layout_width="wrap_content"
08         android:layout_height="wrap_content"
09         android:layout_alignParentLeft="true"
10         android:background="@drawable/ic_launcher" />
11  
12     <TextView
13         android:id="@+id/title"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:layout_centerInParent="true"
17         android:shadowColor="#000986"
18         android:shadowDx="1"
19         android:shadowDy="1"
20         android:shadowRadius="1"
21         android:textColor="@android:color/white"
22         android:textSize="20sp"
23         android:textStyle="bold" />
24  
25     <ImageView
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content"
28         android:layout_alignParentRight="true"
29         android:background="@drawable/ic_launcher" />
30  
31 </RelativeLayout>

我只加入了2张图片,大家可以按照自己的需要来添加元素



下面就上代码了
1 getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE);
2  
3 setContentView(R.layout.main);
4  
5 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

这样就实现了自定义Title的效果,当时,当我们运行在android sdk 3.0以上的时候会出现
报如下错误:android.util.AndroidRuntimeException: You cannot combine custom titles with other title features



原因在于,3.0以上的系统默认使用了Window.FEATURE_ACTION_BAR,我们需要使用自己的样式来修改它
首先考虑到不同版本,建立vlaues-v11和values-v14两个文件夹满足,3.0以上和4.0以上
看样式style.xml
改变背景色修改android:windowTitleBackgroundStyle的值,改变标题栏高度则修改 android:windowTitleSize的值
去掉windowActionBar特征
01 <resources>
02  
03     <!--
04         Base application theme for API 14+. This theme completely replaces
05         AppBaseTheme from BOTH res/values/styles.xml and
06         res/values-v11/styles.xml on API 14+ devices.
07     -->
08     <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
09         <!-- API 14 theme customizations can go here. -->
10         <item name="android:windowActionBar">false</item>
11         <item name="android:windowTitleSize">50dip</item>
12         <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
13     </style>
14      
15     <style name="CustomWindowTitleBackground">
16         <item name="android:background">@drawable/stripes</item>
17     </style>
18  
19 </resources>

接着再修改AndroidManifest.xml文件,找到要自定义标题栏的Activity,添加上android:theme值,       
<activity android:name=".MainActivity" android:theme="@style/activityTitlebar">  这样就解决了问题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值