0

我进行了很多搜索,但没有找到解决我问题的方法。当我创建多个视图并尝试将它们添加到 LinearLayout 时,仅显示第一个视图(蛋糕)。

这是我创建和添加视图的地方。

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.image_View);

    PlayAreaView cake = new PlayAreaView(SecondTestActivity.this, R.drawable.cake);
    views.add(cake);
    PlayAreaView bomb = new PlayAreaView(SecondTestActivity.this, R.drawable.bomb);
    views.add(bomb);
    PlayAreaView crown = new PlayAreaView(SecondTestActivity.this, R.drawable.crown);
    views.add(crown);
    PlayAreaView scissors = new PlayAreaView(SecondTestActivity.this, R.drawable.cut);
    views.add(scissors);
    PlayAreaView trash = new PlayAreaView(SecondTestActivity.this, R.drawable.bin_closed);
    views.add(trash);
    PlayAreaView key = new PlayAreaView(SecondTestActivity.this, R.drawable.bullet_key);
    views.add(key);

    LayoutParams params 
    = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    for(View v : views){
        Log.v("created", "view created");
        v.setLayoutParams(params);
        linearLayout.addView(v);
    }
}

这是我的 main.xml

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

我可以创建一个视图并且没问题,但我无法在 LinearLayout 中添加多个视图。为什么是这样?

4

2 回答 2

0

我找到了我的问题的答案。我不完全理解 Activity 如何处理视图。为了绘制多个单独的视图,我必须遍历添加到数组中的每个视图,并在自定义视图中调用覆盖的绘制方法。在我理解这一点后,我能够创建多个视图并在每个视图上添加单独的拖动功能。这是代码。

    public class ThirdTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LinearLayout layout = (LinearLayout) findViewById(R.id.main_View);
    layout.addView(new MyCircles(this));
}

private class MyCircles extends View{

    private Context myContext;
    private ArrayList<MyCircle> circles = new ArrayList<MyCircle>();
    private int size = 10;

    public MyCircles(Context context) {
        super(context);
        myContext = context;
        addCircles();
    }

    private void addCircles(){
        for (int i = 0; i < size; i++){
            circles.add(new MyCircle(myContext, R.drawable.skullcrossbones, i * 40, 50));
        }
    }

    @Override
    protected void onDraw(Canvas canvas){
        for (View v : circles){
            v.draw(canvas);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        int mouseX = (int)event.getX();
        int mouseY = (int)event.getY();
        MyCircle image = null;
        for(MyCircle images : circles){
            //Log.v("image checked X: " + images.imageX + ", Y: " + images.imageY, "checked");
            // Is the event inside of this view?
            if(images.getImageRect().contains((int)event.getX(), (int)event.getY()))
            {
                image = images;
            }
        }
        if (image != null){
            if(event.getAction() == MotionEvent.ACTION_DOWN)
            {
                Log.v("touched down", "touched down  at X: " + mouseX + ", Y: " + mouseY);
                image.dragDistance = new Point(mouseX, mouseY);
                bringToFront();
                isSelected();
                return true;
            }
            else if(event.getAction() == MotionEvent.ACTION_MOVE)
            {
                Log.v("move", "moving to X: " + mouseX + ", Y: " + mouseY);
                image.dragDistance.set(mouseX, mouseY);
                invalidate();
                return true;
            }
        }   
        return super.onTouchEvent(event);
    }       
}

private class MyCircle extends View{

    private int imageId;
    private Drawable image;
    private Context myContext;      
    private int size = 48;
    private int imageOffset = size/2;
    private int imageX;
    private int imageY;
    private Point dragDistance;

    public MyCircle(Context context, int id, int x, int y) {
        super(context);
        myContext = context;
        imageId = id;
        imageX = x;
        imageY = y;
        dragDistance = new Point(imageX + imageOffset, imageY + imageOffset);
    }

    public Rect getImageRect(){
        return image.getBounds();
    }

    @Override
    public void draw(Canvas canvas) {
        //Log.v("draw","drawn");
        super.onDraw(canvas);
        image = myContext.getResources().getDrawable(imageId);
        imageX = (dragDistance.x - imageOffset);
        imageY = (dragDistance.y - imageOffset);
        image.setBounds(imageX, imageY, imageX + size, imageY + size);
        image.draw(canvas);
    } 
      }
    }

这是为 Android 2.1 API 7 编写的

于 2012-04-11T19:18:39.427 回答
0

如果你看这里,有另一个人基本上有同样的问题。但是,他们没有声明布局的方向,因此默认为水平。在您的布局中,您已明确声明水平。这是有意的(例如让项目并排显示)?如果没有,请将方向更改为垂直,您应该会很好。

如果您需要它们并排显示,那么我不确定如何做到这一点,但我猜您需要将每个视图声明为放置在它之前的视图旁边(例如使用某些东西就像'alignToRightOf'。同样,这只是一个暗中的刺,但它可能会让你走上正确的道路。

希望这可以帮助。

于 2012-04-11T03:22:24.307 回答