一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

安卓Android 自定义标题栏(title栏)实例

时间:2013-11-29 编辑:简简单单 来源:一聚教程网

第一步,向实现自定义标题栏,需要在onCreate方法里这样写

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 

setContentView(R.layout.main); 

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); 

注意:
requestWindowFeature要在setContentView之前
getWindow().setFeatureInit最好在setContentView之后

 


第二步,就是写好自己的布局文件,实现标题栏的自定义。

不过我们会遇到一些问题,就是标题栏的高度不能自定义~下面就是解决办法~

下面通过实例来看一下如何实现。

1、 在layout下创建一个titlebtn.xml文件,内容如下:

 代码如下 复制代码

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="fill_parent"

   android:orientation="horizontal">

 


<<>ImageButton

       android:id="@+id/imageButton1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentLeft="true"

       android:layout_centerVertical="true"

       android:background="#00000000"

       android:src="@drawable/prv"/>

 


<<>TextView

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_centerInParent="true"

       android:text="标题栏"/>

 


<<>ImageButton

       android:id="@+id/imageButton1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentRight="true"

       android:layout_centerInParent="true"

       android:background="#00000000"

       android:src="@drawable/next"/>


修改style.xml文件

加入下面代码

 代码如下 复制代码

name="CustomWindowTitleBackground">

name="android:background">#00cc00

name="test"parent="android:Theme">

name="android:windowTitleSize">50dp

name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground

加入到AndroidManifest

 代码如下 复制代码


android:name=".CustomTitileBarActivity"
android:label="@string/app_name"android:theme="@style/test">
android:name="android.intent.action.MAIN"/>
 android:name="android.intent.category.LAUNCHER"/>

热门栏目