效果图
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et_name"
android:layout_width="80dip"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et_age"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/et_name" />
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/et_age"
android:text="添加学生" />
<ScrollView // 滚动显示 注意里面只可包裹一个显示对象 即 1个LinearLayout 或者其它。
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/et_name" >
<LinearLayout
android:id="@+id/ll_student_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
实现方法:
package com.itheima27.refreshview;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private EditText etName;
private EditText etAge;
private LinearLayout llStudentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.et_name);
etAge = (EditText) findViewById(R.id.et_age);
llStudentList = (LinearLayout) findViewById(R.id.ll_student_list);
findViewById(R.id.btn_add).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.main, menu);
return true;
}
@Override
public void onClick(View v) {
String name = etName.getText().toString();
String age = etAge.getText().toString();
// 添加到线性布局中一个对象
TextView child = new TextView(this);
child.setText("\t" + name + "\t" + age);
child.setTextSize(23);
llStudentList.addView(child);
}
}