package bvb.de.openadbwireless.circle;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.List; public class AddViewByJava extends Activity { private Context context = this;
private RelativeLayout rl_root;
private TextView tv; @TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 最外层的控件 RelativeLayout
rl_root = new RelativeLayout(context); // 添加一个TextView
tv = new TextView(context);
RelativeLayout.LayoutParams tvLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// 设置 margin
tvLayoutParams.leftMargin = 50;
// 设置 padding
tv.setPadding(10, 10, 10, 10);
tv.setText("textView");
tv.setLayoutParams(tvLayoutParams);
rl_root.addView(tv); //添加一个按钮
Button btn = new Button(context);
btn.setText("button");
// 设置规则 : 在TextView 的下方
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, tv.getId());
btn.setLayoutParams(layoutParams);
rl_root.addView(btn);
btn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
tv.setText(getChild());
}
}); // 在Button 下面添加一个LinearLayout,里面水平放4个Button
LinearLayout ll = new LinearLayout(context);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams1.addRule(RelativeLayout.BELOW, btn.getId());
ll.setLayoutParams(layoutParams1);
ll.setBackgroundColor(Color.GREEN);
ll.setPadding(10, 50, 0, 0);
rl_root.addView(ll); ll.setOrientation(LinearLayout.HORIZONTAL);
int btnCount = 4;
for (int i = 0; i < btnCount; i++) {
final Button button = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT));
params.weight = 1;
button.setText("button" + i);
ll.addView(button, params);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(context, button.getText(), Toast.LENGTH_SHORT).show();
}
});
} ListView lv = new ListView(context);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams2.addRule(RelativeLayout.BELOW, ll.getId());
lv.setLayoutParams(layoutParams2);
rl_root.addView(lv);
List<String> datas = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
datas.add("name" + i);
}
lv.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, android.R.id.text1, datas)); setContentView(rl_root);
} public String getChild() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < rl_root.getChildCount(); i++) {
sb.append(rl_root.getChildAt(i).toString()).append("\n");
}
return sb.toString();
} }

android 中通过代码创建控件的更多相关文章

  1. swift 字符转为类,代码创建控件

    在使用类之前要先获得 命名空间 通过json来获取 字符型的类名 然后创建类对象,这时候就要用到字符转类 // 从info字典中获取到 命名空间 转为字符型 let NS = NSBundle.mai ...

  2. android中一个评分的控件

    RatingBar android中一个评分的控件 如何使用 Android Studio下: dependencies { compile 'com.hedgehog.ratingbar:app:1 ...

  3. Android中的自定义视图控件

    简介 当现有控件不能满足需求时,就需要自定义控件. 自定义控件属性 自定义控件首先要继承自View,重写两个构造函数. 第一个是代码中使用的: public MyRect(Context contex ...

  4. Android中使用shape来定义控件

    本文章转接于:http://kofi1122.blog.51cto.com/2815761/521605 Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对s ...

  5. android中的五大布局(控件的容器,可以放button等控件)

    一.android中五大布局相当于是容器,这些容器里可以放控件也可以放另一个容器,子控件和布局都需要制定属性. 1.相对布局:RelativeLayout @1控件默认堆叠排列,需要制定控件的相对位置 ...

  6. WPF中通过代码设置控件的坐标

    用WPF做贪吃蛇小游戏时,发现了一个问题: 贪吃蛇的移动,我是通过不断刷新Rectangle来实现(贪吃蛇的身体由一组Rectangle组成),因此需要不断调整Rectangle的坐标,但是WPF中没 ...

  7. android中动态修改ImageView控件的宽高度

    本例实现了动态修改ImageView控件的宽高度,有两个按钮,一个按钮实现放大image,一个按钮实现缩小image activity_main.xml <?xml version=" ...

  8. android中关于时间的控件

    1.日期选择器 <DatePicker android:layout_width="wrap_content" android:layout_height="wra ...

  9. Android开发之动态添加控件

    动态添加TextView控件: 一:创建一个Android project项目 activity_main.xml文件: 1.用两个LinearLayout布局分别包裹一对TextView,EditT ...

随机推荐

  1. Eclipse常用设置(转)

    http://jingyan.baidu.com/article/3065b3b6efa9d7becff8a4c6.html 用惯了VS,再回过去用Eclipse真是一件痛苦的事.so,在这里记录下使 ...

  2. 安装nodejs+ionic+cordova环境心得

    1.安装node-v0.10.38-x64.msi版本(从nodejs中下载最稳定版本,最后安装ionic安不上,然后又安装node-v0.10.38-x64.msi,再安装ionicok了.不知道是 ...

  3. iOS 开发知识小集(1)

    iOS 开发知识小集(1) 2015-05-15  iOS大全 (点击上方蓝字,快速关注我们) 一直想做这样一个小册子,来记录自己平时开发.阅读博客.看书.代码分析和与人交流中遇到的各种问题.之前有过 ...

  4. 第十三篇 Integration Services:SSIS变量

    本篇文章是Integration Services系列的第十三篇,详细内容请参考原文. 简介在前一篇我们结合了之前所学的冒泡.日志记录.父子模式创建一个自定义的SSIS包日志记录模式.在这一篇,我们将 ...

  5. UserMapper.selectByPrimaryKey-Inline 报错的解决办法

    报错原因,及解决办法: 实体类id属性上少加了@Id 然后mybatis不知道哪个是主键了 我写了个demo public interface UserMapper extends Mapper< ...

  6. .NET Framework 4 中的并行编程9---线程安全集合类

    原文转载自:http://www.cnblogs.com/xray2005/archive/2011/10/11/2206745.html 在.Net 4中,新增System.Collections. ...

  7. Excel将秒转换成标准的时间格式HH:MM:SS

    Excel将秒转换成标准的时间格式HH:MM:SS 比如120秒,转换成00:02:00 Excel公式为: =TEXT(A1/86400,"[hh]:mm:ss") A1为秒数据 ...

  8. JAX-WS(二)之使用wsimport创建WebService客户端

    客户端开发的通常过程是从已有的WSDL处罚,创建辅助类JAXB对象和Service代理类,然后基于这些类开发自己的客户端应用. 开发步骤: 创建eclipse项目: 运行wsimport命令生成客户端 ...

  9. Velocity(3)——字面值和转义

    货币符号: $是美元的符号,在文本中出现的"$2.5"这样的字符串,是不会被Velocity解释为一个变量或者一个属性的,因为2.5或者2或者5都不是一个VTL标识符. 转义: 假 ...

  10. https协议