0

我的应用程序中有一些垂直LinearLayouts,每个都包含 2 TextViews、 2ImgaeButtons和 a Button,每个 Layout 都必须动态创建,因为必须通过Run_Time获取一些数据,我是这样创建的:

// declaring Layout Params
                DisplayMetrics metrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metrics);

                temp_lay = new LinearLayout.LayoutParams(metrics.widthPixels/4, LayoutParams.MATCH_PARENT);
                temp_lay.setMargins(10, 15, 5, 0);


                lp_ineer_ver = new LinearLayout.LayoutParams(
                          LinearLayout.LayoutParams.MATCH_PARENT, 
                          LinearLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER);
                lp_ineer_ver.bottomMargin = 2;
                lp_ineer_ver.leftMargin = 12;
                lp_ineer_ver.topMargin = 2;

// Declaring Layouts and it's contents

            List<LinearLayout> inner_ver = new ArrayList<LinearLayout>();

                LinearLayout temp_inner_ver = new LinearLayout(this);
                temp_inner_ver.setLayoutParams(temp_lay);
                temp_inner_ver.setOrientation(LinearLayout.VERTICAL);
                temp_inner_ver.setWeightSum(2);
                temp_inner_ver.setPadding(7, 7, 7, 7);
                inner_ver.add(temp_inner_ver);


                LinearLayout icon1 = new LinearLayout(this);
                inner_ver.get(0).addView(icon1);

                icon1.setLayoutParams(lp_icon);

                icon1.setBackgroundResource(R.drawable.ac_overlay);
                icon1.setOrientation(LinearLayout.HORIZONTAL);
                icon1.setTag(Lights);

              TextView text1 = new TextView(this);
                icon1.addView(text1);
                LinearLayout.LayoutParams elements_params = (LinearLayout.LayoutParams)text1.getLayoutParams();    
                elements_params.gravity = Gravity.CENTER;
                elements_params.leftMargin = 3;
                elements_params.weight = 1;
                text1.setLayoutParams(elements_params);
                text1.setText("Living Room");
                text1.setTextSize(12);
                text1.setTextColor(Color.WHITE);
                text1.setGravity(Gravity.CENTER|Gravity.LEFT);

             ImageButton rgb1 = new ImageButton(this);
                icon1.addView(rgb1);
                rgb1.setLayoutParams(lp_ineer_ver);
                rgb1.setImageResource(drawable);
                rgb1.setBackgroundColor(Color.TRANSPARENT);

                Button rgp_value1 = new Button(this);
                icon1.addView(rgp_value1);
                elements_params = (LinearLayout.LayoutParams)rgp_value1.getLayoutParams(); 
                elements_params.bottomMargin = 2;
                elements_params.topMargin = 2;
                elements_params.leftMargin = 12;
                elements_params.gravity = Gravity.CENTER;
                rgp_value1.setLayoutParams(elements_params);
                rgp_value1.setText(status.toString());
                rgp_value1.setTextSize(12);
                rgp_value1.setBackgroundColor(Color.TRANSPARENT);
                rgp_value1.setTextColor(Color.WHITE);

            ImageButton cool1 = new ImageButton(this);
                icon1.addView(cool1);
                cool1.setLayoutParams(lp_ineer_ver);
                cool1.setImageResource(Color.TRANSPARENT);
                cool1.setBackgroundColor(Color.TRANSPARENT);

            TextView cool_value1 = new TextView(this);
                icon1.addView(cool_value1);
                elements_params = (LinearLayout.LayoutParams)cool_value1.getLayoutParams();    
                elements_params.gravity = Gravity.CENTER;
                elements_params.leftMargin = 12;
                elements_params.rightMargin = 3;
                cool_value1.setLayoutParams(elements_params);
                cool_value1.setGravity(Gravity.CENTER|Gravity.RIGHT);
                cool_value1.setText("17");
                cool_value1.setTextSize(12);


// Ading the Declared Layouts to the static defined Layout

            ver_rooms = (LinearLayout) findViewById(R.id.ver_rooms);
            ver_rooms.addView(inner_ver_rooms.get(0));

这是包含动态声明的布局的静态声明的布局:

     <HorizontalScrollView
        android:id="@+id/scroll_rooms"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/ver_rooms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:baselineAligned="false"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>

这项工作给了我这样的结果:

在此处输入图像描述

但我想要这个结果:

在此处输入图像描述

4

0 回答 0