当前位置: 代码迷 >> Android >> 在做一个触摸屏测试程序,有个有关问题想请问下
  详细解决方案

在做一个触摸屏测试程序,有个有关问题想请问下

热度:113   发布时间:2016-05-01 22:17:53.0
在做一个触摸屏测试程序,有个问题想请教下
在屏幕上需要布局好多个相同的方格,点一下方格,颜色就改变了。在xml里面布局那么多view太麻烦了。动态布局不知道怎么去搞

------解决方案--------------------
gridview
------解决方案--------------------
直接在view上用canvas画笔画格。点击时动态计算坐标是否点中就行
------解决方案--------------------
探讨
gridview

------解决方案--------------------
2楼的方法扩展性更好
------解决方案--------------------
动态布局很简单,可以尝试一下,下面是我用过的一个动态布局,你可以参考参考

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);
}
  相关解决方案