android界面之自定义标题栏
2012-09-19 11:40
357 查看
android自带的标题栏有点丑,开发时基本上都要重写自己的标题栏,下面是一点介绍。
在继承Activity的类,onCreate函数中,super.onCreate(savedInstanceState)函数调用之后,设置自定义标题栏,如
注意123三步顺序不能错,R.layout.m_title_button是我定义的XML布局文件,如下:
最早时候我是在RelativeLayout属性里头定义了背景的,当时做到这里还没运行之前,以为就已经全部搞定了,内心窃喜,不过运行之后就发现问题了,背景颜色没铺满整个标题栏,怎么办呢?后来百度了好久找到了解决的办法,如下:
项目浏览器里头,res/values/styles.xml里头定义style项,如下
titlebarstyle继承Theme主题样式,设置高度,设置背景项,下面再定义一个背景,然后在项目的AndroidManifest.xml中引用该类型就好,如下:
在继承Activity的类,onCreate函数中,super.onCreate(savedInstanceState)函数调用之后,设置自定义标题栏,如
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);//1 setContentView(R.layout.activity_main);//2 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.m_title_button);//3 }
注意123三步顺序不能错,R.layout.m_title_button是我定义的XML布局文件,如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/add_dict" android:layout_width="60dip" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="上一页" android:textColor="@color/white" android:textSize="12dip" android:background="@drawable/button_bg" /> <TextView android:id="@+id/textview_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="标题" android:textColor="@color/white"/> <Button android:id="@+id/title_btn_right" android:layout_width="60dip" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="下一页" android:textColor="@color/white" android:textSize="12dip" android:background="@drawable/button_bg" /> </RelativeLayout>
最早时候我是在RelativeLayout属性里头定义了背景的,当时做到这里还没运行之前,以为就已经全部搞定了,内心窃喜,不过运行之后就发现问题了,背景颜色没铺满整个标题栏,怎么办呢?后来百度了好久找到了解决的办法,如下:
项目浏览器里头,res/values/styles.xml里头定义style项,如下
<style name="titlebarstyle" parent="android:Theme"> <item name="android:windowTitleSize">38dip</item> <item name="android:windowTitleBackgroundStyle">@style/myTitleBarBackground</item> </style> <style name="myTitleBarBackground"> <item name="android:background">@drawable/titlebar_bg</item> </style>
titlebarstyle继承Theme主题样式,设置高度,设置背景项,下面再定义一个背景,然后在项目的AndroidManifest.xml中引用该类型就好,如下:
<activity android:theme="@style/titlebarstyle" android:name=".NewWordNote"> </activity>至此,运行完事。
相关文章推荐
- Android 浏览网页:WebView 嵌入浏览器(浏览历史返回、自定义加载失败界面、支持缩放、获取标题栏)
- Android 浏览网页:WebView 嵌入浏览器(浏览历史返回、自定义加载失败界面、支持缩放、获取标题栏)
- 解决android自定义标题栏充满的问题
- Android中自定义标题栏(二)
- 【Android 界面效果46】自定义view常处理的回调方法
- Android进阶——RecycleView的使用之自定义聊天界面(三)
- android自定义标题栏progressBar
- 自定义标题栏透明并且不影响界面展示,不占空间
- Android自定义标题栏学习详解!
- android自定义拍照界面SurfaceView+Camera
- Android 自定义标题栏
- 【Android自定义View实战】之自定义项目通用的标题栏CustomTitleBar
- [Android] 自定义顶部标题栏
- android自定义标题栏时候you cannot combine custom titles with other title
- Android界面开发之拖动条及自定义SeekBar
- [置顶] Android--(13)--Android自定义实现actionbar实现微信标题栏
- Android的Master/Detail风格界面中实现自定义ListView的单选
- Android自定义标题栏
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android 平板中 自定义键盘(popuwindow) 居于屏幕左下方 仿微信的密码输入界面