ScrollView动态添加组件

main.xml代码如下:

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

</LinearLayout>
</ScrollView>

.java代码如下:

package org.lxh.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class Hello extends Activity {
	String str[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", };

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		super.setContentView(R.layout.main); // 设置要使用的布局管理器
		LinearLayout linear = (LinearLayout) super.findViewById(R.id.mylinear);//取得组件
		LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);//定义按钮的布局参数
		for(int i=0;i<this.str.length;i++){
			Button btn=new Button(this);//创建按钮组件
			btn.setText(this.str[i]);//设置文本
			linear.addView(btn,param);//增加组件

		}

	}
}

运行如下:

时间: 2024-08-15 22:59:30

ScrollView动态添加组件的相关文章

Android使用addView动态添加组件

在项目开发中,我们经常需要进行动态添加组件,其中可添加的部分有两项:布局和组件 其中,添加的布局主要有RelativeLayout型(相对布局)的和LinearLayout(线性布局) 添加的组件主要有文本显示框,编辑框,按钮等组件. 下面,就让我们来进行实现: 首先我们创建一个新的项目,删除MainActivity.class中没有的代码,仅留下protected void onCreate(Bundle savedInstanceState)函数 往布局文件中添加一个新的组件: 1. add

Angular使用总结 --- 通过指令动态添加组件

之前自己写的公共组件,都是会先引入,需要调起的时候再通过service控制公共组件状态.值.回调函数什么的.但是有一些场景不适合这种方式,还是动态添加组件更加好.通过写过的一个小组件来总结下. 创建组件 场景:鼠标移动到图标上时,展示解释性的说明文字.那就需要创建一个普通的tooltip组件.如下: <aside class="hover-tip-wrapper"> <span>{{tipText}}</span> </aside> HT

Android笔记(六十一)动态添加组件

想要一个功能,点击按钮,可以在已有的布局上,新添加一组组件. 动态的创建组件,本质上还是创建组件,只不过是在程序中根据逻辑来创建.大致步骤是找到要创建控件的位置,然后将要创建的组件添加进去. 看代码: MainActivity.java package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Bu

vue2.0动态添加组件

方法一.<template> <input type="text" v-model='componentName'> <button @click='add'>click me to add a component</button> </template> <script> // 引入要添加的所有组件 import component1 from './components/component1.vue' impo

动态添加组件(XML)

1.利用LayoutInflater的inflate动态加载XMLmLinearLayout = (LinearLayout)findViewById(R.id.LinearLayout_ID);LayoutInflater layoutInflater = LayoutInflater.from(context);View view = layoutInflater.inflate(resource--需要加载的XML, null);XML:resource = R.layout.XML-Na

NGUI ScrollView动态添加Item(不用GameObject.Find()去查找需要动态改变的游戏对象)

借鉴网上的提示,但具体的网址忘记保存了,因此无法放在这里!如需要注明出处,请联系本人. PS:网上说的不够详细,自己敲了下代码实现并整理了一下. 下面我用UITable组件而不是UIGrid来排序Item 将Item做成预设体,如下图中的3:QuestLog(此GO对应官方的例子中的QuestLog) 在预置体QuestLogPrefab中的GO对象QuestLog上挂脚本,如上图中1:脚本命名为QuestLogPrefab(当然类名也一样),然后在脚本里声明你需要动态改变数据的GO,记得需要p

easyui 使用jquery动态添加组件样式问题

可以使用$.parser.parse();这个方法进行处理: 例如: $.parser.parse(); 表示对整个页面重新渲染,渲染完就可以看到easyui原来的样式了: var targetObj = $("<input name='mydate' class='easyui-datebox'>").appendTo("#id"); $.parser.parse(targetObj); 表示重新渲染某个特定的组件.

New UI-Java代码动态添加控件或xml布局

New UI-Java代码动态添加控件或xml布局  --转载请注明出处:coder-pig,欢迎转载,请勿用于商业用途! 小猪Android开发交流群已建立,欢迎大家加入,无论是新手,菜鸟,大神都可以,小猪一个人的 力量毕竟是有限的,写出来的东西肯定会有很多纰漏不足,欢迎大家指出,集思广益,让小猪的博文 更加的详尽,帮到更多的人,O(∩_∩)O谢谢! 小猪Android开发交流群:小猪Android开发交流群群号:421858269 新Android UI实例大全目录:http://blog.

unity3d动态操作组件

利用范型,动态操作组件(添加或删除) e.AddComponent<CubeTranslate> ();//动态添加组件 Destroy (e.GetComponent<CubeTranslate> ());//动态删除组件 其中e为动态生成的对象: public GameObject e;