2013-03-24 81 views
0

我试图在我的应用程序中自定义ToggleButton。我将背景设置为9-patch图像here。 然后在我的布局XML:9补丁背景添加顶部和底部填充。如何避免它?

<ToggleButton 
    android:id="@+id/toggle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:background="@drawable/btn_toggle_bg" 
    android:checked="true" 
    android:gravity="center_horizontal|center_vertical" /> 

btn_toogle_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:id="@+android:id/background" 
     android:drawable="@android:color/transparent"/> 
    <item 
     android:id="@+android:id/toggle" 
     android:drawable="@drawable/btn_toggle"/> 
</layer-list> 

btn_toggle.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/btn_toggle_off" android:state_checked="false"/> 
    <item android:drawable="@drawable/btn_toggle_on" android:state_checked="true"/> 
</selector> 

9补丁看起来像这样(btn_toggle_off):

btn_toggle_off

检查状态相同的图像。

但是当我申请这个背景我得到一些意想不到的顶部和底部填充:

enter image description hereenter image description here

我已经得到了应用此背景时Button或使用XML抽拉相同的结果作为背景。 如何避免意外的填充?请帮助。


更新:

此外,当我加入这种风格应用程序的主题不存在填充但ToggleButton becames无法点击(不改变其状态):

styles.xml

<style name="Widget.Button.Toggle" parent="android:Widget"> 
    <item name="android:background">@drawable/btn_toggle_bg</item> 
    <item name="android:disabledAlpha">?android:attr/disabledAlpha</item> 
</style> 

themes.xml

<style name="MyThemeName" parent="@android:Theme.Black"> 
    <item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item> 
</style> 
+0

你确定这是填充或绘制并没有看法?尝试在带有可绘制的容器中放置彩色视图(而不是文本视图),以检查。 – 2013-03-24 14:08:15

+0

@ User117我不确定,但请参阅更新。 – Sabre 2013-03-24 14:09:38

回答

1

看来,我需要延长android:Widget.Button.Toggle风格:

<style name="Widget.Button.Toggle" parent="android:Widget.Button.Toggle"> 
    <item name="android:background">@drawable/btn_toggle_bg</item> 
    <item name="android:disabledAlpha">?android:attr/disabledAlpha</item> 
</style> 
0

填充的原因是用于描述内容放置位置的右侧黑色像素应该是图像从上到下的整个长度。

试试这个:

enter image description here

+0

我试图这样做,但我得到了同样的结果。也没有内容线我得到了相同的。 – Sabre 2013-03-24 14:15:10

+0

我试过了。不工作。 – Sabre 2013-03-24 14:22:25

相关问题