2015-04-23 47 views

回答

0

好吧,讓我們假設你有一個這樣的活動:

public void onCreate (Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mainLayout); 

    ImageView imageView = (ImageView) findViewById(R.id.myCard); 

    Randon ran = new Random(); 
    int number = ran.nextInt(51); 

    switch(number){ 
     case (0): 
      imageView.setImageResource(R.drawable.card0); 
     case (1): 
      imageView.setImageResource(R.drawable.card1); 
     case (2): 
      imageView.setImageResource(R.drawable.card2); 
     //Rest of cases 
    } 
} 

現在讓我們假設你有一個mainLayout.xml文件RES內/佈局文件夾,看起來像這樣:

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

<ImageView 
    android:id="@+id/myCard" 
    android:src="@drawable/initialViewOfTheCard" 
    android:layout_width="match_parent" 
    android:layout_width="match_parent"/> 

</LinearLayout> 

你有!這很簡單,但我想你會明白的!

編輯:我建議你研究如何在你的佈局中使用GridLayouts!這將使事情變得更容易,以便同時顯示大量的卡片!

編輯2:您可以使用可繪製的ID和卡號作爲屬性對卡片POJO進行建模!然後你可以有一個Card對象列表,你可以使用一個ListAdapter來填充GridView!

相關問題