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. A股市场各行业龙头股一览表

    A股市场各行业龙头股一览表 一.指标股: 工商银行.中国银行.中国石化.中国国航.宝钢股份. 中国神华.建设银行.招商银行.华能国际.中国联通. 长江电力.中国人寿.中国石油 二.金融: 招商银行.浦 ...

  2. jQuery选择器总结(转)

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  3. vs2010制作带卸载功能的安装包过程,及asp.net网站iis发布、备份数据库

    1:新建安装部署项目 打开VS编辑器,点击新建项目,选择:其他项目类型->安装与部署->安装项目,然后点击确定. 2 安装向导完成后即可进入项目文件夹: 双击"应用程序文件夹&q ...

  4. Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)

    <bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...

  5. HashMap put,get操作

    HashMap中的put方法 public V put(K key, V value) { //当key为null,调用putForNullKey方法,保存null与table第一个位置中,这是Has ...

  6. javaScript学习之ajax

    一.xmlHttpRequest对象的创建 ajax的核心的XMLHttpRequest对象,下面的代码给出了兼容各个浏览器的方法实现 function createXHR(){ if(typeof ...

  7. ASP: VS2015 + IIS Express 10 学习笔记

    首先搞清楚 ASP 与 ASP.NET 的区别(.asp与.aspx). https://msdn.microsoft.com/en-us/library/ms973813.aspx 环境配置: ht ...

  8. iOS 快递查询

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  9. snapshot standby database

    快照备库接收和归档主库发送来的redo,但是不会应用:切换成physical standby之后会自动开启redo apply.快照standby不可以参加主备切换:在最大保护性模式下,如果只有一个备 ...

  10. 定制对ArrayList的sort方法的自定义排序

    java中的ArrayList需要通过collections类的sort方法来进行排序 如果想自定义排序方式则需要有类来实现Comparator接口并重写compare方法 调用sort方法时将Arr ...