以编程方式创建"圆形按钮"不画画

时间:2016-02-06 16:17:38

标签: android android-custom-view android-button

我有一个自定义CircleButton类:

public class CircleButton extends ImageView {

private  int radius;
private  int x;
private  int y;

public CircleButton(Context context) {
    super(context);
    constructorTask();
}

public CircleButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    constructorTask();
}

public CircleButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    constructorTask();
}

public CircleButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    constructorTask();
}


private void constructorTask() {
    x = 300;
    y = 300;
    radius = 100;
}

@Override
public void setPressed(boolean pressed) {
    super.setPressed(pressed);
    Log.i("Button Logger","Button Pressed");
}

@Override
protected  void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawCircle(x, y, radius, GameView.green);
    Log.i("Drawing status", "CircleButton Drawing...");
}

}

我有一项活动。此活动包含具有单个自定义视图的相对布局。

以下是自定义视图:

public class GameView extends View {

public static Paint green = new Paint();

public GameView(Context context) {
    super(context);
    green.setARGB(255,0,255,0);
}

public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    green.setARGB(255, 0, 255, 0);

}

public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    green.setARGB(255, 0, 255, 0);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Log.i("GameView Draw Status","Drawing...");
    Main.testButton.invalidate();
    invalidate();
}

}

这是活动代码:

public class Main extends AppCompatActivity {

public static CircleButton testButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    testButton = new CircleButton(getApplicationContext());
    makeFullScreen();

        RelativeLayout screenLayout = (RelativeLayout) findViewById(R.id.screenLayout);
    screenLayout.addView(testButton);
}

private void makeFullScreen() {...}

 }

由于某些原因,我的testButton没有被绘制。为什么不被抽出?

编辑一:这是我的XML。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context="com.example.vroy.customcirclebuttontest.Main"
    android:id="@+id/screenLayout">

    <com.example.vroy.customcirclebuttontest.GameView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        android:id="@+id/gameScreen" />


</RelativeLayout>

编辑二:我通过在相对布局中添加一个普通按钮进行了一些进一步的调试,它运行良好。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testCircleButton = new CircleButton(getApplicationContext());
        makeFullScreen();

        testButton = new Button(getApplicationContext());
        testButton.setX(100);
        testButton.setY(100);
        testButton.setText("HELLO WORLD");

        RelativeLayout screenLayout = (RelativeLayout) findViewById(R.id.screenLayout);
        screenLayout.addView(testCircleButton);
        screenLayout.addView(testButton);

        Log.i("Button Status","Adding Button To Layout");
    }

由于某种原因,circleButton无效,但正常的按钮是。

1 个答案:

答案 0 :(得分:1)

您没有指定视图的大小(layout_widthlayout_height),因此您的视图将在0px xpp空间内呈现,因此不可见。 在将视图添加到布局之前,您可以使用LayoutParams以编程方式设置这些内容。

例如,绝对大小:

testButton.setLayoutParams(new ViewGroup.LayoutParams(100,100));

尽管请记住px和dip之间的区别。您可能希望使用内部radius属性设置值而不是对其进行编码。