动态布局无法正常工作

时间:2015-10-30 04:21:34

标签: android layout dynamic

在Android中创建动态布局时遇到问题。测试代码时我无法获得任何输出,虽然我提供背景颜色,但它在设备上显示为空白。我根据JSON大小使用了重量,最初是3,并且想要相应地创建块。

XML布局

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="9.5"
    android:orientation="horizontal"
    android:weightSum="10">

    <LinearLayout
      android:layout_width="0dp"
      android:layout_weight="0.5"
      android:layout_height="fill_parent"/>

      <LinearLayout
          android:layout_width="0dp"
          android:layout_weight="9"
          android:layout_height="fill_parent">

          <ScrollView
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

              <LinearLayout
                  android:layout_width="fill_parent"
                  android:orientation="vertical"
                  android:id="@+id/mainlinear"
                  android:layout_height="fill_parent">
              </LinearLayout>
          </ScrollView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="fill_parent"/>
</LinearLayout>

java代码

public class offers extends Activity {
    int len;
    String offerjson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.offers);

        offerjson = getIntent().getExtras().getString("offersjson");

        try {
            JSONArray jsonArray = new JSONArray(offerjson);
            len = jsonArray.length();
        } catch (Exception e) {
        }

        ///////////////////////////////////////////////////////////////////////////

        final LinearLayout lm = (LinearLayout) findViewById(R.id.mainlinear);
        lm.setWeightSum(len);

        try {
            JSONArray jsonArray = new JSONArray(offerjson);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject job = jsonArray.getJSONObject(i);
                //////////////////////////////// outer Layout
                LinearLayout l0 = new LinearLayout(offers.this);
                l0.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, 0, 1.0f));
                l0.setBackground(getResources().getDrawable(R.drawable.black_border));
                l0.setOrientation(LinearLayout.VERTICAL);
                l0.setWeightSum(4);

                ////////////////////////////////////inner 1 layout
                LinearLayout l1 = new LinearLayout(offers.this);
                l1.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, 0, 1.0f));
                l1.setBackgroundColor(Color.parseColor("#FFE7E7E7"));
                l1.setOrientation(LinearLayout.VERTICAL);

                l0.addView(l1);
                ///////////////////////////////Inner 2 LLayout
                LinearLayout l2 = new LinearLayout(offers.this);
                l2.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, 0, 2.0f));
                l2.setOrientation(LinearLayout.VERTICAL);

                l0.addView(l2);
                /////////////////////////Inner 3 LLayout
                LinearLayout l3 = new LinearLayout(offers.this);
                l3.setLayoutParams(new   LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, 0, 0.9f));
                l3.setWeightSum(4);
                l3.setOrientation(LinearLayout.HORIZONTAL);
                l3.setBackgroundColor(Color.parseColor("#FFABABA"));

                l0.addView(l3);

                //////////////////////////////////////

                LinearLayout l4 = new LinearLayout(offers.this);
                l4.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, 0, 0.1f));

                l4.setBackgroundColor(Color.parseColor("#FF52caf7"));
                l4.setOrientation(LinearLayout.VERTICAL);

                l0.addView(l4);
                lm.addView(l0);
            }
        } catch (Exception e) {
        }
    }
} 

0 个答案:

没有答案
相关问题