求解:android中如何实现动态插入控件

求解:android中如何实现动态插入控件
我想在原有的相对布局中,通过点击一个按钮,实现在按钮下方插入一个控件,并且原来按钮下方的布局顺延往下一行。

以下是我的代码,麻烦懂的朋友帮忙填一下。我java初学。希望尽量用代码的方式来解释,文字再多我可能也看不懂。先谢谢了

package com.demo.android.text;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class Text extends Activity {
private ImageButton plus_button;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);

findViews();
setListener();
}
private void setListener() {
// TODO Auto-generated method stub
plus_button.setOnClickListener(plus);
}
private void findViews() {
// TODO Auto-generated method stub
plus_button = (ImageButton)findViewById(R.id.btn_add);
}
private OnClickListener plus = new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}
};
}
直接给你上代码吧,写了我半个小时,经过了我的测试了的~

运行岩悄下就能看到结果了~关键的remove的时候有给你写注伏枣帆释~

布局的layout文件内容:
----------------------------------------------------------------------------------

android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/linearlayout">

android:layout_width="wrap_content" android:layout_height="wrap_content">



android:layout_width="fill_parent" android:gravity="center"
android:layout_height="50px">


<缺雹/LinearLayout>
----------------------------------------------------------------------------------

对应Activity的内容:
----------------------------------------------------------------------------------
package com.foxconn.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

public class DialogTest extends Activity implements OnClickListener {

private Button add_btn, remove_btn;
private LinearLayout linearLayout;
private int index = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
register();
}

private void register() {
add_btn.setOnClickListener(this);
remove_btn.setOnClickListener(this);
}

private void findViews() {
add_btn = (Button) findViewById(R.id.add);
remove_btn = (Button) findViewById(R.id.remove);
linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
}

protected View createView() {
Button btn = new Button(this);
btn.setId(index++);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn.setText("aaaaaa" + index);
return btn;
}

private void removeView() {
//获取linearlayout子view的个数
int count = linearLayout.getChildCount();
//研究整个LAYOUT布局,第0位的是含add和remove两个button的layout
//第count-1个是那个文字被置中的textview
//因此,在remove的时候,只能操作的是0 //在执行每次remove时,我们从count-2的位置即textview上面的那个控件开始删除~
if (count - 2 > 0) {
//count-2>0用来判断当前linearlayout子view数多于2个,即还有我们点add增加的button
linearLayout.removeViewAt(count - 2);
}
}

public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
linearLayout.addView(createView(), 1);
break;
case R.id.remove:
removeView();
break;
default:
break;
}
}
}
----------------------------------------------------------------------------------
可以如下操作:

1、在一段文字中插入n个标记,比如

2、用string.split("")方法把该文字拆分成String数组,进而把String数组闭胡转换成TextView数组tvs[n+1]

3、蓝色按钮是自定义的控件,共有n个,慧伏用for循环和LayoutInflater方法转换成cvs[n],

4、for循环用前态携addview的方式,按顺序把tvs[n+1]和cvs[n]一个插一个拼接起来形成一个bigview。
不错,好好学习
← 上一篇: 下一篇: →