Android Activity定制需要的Title
2013-11-04 17:07
246 查看
Activity界面默认的Title是只有文字描述的,当我们想要做成类似微信中Tilte行带有导航和多功能效果的时候,我们就需要自己去定义需要的布局来加载它。上次我们讲到了Activity的全屏显示和去掉Title,说白了就是改变Window窗口features
/** Flag for the "options panel" feature. This is enabled by default. */ public static final int FEATURE_OPTIONS_PANEL = 0; /** Flag for the "no title" feature, turning off the title at the top * of the screen. */ public static final int FEATURE_NO_TITLE = 1; /** Flag for the progress indicator feature */ public static final int FEATURE_PROGRESS = 2; /** Flag for having an icon on the left side of the title bar */ public static final int FEATURE_LEFT_ICON = 3; /** Flag for having an icon on the right side of the title bar */ public static final int FEATURE_RIGHT_ICON = 4; /** Flag for indeterminate progress */ public static final int FEATURE_INDETERMINATE_PROGRESS = 5; /** Flag for the context menu. This is enabled by default. */ public static final int FEATURE_CONTEXT_MENU = 6; /** Flag for custom title. You cannot combine this feature with other title features. */ public static final int FEATURE_CUSTOM_TITLE = 7; /** * Flag for enabling the Action Bar. * This is enabled by default for some devices. The Action Bar * replaces the title bar and provides an alternate location * for an on-screen menu button on some devices. */ public static final int FEATURE_ACTION_BAR = 8; /** * Flag for requesting an Action Bar that overlays window content. * Normally an Action Bar will sit in the space above window content, but if this * feature is requested along with {@link #FEATURE_ACTION_BAR} it will be layered over * the window content itself. This is useful if you would like your app to have more control * over how the Action Bar is displayed, such as letting application content scroll beneath * an Action Bar with a transparent background or otherwise displaying a transparent/translucent * Action Bar over application content. * * <p>This mode is especially useful with {@link View#SYSTEM_UI_FLAG_FULLSCREEN * View.SYSTEM_UI_FLAG_FULLSCREEN}, which allows you to seamlessly hide the * action bar in conjunction with other screen decorations. * * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN}, when an * ActionBar is in this mode it will adjust the insets provided to * {@link View#fitSystemWindows(android.graphics.Rect) View.fitSystemWindows(Rect)} * to include the content covered by the action bar, so you can do layout within * that space. */ public static final int FEATURE_ACTION_BAR_OVERLAY = 9; /** * Flag for specifying the behavior of action modes when an Action Bar is not present. * If overlay is enabled, the action mode UI will be allowed to cover existing window content. */ public static final int FEATURE_ACTION_MODE_OVERLAY = 10;通过上面的源码我们可以看到,如果我要定制自己的Title,我们需要采用Window.FEATURE_CUSTOM_TITLE这个特征来实现。需要注意的是,Window.FEATURE_CUSTOM_TITLE这个特征不能其他特征一起使用,还有就是必须在setContentView()方法之前使用(这个是所有Window特征改变的必须条件),不然无效首先是布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:background="@drawable/ic_launcher" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:shadowColor="#000986" android:shadowDx="1" android:shadowDy="1" android:shadowRadius="1" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="@drawable/ic_launcher" /> </RelativeLayout>我只加入了2张图片,大家可以按照自己的需要来添加元素下面就上代码了
getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); 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特征
<resources> <!-- Base application theme for API 14+. This theme completely replaces AppBaseTheme from BOTH res/values/styles.xml and res/values-v11/styles.xml on API 14+ devices. --> <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"> <!-- API 14 theme customizations can go here. --> <item name="android:windowActionBar">false</item> <item name="android:windowTitleSize">50dip</item> <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> </style> <style name="CustomWindowTitleBackground"> <item name="android:background">@drawable/stripes</item> </style> </resources>接着再修改AndroidManifest.xml文件,找到要自定义标题栏的Activity,添加上android:theme值,
<activity android:name=".MainActivity" android:theme="@style/activityTitlebar">
这样就解决了问题
相关文章推荐
- Android Activity定制需要的Title
- AndroidGUI25:定制Activity的标题栏(Titlebar)
- Android开发:定制Activity的标题栏(Titlebar)
- AndroidGUI25:定制Activity的标题栏(Titlebar)(转载)
- AndroidGUI25:定制Activity的标题栏(Titlebar)
- Android开发:定制Activity的标题栏(Titlebar)
- Android:定制Activity的标题栏(Titlebar)
- AndroidGUI25:定制Activity的标题栏(Titlebar)
- Android:定制Activity的标题栏(Titlebar)
- Android 中去掉activity的title和status bar
- 关于Android创建Activity需要注意的地方
- android中使用startactivityforresult跳转Activity后需要重写onBackPressed()方法
- Android截取所需要的activity并上下拼接其他图片
- Android ApiDemos示例解析(5):App->Activity->Custom Title
- Android Activity的title的设置 .
- C/C++/IOS/Android/MFC/python等软件定制服务,有人需要吗?
- android开发中需要关闭指定activity的方法
- android界面之Activity的Title设置
- android创建一个Activity,并能够使用需要的步骤
- android界面研究 :Activity的Title设置