</RelativeLayout>
--------------------------------------------------------------
public class TestActivity extends Activity implements OnClickListener {
private LinearLayout childLinLayout;
private List<Map<String, String>> viewList = new ArrayList<Map<String, String>>();
private int[] colors = { R.color.Color1, R.color.Color2, R.color.Color3, R.Color.color4, R.color.Color5};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
/**DisplayMetrics提供了一种关于显示的通用信息,如显示大小,分辨率和字体等*/
DisplayMetrics metrics = new DisplayMetrics();
/**获取屏幕分辨率及DisplayMetrics*/
getWindowManager().getDefaultDisplay().getMetrics(metrics);
/**设置两个宽度*/
int width1 = (metrics.widthPixels * 4 / 7);
int width2 = metrics.widthPixels * 3 / 7;
/**设置高度*/
int height = 80;
childLinLayout = (LinearLayout) findViewById(R.id.child_layout);
getListData();
for (int i = 0; i < viewList.size(); i++) {
/**动态设置布局*/
LinearLayout layout1 = new LinearLayout(this);
layout1.setOrientation(LinearLayout.HORIZONTAL);
layout1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
int i1 = (i + 1) * 2 - 1;
int i2 = (i + 1) * 2;
TextView tv1 = new TextView(this);
tv1.setId(i1);
tv1.setOnClickListener(this);
tv1.setGravity(Gravity.CENTER_VERTICAL);
tv1.setText(viewList.get(i).get("left"));
TextView tv2 = new TextView(this);
tv2.setId(i2);
tv2.setOnClickListener(this);
tv2.setGravity(Gravity.CENTER_VERTICAL);
tv2.setText(viewList.get(i).get("right"));
if (i % 2 == 0) {
tv1.setWidth(width1);
tv1.setHeight(height);
tv1.setBackgroundResource(colors[0]);
tv2.setWidth(width2);
tv2.setHeight(height);
tv2.setBackgroundResource(colors[1]);
}
else {
tv1.setWidth(width2);
tv1.setHeight(height);
tv1.setBackgroundResource(colors[2]);
tv2.setWidth(width1);
tv2.setHeight(height);
tv2.setBackgroundResource(colors[4]);
}
layout1.addView(tv1);
layout1.addView(tv2);
childLinLayout.addView(layout1);
}
}
/**添加文字数据*/
private void getListData() {
for (int i = 0; i < 6; i++) {
Map<String, String> strMap = new HashMap<String, String>();
strMap.put("left", "这是左侧数据" + (i + 1) + "left");
strMap.put("right", "这是右侧数据" + (i + 1) + "right");
viewList.add(strMap);
}
}
@Override
public void onClick(View v) {
TextView tv = (TextView) findViewById(v.getId());
String content = tv.getText().toString();
switch (v.getId()) {
case 1:
showToastMsg(content);
break;
case 2:
showToastMsg(content);
break;
case 3:
showToastMsg(content);
break;
case 4:
showToastMsg(content);
break;
case 5:
showToastMsg(content);
break;
case 6:
showToastMsg(content);
break;
case 7:
showToastMsg(content);
break;
case 8:
showToastMsg(content);
break;
case 9:
showToastMsg(content);
break;
case 10:
showToastMsg(content);
break;
case 11:
showToastMsg(content);
break;
case 12:
showToastMsg(content);
break;
}
}
private void showToastMsg(String content) {
Toast.makeText(TestActivity.this, content + content, Toast.LENGTH_SHORT).show();
}
}
--------res/values/colors.xml-----------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Color1">#F0F0F0</color>
<color name="Color2">#D0D0D0</color>
<color name="Color3">#EC182E</color>
<color name="Color4">#C41e85</color>
<color name="Color5">#8B008B</color>
</resources>
原文地址:http://blog.csdn.net/true100/article/details/45038823