码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-04-14 11:13:02      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:android应用   界面   布局   

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        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" />

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

</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>

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

标签:android应用   界面   布局   

原文地址:http://blog.csdn.net/true100/article/details/45038823

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!