父布局
<LinearLayout
android:id="@+id/layout_all_chart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_callon_num"
android:layout_marginTop="24dp"
android:gravity="center"
android:minHeight="120dp"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/layout1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="14dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
子布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img_chart"
android:layout_width="9dp"
android:layout_height="9dp"
android:layout_gravity="center_vertical"
android:background="@color/static_color1"/>
<TextView
android:id="@+id/tv_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_gravity="center_vertical"
android:textColor="#838a99"
android:textSize="14sp"/>
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_gravity="center_vertical"
android:textColor="#747881"
android:textSize="17sp"/>
</LinearLayout>
代码块
动态添加的View中所要的颜色数组;
private int drawableLeftImg[] = { R.color.static_color1,
R.color.static_color2, R.color.static_color3,
R.color.static_color4, R.color.static_color5,
R.color.static_color6, R.color.static_color7,
R.color.static_color8, R.color.static_color9,
R.color.static_color10 };
<pre name="code" class="java">
<pre name="code" class="java">LinearLayout mLayout1 = (LinearLayout)findViewById(R.id.layout1);
LinearLayout mLayout2 = (LinearLayout)findViewById(R.id.layout2);
</pre>customers是从服务器上获取的JSONArray;<pre name="code" class="java"> for (int i = 0; i < customers.length(); i++) {
try {
JSONObject customer = (JSONObject) (customers.get(i));
LinearLayout layout = new LinearLayout(context);
View.inflate(context, R.layout.item_chart, layout);
ImageView img = (ImageView) layout
.findViewById(R.id.img_static_chart);
img.setBackgroundResource(<span style="font-family: Arial, Helvetica, sans-serif;">drawableLeftImg[]</span><span style="font-family: Arial, Helvetica, sans-serif;">[i]);</span>
TextView tv = (TextView) layout
.findViewById(R.id.tv_statistc_value);
TextView num = (TextView) layout.findViewById(R.id.tv_statistc_num);
tv.setText(customer.optString("value"));
num.setText(customer.optInt("count") + "");
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//取模,让布局分开左右两边排列
int index = i % 2;
if (index == 0) {
mLayout1.addView(ll, params);
} else {
mLayout2.addView(ll, params);
}
} catch (JSONException e) {
e.printStackTrace();
}