当前位置:操作系统 > 安卓/Android >>

我的Android进阶之旅------>Android自定义窗口标题实例

该实例的功能比较简单,但是通过该实例的扩展可以在自定义标题中做出菜单导航等实用的功能,为了实现自定义窗口标题,需要做以下几个步骤:
1、给自定义标题提供一个界面
2、将自定义标题应用给Activity窗口
3、把android系统为Activity设置的默认主题改为自己的主题
 
============================下面查看实现该例子的具体代码================================
step1、新建一个项目MyCustomTitle
 
step2、编写自定义标题的布局文件 /res/layout/custom_title.xml
 
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="horizontal" android:layout_width="fill_parent"  
    android:layout_height="fill_parent" android:background="@drawable/rectangle"> <!-- 指定背景,该背景自己画的 -->  
  
    <Button android:id="@+id/infoAtMeTextView"  
        android:textColor="#FF0000" android:layout_width="wrap_content"  
        android:layout_height="match_parent" android:layout_weight="1"  
        android:gravity="center" android:text="\@我" />  
    <Button android:id="@+id/infoCommentTextView"  
        android:textColor="#FF0000" android:layout_width="wrap_content"  
        android:layout_height="match_parent" android:layout_weight="1"  
        android:gravity="center" android:text="评论" />  
    <Button android:id="@+id/infoPrivateMsgTextView"  
        android:textColor="#FF0000" android:layout_width="wrap_content"  
        android:layout_height="match_parent" android:layout_weight="1"  
        android:gravity="center" android:text="私信" />  
</LinearLayout>  

 

 
 
step3、上面布局文件中使用的背景是一个drawable文件,该drawable文件绘制了一个长方形。该文件是/drawable/rectangle.xml
 
<?xml version="1.0" encoding="utf-8"?>  
<!-- 下面定义了一个长方形 -->  
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="rectangle">  
    <gradient android:angle="270" android:endColor="#1DC9CD"  
        android:startColor="#A2E0FB" />  
    <padding android:left="2dp" android:top="2dp" android:right="2dp"  
        android:bottom="2dp" />  
</shape>  

 

 
 step4、将自定义标题设置到Activity中,CustomTitleActivity.java
 
package cn.oyp.title;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.Window;  
  
public class CustomTitleActivity extends Activity {  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //指定使用自定义标题  
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
        setContentView(R.layout.main);  
        //设置窗口的自定义标题布局文件  
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);  
          
    }  
}  

 

 
 
===================================== 读源码开始 ==============================================
 
 
然后运行该应用,发现用户设置后的自定义layout没有办法填充整个标题栏。
通过查看Android源代码得知Android系统为Activity的title默认设置了一个布局文件,该布局文件是core/res/res/layout/screen_title.xml
 
 
 
<?xml version="1.0" encoding="utf-8"?>  
<!-- Copyright (C) 2006 The Android Open Source Project  
  
     Licensed under the Apache License, Version 2.0 (the "License");  
     you may not use this file except in compliance with the License.  
     You may obtain a copy of the License at  
    
          http://www.apache.org/licenses/LICENSE-2.0  
    
     Unless required by applicable law or agreed to in writing, software  
     distributed under the License is distributed on an "AS IS" BASIS,  
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
     See the License for the specific language governing permissions and  
     limitations under the License.  
-->  
  
<!--  
This is an optimized layout for a screen, with the minimum set of features  
enabled.  
-->  
  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:fitsSystemWindows="true">  
    <FrameLayout  
        android:layout_width="match_parent"   
        android:layout_height="?android:attr/windowTitleSize"  
        style="?android:attr/windowTitleBackgroundStyle">  
        <TextView android:id="@android:id/title"   
            style="?android:attr/windowTitleStyle"  
            android:background="@null"  
            android:fadingEdge="horizontal"  
            android:gravity="center_vertical"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent" />  
    </FrameLayout>  
    <FrameLayout android:id="@android:id/content"  
        android:layout_width="match_parent"   
        android:layout_height="0dip"  
        android:layout_weight="1"  
        android:foregroundGravity="fill_horizontal|top"  
        android:foreground="?android:attr/windowContentOverlay" />  
</LinearLayout>  

 

 
 
读上一段代码可以发现该布局文件由两个帧布局构成,而其中的几个属性
?android:attr/windowTitleSize 标题高度
?android:attr/windowTitleBackgroundStyle     标题背景样式
?android:attr/windowContentOverlay 标题前景色
 
而这几个属性的值都是在core/res/res/values/themes.xml文件中被赋值了
 
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <!-- 该样式继承系统的默认样式 -->  
    <style name="customTheme" parent="android:Theme">  
        <!-- 设置标题前景色为透明 -->  
        <item name="android:windowContentOverlay">@drawable/nocolor</item>  
        <!-- 设置标题高度为44dp -->  
        <item name="android:windowTitleSize">44dp</item>  
        <!-- 设置标题背景色 -->  
        <item name="android:windowTitleBackgroundStyle">@style/customBg</item>  
    </style>  
      
    <!-- 定义一个背景样式 -->  
    <style name="customBg">  
        <item name="android:background">@drawable/rectangle</item>  
    </style>  
</resources>  

 

 
===================================== 读源码结束 ==============================================
step5、自定义样式 /res/values/style.xml
 
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <!-- 该样式继承系统的默认样式 -->  
    <style name="customTheme" parent="android:Theme">  
        <!-- 设置标题前景色为透明 --
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,