ImageView从屏幕推出

时间:2014-01-17 21:58:57

标签: android android-tablelayout

我正在玩Android,我想要实现的是10x10tile桌面游戏。 我想读取屏幕的大小和宽度,然后我想要一个中间的正方形,上面和下面有一个textViews。

这是我到目前为止所做的:

    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);
    }
}

我尝试了不同的布局,但似乎没有任何工作完成。 :S

BR

1 个答案:

答案 0 :(得分: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>