2011-11-02 39 views
0

我使用下面的XML文件進行自定義標題欄:的Android定製標題欄正在與一些問題

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/myTitle" 
android:text="custom title bar" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/background" /> 

而且我活動的onCreate()我有以下代碼中:

public class CustomTitleBar 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.mytitle); 
    } 
} 

標題欄沒有問題。我設置爲背景的圖像(圖像名稱也是背景,因爲您可以在上面的xml中看到)也正在出現。但是問題出現在圖像的左右兩側留下一點空隙,即圖像沒有覆蓋父母的整個寬度layout_width已被設置爲「fill_parent」。

任何任何idea.plz幫助。

回答

1

小間隙,以左/右是由框架因爲默認windowTitleBackgroundStylestandard theme使用的9-patch drawablepadding加入。下面是關於如何重寫一個例子:

在AndroidManifest.xml中,爲您的活動添加一個android:theme

<activity 
    android:theme="@style/MyCustomTitlebar" 
    android:label="Custom Titlebar" 
    android:name=".CustomTitlebar" /> 

然後在你的資源某處定義自定義主題(例如在res /價值/主題.XML):

<?xml version="1.0" encoding="UTF-8"?> 
<resources 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <style 
     name="MyCustomTitlebar" 
     parent="@android:style/Theme"> 
     <item 
      name="android:windowTitleBackgroundStyle">@style/MyBackground</item> 
    </style> 
    <style 
     name="MyBackground"> 
     <item 
      name="android:background">@drawable/background</item> 
    </style> 
</resources> 

因爲我們移動背景的風格,我們可以修改您的mytitle.xml佈局如下:

<?xml version="1.0" encoding="UTF-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myTitle" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="custom title bar" /> 

您可能需要調整背景(所以如果它是9補丁,它有一些填充),或者只需在mytitle.xml佈局中設置填充(使用paddingLeft/Right/Top/Bottom)。

0

檢查這篇文章是爲不同設備密度設計的自定義頁眉。 還使用樣式和主題設計您的標題。 Custom Header for Multiple Devices

+0

請檢查您的鏈接是唯一的這個問題的鏈接。 –

+0

@AndroidKiller請檢查現在..錯誤的鏈接張貼 – Venky