2012-04-11 97 views
0

我搜索了很多並沒有找到解決我的問題的方法。當我創建多個視圖並嘗試將它們添加到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。爲什麼是這樣?

回答

0

如果你看起來here,還有一個人有基本相同的問題。但是,他們沒有宣佈他們的佈局方向,所以默認爲橫向。在你的佈局中,你已經明確聲明瞭水平。這是打算(例如,使項目並排顯示)?如果沒有,將方向改爲垂直,你應該很好。

如果你需要它們並排顯示,那麼我不確定如何做到這一點,但我想你需要將每個視圖聲明爲放在它之前的視圖旁邊(例如,使用類似「alignToRightOf」。同樣,這僅僅是一個刺,在這黑暗的,但它可以讓你走的正確道路上。

希望這有助於。

0

我找到了答案,我的問題,我沒有完全理解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