Android在布局文件指定位置动态增加删除布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none" >

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

        <TableLayout
            android:id="@+id/dictTable"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="1" >


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

                <TextView
                    style="@style/insure_message"
                    android:text="被保险人信息" />
                
                <!-- 保险人姓名et_insurer_name -->

                <EditText
                    android:id="@+id/et_insurer_name"
                    style="@style/fill_out_insure_message"
                    android:text="红绿蓝" />

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:paddingBottom="@dimen/activity_insurance_information"
                    android:paddingLeft="@dimen/activity_insurance_information"
                    android:paddingRight="@dimen/activity_insurance_information"
                    android:paddingTop="@dimen/activity_insurance_information" >

                    <!-- 身份证 ib_identity_card -->

                    <ImageButton
                        android:id="@+id/ib_identity_card"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_gravity="center_vertical"
                        android:background="@drawable/check_in_round_s"
                        android:paddingLeft="10dp" />

                    <TextView
                        style="@style/insure_message"
                        android:text="身份证" />

                    <!-- 护照 ib_passport -->

                    <ImageButton
                        android:id="@+id/ib_passport"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="20dp"
                        android:background="@drawable/check_in_round_s"
                        android:paddingLeft="10dp" />

                    <TextView
                        style="@style/insure_message"
                        android:text="护照" />
                </LinearLayout>

                <!-- 身份证 et_identity_card_id -->

                <EditText
                    android:id="@+id/et_identity_card_id"
                    style="@style/fill_out_insure_message"
                    android:text="@string/insure_identity_card" />

                <!-- 护照et_passport_id -->

                <EditText
                    android:id="@+id/et_passport_id"
                    style="@style/fill_out_insure_message"
                    android:text="@string/insure_passport" />
                <!-- 邮箱 et_e_mail -->

                <EditText
                    android:id="@+id/et_e_mail"
                    style="@style/fill_out_insure_message"
                    android:text="@string/insure_e_mail" />
                <!-- 提交申请 bt_submit_applications -->
            </LinearLayout>
        </TableLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnAdd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text=" 添 加 " />
            
             <Button
                    android:id="@+id/btn_delete"
                    android:text="删除"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>
package tgb.lk.tablelayout;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RemoteViews.RemoteView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 实现功能: 1.使用TableLayout动态布局展示,可动态添加和删除. 2.初始化时显示动态展示,初始化的数据改造后可来自数据库.
 * 3.重置时到初始化状态. 4.保存时去重检查,参见代码中去重算法.
 * 
 * @author likun date: 2012-04-07
 */
public class MainActivity extends Activity {
	private Button btnAdd = null;
	private LinearLayout layout;
	private LinearLayout layout2;

	private Button btn_delete;

	private int entity_id = 0;
	private String entity_name = "";

	private DictDaoImpl dao = null;
	private TableLayout table = null;
	private int orders = 0; // 用户记录最大的orders值

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

		// 实例化按钮并设置监听器.
		btnAdd = (Button) findViewById(R.id.btnAdd);
		btn_delete = (Button) findViewById(R.id.btn_delete);

		btnAdd.setOnClickListener(listener);
		btn_delete.setOnClickListener(listener);

		entity_id = 1; // intent.getIntExtra(CrmConstances.ID, 0);
		entity_name = "区域";// intent.getStringExtra(CrmConstances.NAME);

		table = (TableLayout) findViewById(R.id.dictTable);

		appendNewRow(table);
	}

	OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.btnAdd:
				appendNewRow(table);
				break;

			case R.id.btn_delete:
				table.removeViewAt(table.getChildCount()-1);
				table.removeViewAt(table.getChildCount()-1);
				break;
			default:
				break;
			}

		}
	};

	

	public void toastShow(String text) {
		Toast.makeText(this, text, 1000).show();
	}

	private void appendNewRow(final TableLayout table) {
		TableRow row = new TableRow(this);
		row.setId(0);
		orders++;

		TableRow row1 = new TableRow(this);
		TextView text1 = new TextView(this);
		text1.setText("被保险人信息");
		text1.setVisibility(View.VISIBLE);
		row1.addView(text1);

		EditText et = new EditText(this);
		et.setText("红绿蓝");
		et.setVisibility(View.VISIBLE);
		TableRow row2 = new TableRow(this);
		row2.addView(et);

		TableRow row5 = new TableRow(this);	
		
		ImageView image = new ImageView(this);
		image.setBackgroundResource(R.drawable.check_in_round_s);
		image.setVisibility(View.VISIBLE);
/*		image.setX(15);
		image.setY(15);*/
		
		EditText dictName = new EditText(this);
		dictName.setText("");
		dictName.setHint("请输入业务字典名称");
		dictName.setPadding(3, 3, 3, 3);
		dictName.setWidth(190);
		dictName.setGravity(Gravity.CENTER);
		row5.addView(image);
		row5.addView(dictName);

		TableRow row3 = new TableRow(this);
		EditText order = new EditText(this);
		order.setText(String.valueOf(orders));
		order.setPadding(3, 3, 3, 3);
		order.setWidth(40);
		order.setGravity(Gravity.CENTER);
	
		
		row3.addView(order);
		
		
		EditText et1 = new EditText(this);
		et.setText("红绿蓝");
		et.setVisibility(View.VISIBLE);
		TableRow row6 = new TableRow(this);
		row6.addView(et1);

		EditText et2 = new EditText(this);
		et.setText("红绿蓝");
		et.setVisibility(View.VISIBLE);
		TableRow row7 = new TableRow(this);
		row7.addView(et2);

		EditText et3 = new EditText(this);
		et.setText("红绿蓝");
		et.setVisibility(View.VISIBLE);
		TableRow row8 = new TableRow(this);
		row8.addView(et3);

		Button oper = new Button(this);
		oper.setText("删除");
		oper.setPadding(3, 3, 3, 3);
		order.setWidth(60);
		oper.setGravity(Gravity.CENTER);
		oper.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//table.removeView(table.findViewById(0));
			}
		});

		layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.addView(row1);
		layout.addView(row2);
		layout.addView(row3);
		layout.addView(row5);
		layout.addView(row6);
		layout.addView(row7);
		layout.addView(row8);
		layout.addView(oper);
		
		EditText et4 = new EditText(this);
		et4.setText("红绿蓝");
		et4.setVisibility(View.VISIBLE);
		EditText et5 = new EditText(this);
		et5.setText("红绿蓝ssss");
		et5.setPadding(10, 0, 0, 0);
		et5.setVisibility(View.VISIBLE);

		layout2 = new LinearLayout(this);
		layout2.setOrientation(LinearLayout.HORIZONTAL);
		layout2.addView(et4);
		layout2.addView(et5);
		table.addView(layout);
		table.addView(layout2);
		;

	}

}


你可能感兴趣的:(Android在布局文件指定位置动态增加删除布局)