Android-没有显示多个视图(仅显示第一个视图)

问题描述:

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

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

@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。为什么是这样?

如果你看起来here,还有一个人有基本相同的问题。但是,他们没有宣布他们的布局方向,所以默认为横向。在你的布局中,你已经明确声明了水平。这是打算(例如,使项目并排显示)?如果没有,将方向改为垂直,你应该很好。

如果你需要它们并排显示,那么我不确定如何做到这一点,但我想你需要将每个视图声明为放在它之前的视图旁边(例如,使用类似“alignToRightOf”。同样,这仅仅是一个刺,在这黑暗的,但它可以让你走的正确道路上。

希望这有助于。

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

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