动态添加示例一:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LinearLayout layout2 = new LinearLayout(this);
layout2.setOrientation(LinearLayout.VERTICAL);
Button btn1 = new Button(this);
Button btn2 = new Button(this);
btn1.setText("Button1");
btn2.setText("Button2");
layout2.addView(btn1);
layout2.addView(btn2);
setContentView(layout2);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setTitle("点击button1 ");
Button btn3=new Button(v.getContext());
layout2.addView(btn3);
btn3.setText("Button3");
}
});
}
}
-------------------------------------------------------------------------------------------------------------------------------------
动态添加示例二:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ImageView添加到这个RelativeLayout下边
RelativeLayout rl_1 = (RelativeLayout)findViewById(R.id.rl_1);
//要被添加的ImageView
ImageView iv_1 = new ImageView(this);
iv_1.setImageResource(R.drawable.ic_launcher);
// iv.setId(110);//注意这点 设置id 特别注意的是设置id(imgApple2.setId(110);),方便在以后onclick监听事件中进行对应的处理
iv_1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "you clicked me", 0).show();
}
});
//添加时的布局参数设置
RelativeLayout.LayoutParams layoutParams_1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// layoutParams_1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
// layoutParams_1.setMargins(30, 50, 100, 100);
layoutParams_1.leftMargin=500;
layoutParams_1.topMargin = 500;
rl_1.addView(iv_1,layoutParams_1);
//ImageView添加到这个RelativeLayout下边
RelativeLayout rl_2 = (RelativeLayout)findViewById(R.id.rl_2);
//要被添加的ImageView
ImageView iv_2 = new ImageView(this);
iv_2.setImageResource(R.drawable.ic_launcher);
// iv.setId(110);//注意这点 设置id
iv_2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "you clicked me", 0).show();
}
});
//添加时的布局参数设置
RelativeLayout.LayoutParams layoutParams_2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// layoutParams_2.setMargins(30, 50, 100, 100);
layoutParams_2.leftMargin=40;
layoutParams_2.topMargin = 400;
rl_2.addView(iv_2,layoutParams_2);
}
}
--------------------------------------------------------------------------------------------------------------------------------------
动态添加示例三:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置布局
setContentView(R.layout.activity_main);
//自定义的带提示文本的EditText组件
EditText ed = new EditText(this);
ed.setHint("请输入。。。。。。。。");
//设置从左边140px处开始输入文本
ed.setPadding(140, 0, 0, 0);
//向如下relativeLayout这个布局中动态添加一个EditText
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rl_2);
//创建一个LayoutParams对象
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//设置android:layout_below属性的值
layoutParams.addRule(RelativeLayout.BELOW, R.id.et_inner);
//动态添加EditText
relativeLayout.addView(ed, layoutParams);
}
}
--------------------------------------------------------------------------------------------------------------------------------------
动态添加示例(综合《很好》):
public class MainActivity extends Activity {
private static int id = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout lin = (LinearLayout) findViewById(R.id.list_Lin);
LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout newSingleRL=new RelativeLayout(this);
for(int i=0;i<10;)
{
newSingleRL=generateSingleLayout(id,"第"+(++i)+"个动态列表");
lin.addView(newSingleRL,LP_FW);//全部用父结点的布局参数
}
// final LinearLayout root = (LinearLayout) findViewById(R.id.layout_root); //获取总根结点
// setContentView(root); //这里必须是总根结点
}
/**
* 新建一个列表item
* @param imageID 新建imageView的ID值
* @param str TextView要显示的文字
* @return 新建的单项布局变量
*/
private RelativeLayout generateSingleLayout(int imageID,String str)
{
//最外层相对布局:其他(一个LinearLayout,一个ImageView)都会添加在此布局内部。
RelativeLayout layout_root_relative=new RelativeLayout(this);
//ImageView被添加在最外层layout_root_relative的右边:params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ImageView imageView = new ImageView(this);
//out.1
RelativeLayout.LayoutParams RL_WW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imageView.setPadding(5, 5, 5, 5);
RL_WW.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageView.setLayoutParams(RL_WW);
imageView.setClickable(true);
imageView.setId(imageID);
imageView.setImageResource(R.drawable.plus);
layout_root_relative.addView(imageView);
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//LinearLayout被添加在最外层layout_root_relative里边,并且在ImageView的左边:params.addRule(RelativeLayout.LEFT_OF,imageID);
LinearLayout layout_sub_Lin=new LinearLayout(this);
layout_sub_Lin.setBackgroundColor(Color.argb(0xff, 0x00, 0xff, 0x00));
layout_sub_Lin.setOrientation(LinearLayout.VERTICAL);
layout_sub_Lin.setPadding(5, 5, 5, 5);
//TextView是被添加在最外层layout_root_relative内部的LinearLayout里边
TextView tv = new TextView(this);
//inner.1
LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tv.setText(str);
tv.setTextColor(Color.argb(0xff, 0x00, 0x00, 0x00));
tv.setTextSize(20);
tv.setLayoutParams(LP_WW);
layout_sub_Lin.addView(tv);
//out.2
RelativeLayout.LayoutParams RL_MW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);//尤其注意这个位置,用的是父容器的布局参数
RL_MW.setMargins(5, 5, 150, 5);
RL_MW.addRule(RelativeLayout.LEFT_OF,imageID);
layout_root_relative.addView(layout_sub_Lin,RL_MW);
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
return layout_root_relative;
}
}
--------------------------------------------------------------------------------------------------------------------------------------
动态添加:尺码,颜色,容量,可以使用这里(很好)
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
// int height = dm.heightPixels;
// 自定义layout组件
RelativeLayout layout = new RelativeLayout(this);
// 这里创建16个按钮,每行放置4个按钮
Button Btn[] = new Button[14];
int j = -1;
for (int i = 0; i < Btn.length; i++) {
Btn[i] = new Button(this);
Btn[i].setId(2000 + i);
Btn[i].setText("按钮" + i);
RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams(
(width - 50) / 4, 100); // 设置按钮的宽度和高度
if (i % 4 == 0) {
j++;
}
btParams.leftMargin = 10 + ((width - 50) / 4 + 10) * (i % 4); // 横坐标定位
btParams.topMargin = 20 + 100 * j; // 纵坐标定位
layout.addView(Btn[i], btParams); // 将按钮放入layout组件
}
this.setContentView(layout);
// 批量设置监听
for (int k = 0; k <= Btn.length - 1; k++) {
// 这里不需要findId,因为创建的时候已经确定哪个按钮对应哪个Id
Btn[k].setTag(k); // 为按钮设置一个标记,来确认是按下了哪一个按钮
Btn[k].setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
int i = (Integer) v.getTag(); // 这里的i不能在外部定义,因为内部类的关系,内部类好多繁琐的东西,要好好研究一番
Toast.makeText(MainActivity.this, "count = " + i,
Toast.LENGTH_SHORT).show();
}
});
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------
一种思路:
通过使用LayoutInflater 每次点击按钮时候去读取布局文件,然后找到布局文件里面的各个VIEW 操作完VIEW 后加载进我们setContentView 方面里面的要放的布局文件里面,
每次动态加载文件必需 调用 removeAllViews方法,清除之前的加载进来的 View 。
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//上边一个线性布局,里边左右各一个按钮,。下边一个线性布局,当点击上边的线性布局里边的左右按钮时,此处的内容改变。
final LayoutInflater inflater = LayoutInflater.from(this);
Button btn = (Button) findViewById(R.id.Button01);
Button btn2 = (Button) findViewById(R.id.Button02);
final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01);//下边的线性布局
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = View.inflate(MainActivity.this, R.layout.listview, null); //listview.xml里边根节点一个线性布局(感觉有点多余),线性布局里边一个listview
LinearLayout ll = (LinearLayout) view.findViewById(R.id.layout);
ListView lv = (ListView) ll.getChildAt(0);
lv.setAdapter(new listAdapter(MainActivity.this));
lin.removeAllViews();
lin.addView(ll);
// LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.listview, null).findViewById(R.id.layout);
// ListView lv = (ListView) layout.getChildAt(0);
// lv.setAdapter(new listAdapter(MainActivity.this));
// lin.removeAllViews();
// lin.addView(layout);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout layout = (LinearLayout) inflater.inflate(
R.layout.hello, null).findViewById(R.id.hellolayout); //hello.xml里边根节点是一个线性布局(感觉有点多余),线性布局里边一个textview
TextView lv = (TextView) layout.getChildAt(0);
lv.setTextColor(Color.RED);
lin.removeAllViews();
lin.addView(layout);
}
});
}
}