2013-10-02 57 views
0

我從一個完全正常工作的應用程序開始,所有按鈕位於正確的位置和正確的尺寸......但現在我想嘗試第一次使用樣式。我特別想讓按鈕上的文字顏色爲深藍色,背景爲白色。所以我寫在styles.xml以下在res /值 - >Eclipse GUI不正確地顯示新樣式的按鈕

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <style name="mybut" parent="@android:style/Widget.Button"> 

     <item name="android:textColor">@color/dblue</item> 
     <item name="android:background">#ffffffff</item> 
    </style> 

</resources> 

我修改按鈕代碼如下:

<Button 
     android:id="@+id/spec" 
     style="@style/mybut" <-- I added this line here 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.12" 
     android:text="@string/spec" /> 

在Eclipse的XML瀏覽器,新的按鈕在各方面都合適的看着。但在運行時,在我的Android設備上,按鈕的高度縮小了大約三分之一!有任何想法嗎?

編輯:我對parent="@android:style/Widget.Button"位不是很有信心。我很懷疑,也許我已經在某種程度上已經在使用其他風格?/主題?也許線應該看起來類似於parent="@android:otherstyle/Widget.Button"parent="@android:style/other.Widget.Button" ...或類似的東西。

編輯:僅供參考......我正在嘗試一種只有兩個大按鈕的「主屏幕」活動。我將style="@style/mybut"添加到兩個按鈕中的一個。現在它們的尺寸明顯不同。

編輯:我注意到,在清單我android:theme="@android:style/Theme.NoTitleBar.Fullscreen" ...這是否意味着我需要讓我的按鈕的父android:theme="@android:style/Theme.NoTitleBar.Fullscreen.Widget.Button"?或類似的東西?

回答

1

默認情況下,Button有9補丁的背景下,不是一個簡單的顏色。該圖像具有填充和改變按鈕實際大小的內容區域。

當你改變背景時,你正在剝離該填充,並且它看起來更小。做到這一點的正確方法是基於舊的創建一個新的9補丁,但顏色已更改。

0

問題必須在這裏:

android:layout_weight="0.12" 

如果您在運行你的應用程序的按鈕將會收縮有action_bar或類似的東西。在你的預覽中,action_bar不會出現(我只是猜測)。

編輯
這裏是我在我的應用程序中使用自定義按鈕的樣本,我把一分鐘的高度和寬度。 而不是使用:parent="@android:style/Widget.Button"我使用:parent="android:Widget.Button"

<style name="ButtonCustom" parent="android:Widget.Button"> 
    <item name="android:background">@drawable/btn_default_holo_light</item> 
    <item name="android:minHeight">48dip</item> 
    <item name="android:minWidth">64dip</item> 
    <item name="android:textColor">#fff</item> 
</style> 
+0

謝謝,但高度變化(30%?)太激烈了,因此成爲問題。 – Mick

+0

@米克我編輯了答案,你可以試試這個解決方案嗎? – Dyna