Android基础:代码中实现界面动态布局

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

            android:id="@+id/top_tv"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#ffffff"
        android:gravity="center"
        android:text="在代码中动态布局"
        android:textColor="#7B68EE" />

            android:id="@+id/child_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/top_tv"
        android:orientation="vertical" >
   

--------------------------------------------------------------

public class TestActivity extends Activity implements OnClickListener {
private LinearLayout childLinLayout;
private List> viewList = new ArrayList>();
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 strMap = new HashMap();
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-----------------------------



    #F0F0F0
    #D0D0D0
    #EC182E
    #C41e85
    #8B008B

你可能感兴趣的:(安卓整理)