2014-01-17 86 views
0

我在Android上玩耍,我試圖達到的是一個10x10的桌面遊戲。 我想讀取屏幕的大小和寬度,然後我想要一個正方形在上面和下面的textViews中間。ImageView從屏幕推出

這是我迄今所做的:

public void init(){ 

     Point size = new Point(); 
     WindowManager w = getWindowManager();   

     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      w.getDefaultDisplay().getSize(size); 
      screenWidth = size.x; 
      screenHeight = size.y; 
     }else{ 
      Display d = w.getDefaultDisplay(); 
      screenWidth = d.getWidth(); 
      screenHeight = d.getHeight(); 
     } 

     LinearLayout linearLayout = (LinearLayout) findViewById(R.id.LinearLayout); 
     linearLayout.setOrientation(LinearLayout.VERTICAL); 

     TextView TVtop = new TextView(this.getApplicationContext()); 
     TVtop.setHeight((screenHeight-screenWidth)/2); 
     TVtop.setWidth(screenWidth); 
     TVtop.setText("TOP"); 

     TextView TVbot = new TextView(this.getApplicationContext()); 
     TVbot.setHeight((screenHeight-screenWidth)/2); 
     TVbot.setWidth(screenWidth); 
     TVbot.setText("BOT"); 

     TableLayout tableLayout = new TableLayout(this.getApplicationContext()); 

     //Make a cube 
     LayoutParams tableParams = new LayoutParams(screenWidth, screenWidth); 
     tableLayout.setLayoutParams(tableParams); 

     //Populate tableLayout 
     for(int i = 0; i < nrOfTiles; i++){ 

      TableRow tableRow = new TableRow(this.getApplicationContext()); 

      for(int j = 0; j < nrOfTiles; j++){ 

       ImageView imgView = new ImageView(this.getApplicationContext()); 
       imgView.setImageResource(R.drawable.cell); 
       tableRow.addView(imgView); 
      } 
      tableLayout.addView(tableRow); 
     } 

     linearLayout.addView(TVtop); 
     linearLayout.addView(tableLayout); 
     linearLayout.addView(TVbot); 
    } 
} 

我試過不同勢的LayoutParams但似乎沒有完成工作。 :S

BR

回答

1

就個人而言,我會使用的項RelativeLayoutsetMargins

儘管不直接回答您的問題,但以下代碼將在三行中顯示15個圖標。這應該足以解釋並讓你開始。

主要活動。

public class MainActivity extends Activity { 

    private int mWidth; 
    private int mTile; 
    private int mColMax = 5; 
    private Context mContext; 

    @SuppressWarnings("deprecation") 
    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mContext = this; 

     // the screen width is need to work out the tile size 
     WindowManager w = getWindowManager(); 

     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      Point size = new Point(); 
      w.getDefaultDisplay().getSize(size); 
      mWidth = size.x; 
     }else{ 
      mWidth = w.getDefaultDisplay().getWidth(); 
     } 

     // how wide (and high) each icon will be to fit the screen. 
     mTile = (mWidth/mColMax); 

     setContentView(R.layout.activity_main); 
     // layout the icons 
     initUI(); 
    } 

    /** 
    * Layout 15 icon images in three rows dynamically. 
    */ 
    private void initUI() { 
     // this is the layout from the XML 
     ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout); 

     ImageView iv; 
     RelativeLayout.LayoutParams params; 

     int i = 0; 
     int row = 0; 
     int col = 0; 
     do { 
      params = new RelativeLayout.LayoutParams(mTile,mTile); 
      params.setMargins((col * mTile), (row * mTile), 0, 0); 
      iv = new ImageView(mContext); 
      iv.setAdjustViewBounds(true); 
      iv.setScaleType(ScaleType.FIT_CENTER); 
      iv.setImageResource(R.drawable.ic_launcher); 
      iv.setLayoutParams(params); 
      layout.addView(iv); 
      if (col == mColMax) { 
       row++; 
       col = 0; 
      } else { 
       col++; 
      } 
     } while (++i <= 16); 
    } 
} 

和佈局XML。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</RelativeLayout>