Android-多个视图未显示。(仅显示第一个视图)

时间:2012-04-11 02:59:13

标签: android android-layout android-view

我搜索了很多,但没有找到解决问题的方法。当我创建多个视图并尝试将它们添加到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中添加多个视图。这是为什么?

2 个答案:

答案 0 :(得分:0)

如果你看here,那么另一个人的问题基本相同。然而,他们没有宣布他们的布局方向,因此默认为水平。在您的布局中,您已明确声明水平。这是否打算(例如让项目并排显示)?如果没有,请将方向更改为垂直方向,您应该很好。

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

希望这有帮助。

答案 1 :(得分:0)

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

    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编写的