动态 Layout 代Android

发布时间: 2014/8/19 4:38:02
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有问题生成下面提到的 android layout 的代码。

                    <RelativeLayout
                    android:id="@+id/profile_info_gender"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:id="@+id/ProfileTable"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#D9D9D9"
                        android:baselineAligned="false"
                        android:orientation="horizontal" >

                        <RelativeLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:layout_weight="0.5"
                            android:background="#F3F3F3"
                            android:orientation="horizontal" >

                            <TextView
                                android:id="@+id/gender_txt"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="15dp"
                                android:layout_marginTop="8dp"
                                android:textColor="@color/info_left"
                                android:textSize="14sp"
                                android:textStyle="bold" />
                        </RelativeLayout>

                        <RelativeLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="10dp"
                            android:layout_weight="0.5"
                            android:background="@color/white"
                            android:orientation="horizontal" >

                            <TextView
                                android:id="@+id/profile_info_text_gender"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="15dp"
                                android:layout_marginTop="8dp"
                                android:textSize="14sp" />
                        </RelativeLayout>
                    </LinearLayout>
                </RelativeLayout>

我试过很多事情从谷歌和博客,但不是能结束这 layout 任何帮助将高度赞赏和将会羡慕的谢谢

解决方法 1:

试试这个。

    LinearLayout first_lay = new LinearLayout(this);

    LinearLayout.LayoutParams lp_icon = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.MATCH_PARENT, 
              LinearLayout.LayoutParams.MATCH_PARENT);

    first_lay.setOrientation(LinearLayout.HORIZONTAL);
    first_lay.setLayoutParams(lp_icon);

    LinearLayout left_lay = new LinearLayout(this);

    LinearLayout.LayoutParams left_icon = new LinearLayout.LayoutParams(
              0, 
              LinearLayout.LayoutParams.WRAP_CONTENT,1);

    left_lay.setGravity(Gravity.CENTER);

    left_lay.setBackgroundColor(Color.parseColor("#696969"));

    left_lay.setLayoutParams(left_icon);


     LinearLayout.LayoutParams tests = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.WRAP_CONTENT, 
              LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);

     TextView text1 = new TextView(this);
     left_lay.addView(text1);
     text1.setLayoutParams(tests);
     text1.setText("Left Text");
     text1.setTextSize(18);

     first_lay.addView(left_lay);

     LinearLayout right_lay = new LinearLayout(this);

    LinearLayout.LayoutParams right_icon = new LinearLayout.LayoutParams(
              0, 
              LinearLayout.LayoutParams.WRAP_CONTENT,1);

    right_lay.setGravity(Gravity.CENTER);

    right_lay.setBackgroundColor(Color.parseColor("#D9D9D9"));

    right_lay.setLayoutParams(right_icon);

    TextView text2 = new TextView(this);
    right_lay.addView(text2);
     text2.setLayoutParams(tests);
     text2.setText("Right Text");
     text2.setTextSize(18);

     first_lay.addView(right_lay);
官方微信
官方QQ群
31647020