標題欄設置

很多網友發現自己Android程序的標題欄TitleBar區域很單調,如果想個性化一些可以通過下面的方法來爲自己軟件的標題定製一個layout佈局文件,比如瀏覽器的標題欄,它包含了網站的Favicon,自定義的進度條,和不確定的進度指示等等,實現的方法自己控制吧。下面代碼在onCreate 中使用,同時順序不要改變,否則將無法生效:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);   
setContentView(R.layout.main);   //軟件activity的佈局
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);  //titlebar爲自己標題欄的佈局
這樣雖然可以在一定程度上定製標題欄, 不過, 這裏無法改變標題欄的高度和背景(背景設置之後會在兩端有兩個非常難看的邊框).  據說, 原因是android 固有的. 
這裏有修改方法:
原理是這樣的. 直接像上述代碼那樣添加title僅僅是把一個子界面添加到原有的title上的, 並沒有改變原來的屬性, 比如 標題欄大小, 標題欄背景. 這些需要在theme 主題裏面定義. 
因此先定義一個style, 若修改背景請修改android:windowTitleBackgroundStyle
若修改標題欄高度,請修改android:windowTitleSize
例子:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">


<style name="CustomWindowTitleBackground">
       
<item name="android:background">#565656</item>
</style>


<style name="test" parent="android:Theme">
     
<item name="android:windowTitleSize">50dp</item>
     
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>
在程序的android manifest.xml中對應activity中添加屬性  android:theme = "@style/test"  就可以了
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package
="com.guardian"
      android:versionCode
="1"
      android:versionName
="1.0">
    
<application android:icon="@drawable/icon" android:label="@string/app_name" >
        
<activity android:name=".Guardian"
                  android:label
="@string/app_name"
                  android:theme 
= "@style/test"   //就在這裏
                  
>
            
<intent-filter>
                
<action android:name="android.intent.action.MAIN" />
                
<category android:name="android.intent.category.LAUNCHER" />
            
</intent-filter>
        
</activity>

    
</application>
    
<uses-sdk android:minSdkVersion="4" />

</manifest> 
之後藉助於設置自定義的標題欄xml文件,就可以自定義標題欄佈局了

Android改變窗口標題欄的佈局

 

一、  重點
一般應用的Title都是建立應用時在AndroidManifest.xml中配置的,或是用setTitle設置的簡單字符串,要是想加入按鈕,圖片等多個複雜的佈局,使用以下方法:
在窗口建立時,可以把一個xml佈局設置成該應用的Title

二、  實例

a)       功能:把title設置成爲一個字串和一個按鈕的組合

b)       修改xxActivity.java代碼
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.title);
}

c)          填加title.xml代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android=
http://schemas.android.com/apk/res/android
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
      <TextView android:id="@+id/text"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:text="text" />  
      <Button android:id="@+id/button"  
        android:layout_width="wrap_content"  
        android:layout_height="30px"  
        android:text="button" /> 
</LinearLayout>

三、  注意

a)          注意設置順序
requestWindowFeature
要在setContentView之前
getWindow().setFeatureInit
最好在setContentView之後

b)         注意requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)不要和其它對TITLE的設置requestWindowFeature(xxxx)一起使用

轉自:http://java-admin.iteye.com/blog/813158

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章