2012-06-18 124 views
0

我想對齊該欄右側的ToogleButton中的文本,該文本在按下時變爲綠色/白色。ToogleButton對齊右側的文本標籤

爲了更明確我會發布代碼和我現在的結果。

<ToggleButton android:id="@+id/ToggleButton01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        layout_gravity="center_vertical" 
        android:textOff="Off" 
       android:textOn="On"/> 

ToggleButton onOffButton = (ToggleButton) findViewById(R.id.ToggleButton01); 
onOffButton.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL); 
onOffButton.setPadding(0, 0, 5, 0); 

和結果: enter image description here

我想酒吧和文字不能重複。

回答

2

你試過了attibute gravity嗎?

android:gravity="center" 

android:layout_gravity="center"android:gravity="center"之間的性差異是第一個將集中你的View內的layout和第二中心將是View的內容。

這是我對你的按鈕的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ToggleButton android:id="@+id/ToggleButton01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical" 
        android:textOff="Off" 
        android:gravity="center" 
       android:textOn="On"/> 

</LinearLayout> 
+0

它不起作用..沒有效果。 – Paul

+0

好吧,我測試了它,所以你可能會重寫代碼中某處的值。 – Raykud

+0

不工作......基本上我想要的是(因爲也許我沒有表達得足夠精確)是與綠線水平對齊的文本(文本 - On/Off在綠線右側)。 – Paul

1

你正在尋找android:drawableLeft="@drawable/the_button_resource"

有喜歡drawableRight/drawableTop/drawableBottom,在帕拉姆地方繪製資源等選項建議

+0

請注意切換顏色狀態的變化必須在狀態xml中處理 - 即(the_button_resource)包含在「pressed的狀態」,「on_state」等時使用的資源。 – takrishna