0

OK, I know this issue has been covered in different questions but I'm trying a different approach here.

This is my custom View class:

public class MyView extends View {
    Button mButton;

    public MyView(Context context) {
        super(context);
        mButton = new Button(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //Sets the size needed.
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        //Draws some graphics.
        canvas.restore();

        canvas.save();
            RectF boundRect = new RectF(left,top,right,bottom);
            canvas.clipRect(boundRect);
            mButton.layout(0,0,canvas.getWidth(),canvas.getHeight());
            mButton.draw(canvas);
        canvas.restore();
    }
}

This draws the button in the correct position and size, but, the button seems half transparent and is not clickable. Does anybody know why and how to fix it?

4

2 回答 2

0

Did you set an OnClickListener in your Activity? Your View doesnt get shown clicked because you havent set the different styles for the click-states. Create a new xml file in your drawable folder, for example black_button_style:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
    <shape>
        <solid
            android:color="#343434" />
        <stroke
            android:width="1dp"
            android:color="#171717" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="3dp"
            android:top="10dp"
            android:right="3dp"
            android:bottom="10dp" />
    </shape>
</item>
<item>
    <shape>
        <gradient
            android:startColor="#343434"
            android:endColor="#171717"
            android:angle="270" />
        <stroke
            android:width="1dp"
            android:color="#171717" />
        <corners
            android:radius="4dp" />
        <padding
            android:left="3dp"
            android:top="10dp"
            android:right="3dp"
            android:bottom="10dp" />
    </shape>
</item>

In your xml-layout file set the background of your view:

android:background="@drawable/black_button_style"
于 2012-08-24T18:03:51.843 回答
0

I don't know if anyone is following this but I didn't feel it was right to leave the question open. It appears that you can do this directly extending the View class but you must also implement some interfaces that ViewGroup implements:

  • android.graphics.drawable.Drawable.Callback
  • android.view.KeyEvent.Callback
  • android.view.ViewManager
  • android.view.ViewParent
  • android.view.accessibility.AccessibilityEventSource

This will make the button clickable.

I also discovered that the transparency originates from the default ICS Holo Dark theme, so the fact the button is a little transparent is desirable.

Eventually, not wanting to reinvent the wheel, I chose to extend ViewGroup instead of View.

于 2012-11-17T15:47:08.487 回答