1
[android] 我有一個LinearLayout,它包含2個視圖,1st是imageView,第二個是canvas。 如果我做了mLinearLayout.addView(i);然後mLinearLayout.addView(c);它會顯示兩者,但如果我在反向操作(mLinearLayout.addView(c)然後mLinearLayout.addView(i))中執行操作,它只顯示畫布。 我想分享這兩個視圖之間的屏幕。有人可以幫助我嗎?是否可以在android中調整畫布大小?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLinearLayout = new LinearLayout(this);
mLinearLayout.setBackgroundColor(0xff74AC23);
ImageView i = new ImageView(this);
View c = new DrawView(this);
i.setImageResource(R.drawable.bol_groen);
i.setAdjustViewBounds(true);
mLinearLayout.addView(i);
mLinearLayout.addView(c);
setContentView(mLinearLayout);
}
}
公共類drawView函數擴展視圖{
private ShapeDrawable mDrawable;
public DrawView(Context context) {
super(context);
setFocusable(true); //necessary for getting the touch events
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}
@Override protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFCCCCCC);
mDrawable.draw(canvas);
}
你是什麼意思,它只顯示畫布? DrawView佔用整個屏幕,或者是正確的大小,但ImageView只是不顯示?還有一個原因,你不在xml中這樣做? – Idistic
請看看這篇文章;希望能幫到你 - http://stackoverflow.com/questions/11071258/scale-a-canvas-to-fit-a-view-programmatically –