【问题标题】:Programmatically created "Circle Button" not drawing以编程方式创建的“圆形按钮”不绘图
【发布时间】:2016-02-07 05:18:53
【问题描述】:

我有一个自定义的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 无法正常工作,但普通按钮可以。

【问题讨论】:

  • 您没有将按钮添加到布局中,您只是创建了它的一个实例。将其添加到 xml 布局文件或使用 contentview 动态添加
  • @user2395334 糟糕,刚刚添加了它。但是它仍然无法正常工作?也许我没有正确添加它?
  • 你能把xml的相关部分贴出来吗?
  • 顺便说一下,不要使用activity上下文来创建按钮,传递getapplicationcontext作为参数防止内存泄漏
  • 是的,我看到了,我删除了那条评论,但奇怪的是你有一个静态按钮

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


【解决方案1】:

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

以绝对大小为例:

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

虽然请记住 px 和 dip 之间的区别。您可能希望使用内部半径属性设置值,而不是对它们进行编码。

【讨论】:

猜你喜欢
  • 2021-01-31
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2011-06-29
  • 2023-03-25
相关资源
最近更新 更多