dynamic_view.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/ll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>

<Button
android:id="@+id/btn_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ll_layout"
android:layout_marginTop="10dp"
android:text="添加" />
</LinearLayout>
</ScrollView>

</LinearLayout>



Activity:

package com.example.jsapp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DynamicActivity extends Activity {

private LinearLayout llLayout;
private Button btnClick;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamic_view);

setView();
}

private void setView() {

llLayout = (LinearLayout) findViewById(R.id.ll_layout);
btnClick = (Button) findViewById(R.id.btn_click);
llLayout.setOrientation(LinearLayout.HORIZONTAL);
btnClick.setOnClickListener(new OnClickListener() {
int count = 0;
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//第一个参数为xml文件中view的id,第二个参数为此view的父组件,可以为null,android会自动寻找它是否拥有父组件
View view = inflater.inflate(R.layout.item, null);
TextView tvCaiLiao = (TextView) view.findViewById(R.id.tv_cai_liao);
TextView tvShuLiang = (TextView) view.findViewById(R.id.tv_shu_liang);

tvCaiLiao.setText("芹菜" + count);
tvShuLiang.setText("50g" + count);
llLayout.setOrientation(LinearLayout.VERTICAL);
llLayout.addView(view);
count++;
}
});
}

}

更多相关文章

  1. Android界面编程——Android布局组件(二)
  2. Android中Activity组件详解
  3. Android应用程序的基本组件之Activity
  4. Android百度地图(一):百度地图定位sdk 类方法参数、定位原理详细
  5. 在ListItem某组件添加响应事件

随机推荐

  1. android提示:Gradle DSL method not found
  2. Android面试系列文章2018之Android部分We
  3. Android(安卓)面试题总结之Android(安卓)
  4. Android(安卓)的Margin和Padding属性以及
  5. Android——文件存储
  6. 修改android框架代码
  7. 【安卓学习之开发工具】 Android(安卓)St
  8. 《精通Android(安卓)2》书评
  9. Android(安卓)Studio 工程视图&项目构成
  10. android2.x中android:layout_marginRight