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精进之路-04】Android核心组件Activity(Activity的生
  2. android intent——小白入口
  3. Android(安卓)基础知识点(持续更新)
  4. Android笔试总结
  5. android的listView组件
  6. Android界面编程——Android布局组件(二)
  7. android Intents和Intent Filters - 开发文档翻译 - 1
  8. android Intent机制详解
  9. Android(安卓)第七课——UI布局

随机推荐

  1. C语言中数组元素的下标下限是什么
  2. c语言如何实现做界面
  3. c语言return的用法是什么?
  4. “->” 在 C 语言什么意思?
  5. C 语言结构体详解
  6. c语言break和continue用法
  7. C语言--x和x--的区别
  8. c语言中fun用法详解
  9. c++中string类的常用方法有哪些
  10. C++ 引用和指针区别