2014-01-17 54 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>