当前位置: 代码迷 >> Android >> 动态添加控件有关问题
  详细解决方案

动态添加控件有关问题

热度:56   发布时间:2016-05-01 21:02:57.0
动态添加控件问题 高手进!!
Java code
 LinearLayout linlayout = (LinearLayout)findViewById(R.id.line);//                     LinearLayout.LayoutParams ll = new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, //                             LinearLayout.LayoutParams.FILL_PARENT);                     RadioGroup rg = new RadioGroup(otherActivity.this);                     RadioButton cate = new RadioButton(otherActivity.this);                     cate.setText("收入");                     RadioButton much = new RadioButton(otherActivity.this);                     much.setText("支出");                     rg.addView(cate);                     rg.addView(much);                     rg.setOrientation(LinearLayout.HORIZONTAL);                     TextView mingmu = new TextView(otherActivity.this);                     mingmu.setText("名目");                     EditText edmingmu = new EditText(otherActivity.this);                     TextView shumu = new TextView(otherActivity.this);                     mingmu.setText("数目");                     EditText edshumu = new EditText(otherActivity.this);                     linlayout.addView(rg);                     linlayout.addView(mingmu);                     linlayout.addView(edmingmu);                     linlayout.addView(shumu);                     linlayout.addView(edshumu);

XML code
<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     >          <DatePicker         android:layout_width="fill_parent"        android:layout_height="wrap_content"        />           <ScrollView         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:id="@+id/sv"         >         <LinearLayout              android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical"            android:id="@+id/line"             >            <RadioGroup                   android:layout_width="fill_parent"                  android:layout_height="wrap_content"                  android:orientation="horizontal"                  android:gravity="center"                >                <RadioButton                     android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="收入"                    android:checked="true"                    android:id="@+id/income"                    />                 <RadioButton                     android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="支出"                    android:id="@+id/outcome"                    />            </RadioGroup>            <TextView                 android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:text="名目:"                />            <EditText                 android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:id="@+id/categry"                />                          <TextView                 android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:text="数目:"                />            <EditText                 android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:id="@+id/howmuch"                />              </LinearLayout>         </ScrollView>                         <LinearLayout                 android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="horizontal">                <Button                     android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="确定"                    android:id="@+id/thirdenter"                    />                <Button                     android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="添加更多"                    android:id="@+id/thirdmore"                    />                            </LinearLayout>                </LinearLayout>
  相关解决方案