gpt4 book ai didi

android - 难以创建我自己的自定义 View

转载 作者:行者123 更新时间:2023-11-30 04:39:56 24 4
gpt4 key购买 nike

所以我试图在 xml 布局之外创建自己的 View ,但很难让它显示任何内容。我确定我遗漏了一些简单的东西,但看不到它可能是什么。任何输入表示赞赏。这是我用于测试目的的两个类。

public class TestSuite extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Turns off the application title at the top..
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Turns off the status bar at the top..
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(new Background(this));
}
}

public class Background extends View {

public Background(Context context) {
super(context);
}

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

public Background(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);

Resources res = getResources();

Rect baseRectBounds = new Rect(0,0,200,200);
Rect topRectBounds = new Rect(20,20,160,160);
Rect bottomRectBounds = new Rect(40,40,120,120);

Paint baseColor = new Paint(res.getColor(R.color.blue));
Paint topColor = new Paint(res.getColor(R.color.red));
Paint bottomColor = new Paint(res.getColor(R.color.green));

canvas.drawRect(baseRectBounds, baseColor);
canvas.drawRect(topRectBounds, topColor);
canvas.drawRect(bottomRectBounds, bottomColor);
}

}

编辑

这是我正在使用的最新 onDraw 方法。

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);

Resources res = getResources();

Rect canvasBounds = canvas.getClipBounds();

Log.v("CanvasBounds before", "left - " + canvasBounds.left);
Log.v("CanvasBounds before", "top - " + canvasBounds.top);
Log.v("CanvasBounds before", "right - " + canvasBounds.right);
Log.v("CanvasBounds before", "bottom - " + canvasBounds.bottom);

Rect baseRect = new Rect(0,0,200,200);
Rect topRect = new Rect(20,20,160,160);
Rect botRect = new Rect(40,40,120,120);

Paint baseColor = new Paint(res.getColor(R.color.red));
Paint topColor = new Paint(res.getColor(R.color.green));
Paint botColor = new Paint(res.getColor(R.color.blue));
baseColor.setStyle(Style.FILL);
topColor.setStyle(Style.FILL);
botColor.setStyle(Style.FILL);

canvas.drawRect(baseRect, baseColor);
canvas.drawRect(topRect, topColor);
canvas.drawRect(botRect, botColor);

canvasBounds = canvas.getClipBounds();

Log.v("CanvasBounds after", "left - " + canvasBounds.left);
Log.v("CanvasBounds after", "top - " + canvasBounds.top);
Log.v("CanvasBounds after", "right - " + canvasBounds.right);
Log.v("CanvasBounds after", "bottom - " + canvasBounds.bottom);
}

Log语句的结果如下:

CanvasBounds before left - 0
CanvasBounds before top - 0
CanvasBounds before right - 480
CanvasBounds before bottom - 800
CanvasBounds after left - 0
CanvasBounds after top - 0
CanvasBounds after right - 480
CanvasBounds after bottom - 800

完成编辑

感谢 Ted 的帮助,这是我最后的 onDraw 方法(对于可能需要它的其他人)。

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);

Resources res = getResources();

Rect baseRect = new Rect(0,0,200,200);
Rect topRect = new Rect(20,20,160,160);
Rect botRect = new Rect(40,40,120,120);

Paint color = new Paint();

color.setColor(res.getColor(R.color.red));
canvas.drawRect(baseRect, color);
color.setColor(res.getColor(R.color.blue));
canvas.drawRect(topRect, color);
color.setColor(res.getColor(R.color.green));
canvas.drawRect(botRect, color);
}

最佳答案

两个建议:

  1. 在你的c'tor中,调用setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT))(这里,LayoutParamsViewGroup.LayoutParams)
  2. 覆盖measure(int,int) 并按照docs for View 中的建议实现它

关于android - 难以创建我自己的自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6171308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com