开发者

Android user interface with bitmaps problem

I have a class Ball which extents View.Inside there i give some characteristics and implements onTouchEvent() so i can handle the movement.Also i use onDraw so i can draw the bitmap of the ball. in my activity class i crate a new Layout and i add the view to it so it can be displayed. Everything works fine except when, i try to add more balls to my layout they don't appear!Always the first added in layout ball is displayed! Here's the onCreate code from activity class:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    int lHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
    int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;

    Point point1 = new Point();
    point1.x = 50;
    point1.y = 20;
    Point point2 = new Point();
    point2.x = 100;
    point2.y = 20;
    Point point3 = new Point();
    point3.x = 150;
    point3.y = 20;

    ColorBall ball1 = new ColorBall(this,R.drawable.bol_groen, point1);
    ll.addView(ball1, new LinearLayout.LayoutParams(lHeight, lWidth));
    setContentView(ll);

    ColorBall ball2 = new ColorBall(this,R.drawable.bol_rood, point2);
    ll.addView(ball2, new LinearLayout.LayoutParams(lHeight, lWidth));
    setContentView(ll);

    ColorBall ball3 = new ColorBall(this,R.drawable.bol_blauw, point3);

    ll.addView(ball3, new LinearLayout.LayoutParams(lHeight, lWidth));
    setContentView(ll);        

}

What could possibly be the problem?I have try it also with only one setContentView() at the 开发者_开发技巧end.I am thinking that i can't use a Layout so i can draw bitmaps which are in a custom View!Am i right? Should i change my code and create a View and inside there make an array with all the balls i want to be displayed and then set this View to be displayed from my main class of activity?(like this setContentView(customview)).


You are calling setContentView several times, while it is expected to call only once per activity initialization.

UPDATE:

Could you use this layout xml instead of programmatic way you use? This is just to be 100% sure the container to which you will add the ColorBalls is Ok.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/container"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" />

Just in case, here is the code to include it in the activity:

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

    setContentView(R.layout.your_name_of_layout);

    LinearLayout container = (LinearLayout) findViewById(R.id.container);
    ..
    container.addView(ball1);
    container.addView(ball2);
    ..
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜