<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" >
</Button>
<Button
android:id="@+id/remove"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Remove" >
</Button>
</LinearLayout>
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="50px"
android:gravity="center"
android:text="This is textView." >
</TextView>
</LinearLayout>
package com.example.zzr_photodemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class FourActivity extends Activity implements OnClickListener {
private Button add_btn, remove_btn;
private LinearLayout linearLayout;
private int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_four);
add_btn = (Button) findViewById(R.id.add);
remove_btn = (Button) findViewById(R.id.remove);
linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
add_btn.setOnClickListener(this);
remove_btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.four, menu);
return true;
}
protected View createView() {
Button btn = new Button(this);
btn.setId(index++);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
btn.setText("添加按钮" + index);
return btn;
}
private void removeView() {
// 获取linearlayout子view的个数
int count = linearLayout.getChildCount();
// 研究整个LAYOUT布局,第0位的是含add和remove两个button的layout
// 第count-1个是那个文字被置中的textview
// 因此,在remove的时候,只能操作的是0<location<count-1这个范围的
// 在执行每次remove时,我们从count-2的位置即textview上面的那个控件开始删除~
if (count - 2 > 0) {
// count-2>0用来判断当前linearlayout子view数多于2个,即还有我们点add增加的button
linearLayout.removeViewAt(count - 2);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.add:
linearLayout.addView(createView(), 1);
break;
case R.id.remove:
removeView();
break;
default:
break;
}
}
}
动态设置控件的宽高:
①:
try {
linear = (LinearLayout) checkdetais_listview.findViewById(R.id.linear_layout);
Bitmap photo = BitmapFactory.decodeStream(getContentResolver().openInputStream(uritempFile));
LinearLayout linearlayout=new LinearLayout(this);
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.width = dip2px(this, 41);//dp转px
params.height = dip2px(this, 41);//dp转px
params.setMargins(5,0,5,0);
linearlayout.addView(imageView);
imageView.setImageBitmap(photo);
linear.addView(linearlayout,params);
②:
try {
linear = (LinearLayout) checkdetais_listview.findViewById(R.id.linear_layout);
Bitmap photo = BitmapFactory.decodeStream(getContentResolver().openInputStream(uritempFile));
int width = dip2px(this, 41);//dp转px
int height = dip2px(this, 41);//dp转px
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new ViewGroup.LayoutParams(width, height));
imageView.setPadding(5,0,5,0);
imageView.setImageBitmap(photo);
linear.addView(imageView);
/**
* 将dip或dp值转换为px值,保证尺寸大小不变
*/
public int dip2px(Context context, float dipValue) {
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dipValue,context.getResources().getDisplayMetrics());