2012-11-18 52 views
0

我有一個幾乎完成的應用程序,它使用ExpandableListView和每個子行的單個切換按鈕。我可以使用CheckBox或ToggleButton,但底層的CompoundButton不支持水平居中的按鈕圖形(wtf?)。所以我 建立了我自己的,但不知何故使用它,而不是在列表視圖中呈現。(Expandable)ListView中的自定義CompoundButton不可點擊(但CheckBox是)

在常規佈局中,自定義複合按鈕可以很好地工作。

一些魔法咒語似乎丟失了,我不太確定那會是什麼。我會感謝你的任何見解。

package net.shangtai.listener; 

import android.widget.CompoundButton; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.view.Gravity; 

class ImageToggle extends CompoundButton { 
    private Drawable buttonDrawable; 

    public ImageToggle(Context context) { 
     super(context, null); 
    } 
    public ImageToggle(Context context, AttributeSet attrs) { 
     super(context, attrs, 0); 
    } 
    public ImageToggle(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void setButtonDrawable(Drawable d) { 
     super.setButtonDrawable(d); 
     buttonDrawable=d; 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     if (buttonDrawable != null) { 
      final int gravity = getGravity(); 
      final int height = buttonDrawable.getIntrinsicHeight(); 
      final int width = buttonDrawable.getIntrinsicWidth(); 

      int top=0; 
      int bottom=0; 
      int left=getWidth(); 
      int right=getHeight(); 

      switch (gravity) { 
       case Gravity.TOP: 
        top=0; 
        bottom=height; 
        break; 
       case Gravity.BOTTOM: 
        top=getHeight() - height; 
        bottom=0; 
        break; 
       case Gravity.CENTER_VERTICAL: 
        top=(getHeight() - height)/2; 
        bottom=top+height; 
        break; 
       case Gravity.LEFT: 
        left=0; 
        right=width; 
        break; 
       case Gravity.RIGHT: 
        left=getWidth() - width; 
        right=0; 
        break; 
       case Gravity.CENTER_HORIZONTAL: 
        left=(getWidth() - width)/2; 
        right=left+width; 
        break; 
       case Gravity.CENTER: 
        left=(getWidth() - width)/2; 
        right=left+width; 
        top=(getHeight() - height)/2; 
        bottom=top+height; 
        break; 
      } 

      buttonDrawable.setBounds(left, top, right, bottom); 
      buttonDrawable.draw(canvas); 
     } 
    } 
} 

回答

1

我認爲是缺少神奇的是從一個默認樣式屬性爲自定義窗口小部件的自動應用,特別是機器人:可點擊=真。也許令人驚訝的是,按鈕默認是不可點擊的;這是通過造型設置的。現在

,如果這是你的問題,這裏有兩種方法如何解決這一問題:

  1. 快速修復:setClickable(真)在你的widget的構造(S),也許除非機器人:可點擊已明確設置爲false。有人可能會說,一個按鈕默認點擊,或

  2. 更復雜:按照的How to: Define theme (style) item for custom widget

對於2公認的答案列出的程序,請確保您定義默認的風格爲您定製小部件在例如style_imagetoggle.xml有父「可點擊」的風格,如Widget.CompoundButton,或設置機器人:可點擊=真直接:

<resources> 
    <!-- Default style attribute values, can be expanded upon in themes --> 
    <style name="Widget.CompoundButton.ImageToggle" parent="@android:style/Widget.CompoundButton" /> 
</resources> 

注:以下可爲什麼這一切都是必要: Issue 12683: Problem with the defStyle parameter of the View class constructor

+0

謝謝,我也有一段時間回到你身邊,但這是一個偉大,清晰和詳盡的答案。我做了一個小測試,android:clickable可以像你所描述的那樣工作。 – Staffan

0

是這個問題特別是如果你的groupView行自定義複選框?您的複選框必須設置爲不可聚焦的它不能直接與groupview展開/摺疊指示符相鄰(嘗試在它們之間放置textview或其他東西)。出於某種原因,這就像他們爭取支配點擊時所表達的內容。

+0

不,正如我寫的,它是在一個子視圖,而不是組。我已經嘗試在xml和代碼中將它設置爲非可聚焦的,但是這沒有任何作用(請注意,僅僅用CheckBox或ToggleButton *替換「mybutton」不會工作,只是無法居中在那些按鈕圖形) – Staffan

相關問題