作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我试图在 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);
}
最佳答案
两个建议:
setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT))
(这里,LayoutParams
是ViewGroup.LayoutParams
) measure(int,int)
并按照docs for View
中的建议实现它关于android - 难以创建我自己的自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6171308/
我想模拟这个函数: function getMetaData(key) { var deferred = $q.defer(); var s3 = vm.ini
我是一名优秀的程序员,十分优秀!