尝试这个。根据您的需要定制。
主要活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("VALUE", 2); //Dynamic value
startActivity(i);
}
}SecondActivity
public class SecondActivity extends Activity {
LinearLayout LLMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
LLMain = (LinearLayout)findViewById(R.id.Linearroot);
Intent intent = getIntent();
int Value = intent.getIntExtra("VALUE", 0);
for(int i = 0;i < Value;i++)
{
CreateLayout(i);
}
}
public void CreateLayout(int val)
{
LinearLayout LLTop = new LinearLayout(this);
LLTop.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout LLBtm = new LinearLayout(this);
LLBtm.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramT,paramB,param1,param2,param3,param4,param5,param6;
paramT = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
paramT.setMargins(20, 10, 20, 10);
paramB = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
paramB.setMargins(20, 10, 20, 10);
param1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
param2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
param2.gravity = Gravity.CENTER_VERTICAL;
param3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
param4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
param5 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
param6 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ImageView iv = new ImageView(this);
iv.setBackgroundResource(R.drawable.ic_launcher);
TextView tv = new TextView(this);
tv.setText("Student" + val);
TextView tv1 = new TextView(this);
tv1.setText("Phone");
TextView tv2 = new TextView(this);
tv2.setText("Email");
EditText et = new EditText(this);
EditText et1 = new EditText(this);
LLTop.addView(iv, param1);
LLTop.addView(tv, param2);
LLBtm.addView(tv1, param3);
LLBtm.addView(et, param4);
LLBtm.addView(tv2,param5);
LLBtm.addView(et1,param6);
LLMain.addView(LLTop, paramT);
LLMain.addView(LLBtm,paramB);
}
}second.xml
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Linearroot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondActivity" >
希望能帮助到你。!