最近我在我正在處理的應用程序上的ToggleButtons失去了它的綠燈指示。我從各種教程開始,彙集了一組9個補丁指示器,以使它們再次正確工作。唯一的問題是現在的案文坐在指標之上,就像這樣:定製安卓ToggleButton顯示文本
我希望像舊開關按鈕在上面的文字...有沒有辦法做到這一點?
btn_toggle_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:drawable/btn_default" />
<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:state_checked="true" android:drawable="@drawable/btn_toggle_on" />
<item android:state_checked="false" android:drawable="@drawable/btn_toggle_off" />
</selector>
,並在我的活動切換按鈕標籤:
<ToggleButton
android:background="@drawable/btn_toggle_bg"
style="@style/OurThemeName"
android:textOff="@string/fertilizer"
android:textOn="@string/fertilizer"
android:id="@+id/toggleFertilizer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false">
</ToggleButton>
<ToggleButton
android:background="@drawable/btn_toggle_bg"
style="@style/OurThemeName"
android:textOff="@string/irrigation"
android:textOn="@string/irrigation"
android:id="@+id/toggleIrrigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false">
</ToggleButton>
差點忘了的themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Overwrite the ToggleButton style -->
<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">@drawable/btn_toggle_bg</item>
<item name="android:textOn">On</item>
<item name="android:textOff">Off</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
</style>
<style name="OurThemeName" parent="@android:Theme.Light">
<item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item>
<item name="android:textOn"></item>
<item name="android:textOff"></item>
</style>
</resources>
我最終只使用了holo theme 9 patch togglebuttons。你可以在可拖動的文件夾中找到它們以及默認的切換按鈕,並且它們已經被正確地格式化,以便將文本置於等等。 – tubby