2010-04-19 25 views
15

因此,我使用this thread中的技術爲我的標題欄使用自定義背景。不幸的是,框架將我的佈局放置在一個FrameLayouttitle_container)中,其中有填充,如下所示。沒有填充的自定義標題欄(Android)

alt text http://lh3.ggpht.com/_KP55v0Zt2pk/S80CUbrYkCI/AAAAAAAAARY/FJ_WPt8ah2s/s800/title_bar.png

反正是有刪除灰邊框?框架佈局在com.android.internal.R.id.title_container中定義,因此通過ID訪問框架會很脆弱。

回答

16

我一直在努力解決這個問題,現在我有了答案!

正如你可能已經看到了好幾個地方,你需要創建的themes.xml資源:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="MyTheme" parent="android:Theme"> 
      <item name="android:windowTitleSize">44dip</item> 
      <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item> 
    </style> 
</resources> 

注意它是不是經常在網站上提到談論自定義標題欄,你需要在選擇這個主題視圖,活動或應用程序級別。我這樣做是在應用層加入安卓主題屬性的應用程序:

<application android:icon="@drawable/icon" 
       android:label="@string/app_name" 
       android:theme="@style/MyTheme"> 

你還需要一個styles.xml定義WindowTitleBackgroundStyle。不像這個文件我已經看到漂浮aroung網絡的各種版本,我已經添加了一個額外的線路,設置填充爲0,其擺脫了填充的文件,你都在抱怨:

<?xml version="1.0" encoding="UTF-8"?> 
<resources> 
    <style name="WindowTitleBackground"> 
     <item name="android:background">@android:color/transparent</item> 
     <item name="android:padding">0px</item> 
    </style> 
</resources> 
+0

謝謝,這對我有很大的幫助。 – 2011-11-03 18:49:39

+0

ViewGroup上的setPadding沒有爲我做。你用0填充設置背景的建議做到了。 – AlanKley 2012-01-23 03:19:49

+0

非常感謝,這工作完美。 – 2012-11-27 10:34:03

1

有上anddev.org一個lenghty線程在那也許能夠幫助你

http://www.anddev.org/my_own_titlebar_backbutton_like_on_the_iphone-t4591.html

我也跟着它,併成功創建了自己的標題欄,讓我來設置填充。

XML對於我的標題欄:


<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/header" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="38px" android:layout_width="fill_parent" 
android:background="@drawable/gradient"> 

<TextView android:id="@+id/title" android:layout_width="wrap_content" 
android:gravity="center_vertical" style="@style/PhoneText" 
android:layout_alignParentLeft="true" 
android:text="New Title" android:background="@android:color/transparent" 
android:layout_height="wrap_content" android:layout_alignParentTop="true" 
android:padding="5dip" android:layout_marginBottom="7px"/> 

<TextView android:id="@+id/time" android:layout_width="wrap_content" 
android:gravity="center_vertical" style="@style/PhoneText2" 
android:layout_alignParentRight="true" 
android:text="Test Text" android:background="@android:color/transparent" 
android:layout_height="wrap_content" android:layout_alignParentTop="true" 
android:padding="4dip" android:layout_marginBottom="7px"/> 
</RelativeLayout> 

上面創建一個帶文本的小灰欄對齊左右兩側

+0

這實際上並不適用於我。我試圖刪除填充,而不是添加額外的填充 – Casebash 2010-04-19 23:47:51

7

其實沒有必要使用自定義佈局來自定義背景圖片。在theme.xml下面的代碼將工作:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="TitlebarBackgroundStyle"> 
     <item name="android:background">@drawable/header_bg</item> 
    </style> 
    <style name="Theme.OTPMain" parent="android:Theme"> 
     <item name="android:windowTitleBackgroundStyle">@style/TitlebarBackgroundStyle</item> 
    </style> 
</resources> 

但是,如果你真的想使用一個自定義標題欄,然後將下面的代碼將刪除保證金:

ViewGroup v = (ViewGroup) findViewById(android.R.id.title); 
v.setPadding(0, 0, 0, 0) 

title_bar_background被設置爲XML中背景圖像的ID。我設置了android:scaleType="fitXY"

+0

它似乎不適用於Android 4.你有什麼想法,爲什麼會這樣?我使用android:Theme.Holo.Light作爲父母 – nLL 2012-05-09 21:59:16

1

我通過獲得標題容器並將填充設置爲0來消除填充。它適用於Android 4。

titleContainerId = (Integer)Class.forName("com.android.internal.R$id").getField("title_container").get(null); 

ViewGroup vg = ((ViewGroup) getWindow().findViewById(titleContainerId)); 
vg.setPadding(0, 0, 0, 0); 
1

正如dalewking提到的,你可以改變清單是這樣的:

<application android:icon="@drawable/icon" 
      android:label="@string/app_name" 
      android:theme="@style/MyTheme"> 

但直到我說

android:theme="@style/MyTheme" 

到活動中它的自我喜歡它並沒有爲我工作:

<activity 
     android:name=".MainActivity" 
     android:screenOrientation="portrait" 
     android:theme="@style/CustomTheme" >    
    </activity> 
0

由於4.2和ADT 21創建默認佈局標準尺寸被添加到父容器時:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

</RelativeLayout> 

值位於在res/dimens.xml:

<!-- Default screen margins, per the Android Design guidelines. --> 
<dimen name="activity_horizontal_margin">16dp</dimen> 
<dimen name="activity_vertical_margin">16dp</dimen> 

</resources> 

無論是從父除去它們或將dimens.xml值更改爲所需的值。

0

我添加了以下填充項目AppTheme在styles.xml

<style name="AppTheme" parent="AppBaseTheme"> 
... 
<item name="android:padding">0dp</item> 
... 
</style> 

應用程序清單,使用該主題:

<application 
... 
android:theme="@style/AppTheme" > 
... 

這解決了對我來說。