在屏幕上需要布局好多个相同的方格,点一下方格,颜色就改变了。在xml里面布局那么多view太麻烦了。动态布局不知道怎么去搞

解决方案 »

  1.   

    直接在view上用canvas画笔画格。点击时动态计算坐标是否点中就行
      

  2.   


    用gridview写总感觉怪怪的
      

  3.   

    动态布局很简单,可以尝试一下,下面是我用过的一个动态布局,你可以参考参考SoapObject response = (SoapObject) envelope.getResponse();
    Log.e("getResponse","getResponse");

    //TODO 填充通知
    if(response.getPropertyCount()==0)
    Toast.makeText(noticeActivity.this, "无系统通知", Toast.LENGTH_SHORT).show();
    for(int i=0;i<response.getPropertyCount();i++)
    {
    String value=String.valueOf(response.getProperty(i));
    String[] values=value.split("__");

    TableRow  row=new TableRow(this);
    final TextView textL=new TextView(this);
    textL.setText(values[0]);
    textL.setWidth(135);
    textL.setGravity(Gravity.LEFT);
    final TextView textC=new TextView(this);
    textC.setText(values[1]);
    textC.setWidth(85);
    textC.setGravity(Gravity.LEFT);
    final TextView textR=new TextView(this);
    textR.setText(values[2]);
    textR.setWidth(100);
    textR.setGravity(Gravity.RIGHT);
    row.addView(textL);
    row.addView(textC);
    row.addView(textR);
    final String noticeID=values[3];

    if(i%2==0)
    {
    row.setBackgroundColor(Color.DKGRAY);
    }

    row.setOnClickListener(
             new TableRow.OnClickListener()
             {
    public void onClick(View v) {
    LayoutInflater layoutInflater = LayoutInflater.from(noticeActivity.this); 
            View noticeView = layoutInflater.inflate(R.layout.notice, null); 
            
            String noticeContext=getNoticeContextByID(noticeID);
            TextView noticetxtview = (TextView) noticeView.findViewById(R.id.noticetxt);
            noticetxtview.setText(noticeContext);
            new AlertDialog.Builder(noticeActivity.this).setTitle("系统通知").setView( 
             noticeView).setNegativeButton("OK", 
                         new DialogInterface.OnClickListener() { 
                             public void onClick(DialogInterface dialog, int which) {
                             } 
                         }).show();
    }
             }
            );

    tableLayout.addView(row);
    }
      

  4.   

    我刚开始用的grid实现的,后来总觉得很怪,就用的canvas画的,谢谢各位
      

  5.   

    我的做法:
    http://blog.csdn.net/sanjinxiong/article/details/7232012