簡體   English   中英

(Expandable)ListView中的自定義CompoundButton是不可單擊的(但CheckBox是可單擊的)

[英]Custom CompoundButton in (Expandable)ListView is not clickable (but a CheckBox is)

我有一個即將完成的應用程序,該應用程序使用ExpandableListView,並且每個子行都有一個切換按鈕。 我可以很好地使用CheckBox或ToggleButton,但是底層的CompoundButton不支持按鈕圖形(wtf?)的水平居中。 因此,我構建了自己的應用程序,但是以某種方式使用它卻使其在列表視圖中不可點擊。

在常規布局中,自定義的復合按鈕可以完美地工作。

一些不可思議的咒語似乎丟失了,我不確定那會是什么。 我會感謝您的任何見解。

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);
        }
    }
}

我認為缺少的魔術是自定義控件的默認樣式的屬性的自動應用 ,特別是android:clickable = true 也許令人驚訝的是,默認情況下按鈕是不可單擊的。 這是通過樣式設置的。

現在,如果這是您的問題,可以通過以下兩種方法解決此問題:

  1. 快速修復:小部件的構造函數中的setClickable(true) ,除非android:clickable已顯式設置為false 有人可能會爭辯說默認情況下Button是可單擊的,或者

  2. 更詳盡:按照“ 如何:為自定義窗口小部件定義主題(樣式)”的可接受答案中概述的過程進行操作

對於2,請確保您在style_imagetoggle.xml中為自定義窗口小部件定義了默認樣式,該樣式具有“可點擊”樣式,例如Widget.CompoundButton為父級,或直接設置android:clickable = true

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

注意:以下可能是為什么需要所有這些的原因: 問題12683:View類構造函數的defStyle參數出現問題

如果您的groupView行中的自定義復選框是否特別是此問題? 您的復選框必須設置為不可聚焦並且不能直接與groupview展開/折疊指示器相鄰(嘗試在它們之間放置textview或其他內容)。 出於某種原因,這就像是他們在爭奪點擊時要表達的內容的主導地位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM