在android活动中动态添加以下布局代码

在android活动中动态添加以下布局代码,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,我已经在AndroidLayouts文件夹xml文件中静态实现了一个布局,如下所示 <TableRow android:layout_height="wrap_content" android:layout_width="match_parent" > <LinearLayout andr

我已经在AndroidLayouts文件夹xml文件中静态实现了一个布局,如下所示

            <TableRow
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                >
                <LinearLayout
                    android:weightSum="1.0"
                    android:layout_weight=".75"
                    android:layout_height="match_parent"
                    android:layout_width="0dip"
                    android:background="#ffef1128"
                    >


                    <Button
                        android:id="@+id/textbox12"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.2"
                        android:layout_width="0dip"
                        android:textSize="14sp"
                        android:text="20%"
                        android:background="@color/fund_background_blue"
                        android:textColor="@color/gen_text_color"
                         />
                     </LinearLayout>

                <LinearLayout
                    android:layout_weight=".25"
                    android:layout_height="match_parent"
                    android:layout_width="0dip"
                    >


                    <TextView
                        android:id="@+id/TextView22"
                        android:text="12.4"
                        android:layout_weight=".15"
                        android:layout_height="wrap_content"
                        android:layout_width="0dip"
                        android:textColor="#000000"
                        android:gravity="right"
                        android:layout_gravity="center_vertical" />


                     <ImageView
                        android:src="@drawable/ic_up"
                        android:layout_height="15dp"
                        android:layout_width="0dip"
                        android:layout_weight=".05"
                        android:layout_gravity="center_vertical" />
                </LinearLayout>
            </TableRow>

如何在活动中以编程方式而不是在xml布局中添加这些内容。因为它涉及布局权重和权重总和

样本输出供参考


您可以从所有小部件中获取实例,并像这样以编程方式设置布局

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
 LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    layoutParams.weight=1;

    your_widget.setLayoutParams(layoutParams);

LinearLayout linLayout1=新的LinearLayout(此);LinearLayout.LayoutParams linLayoutParams1=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,LinearLayout.LayoutParams.WRAP_内容);linLayoutParams1.weight=0.75f//为线性布局1 linLayout1添加权重和。设置权重和(1.0f);linLayout1.setBackgroundColor(颜色:红色);linLayout1.设置重力(重力、中心和垂直);linLayout1.setLayoutParams(linLayoutParams1);兄弟,实际上我尝试了一些东西,比如代码共享,但结果却是空的