android 中通过代码创建控件
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 中通过代码创建控件的更多相关文章
- swift 字符转为类,代码创建控件
在使用类之前要先获得 命名空间 通过json来获取 字符型的类名 然后创建类对象,这时候就要用到字符转类 // 从info字典中获取到 命名空间 转为字符型 let NS = NSBundle.mai ...
- android中一个评分的控件
RatingBar android中一个评分的控件 如何使用 Android Studio下: dependencies { compile 'com.hedgehog.ratingbar:app:1 ...
- Android中的自定义视图控件
简介 当现有控件不能满足需求时,就需要自定义控件. 自定义控件属性 自定义控件首先要继承自View,重写两个构造函数. 第一个是代码中使用的: public MyRect(Context contex ...
- Android中使用shape来定义控件
本文章转接于:http://kofi1122.blog.51cto.com/2815761/521605 Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对s ...
- android中的五大布局(控件的容器,可以放button等控件)
一.android中五大布局相当于是容器,这些容器里可以放控件也可以放另一个容器,子控件和布局都需要制定属性. 1.相对布局:RelativeLayout @1控件默认堆叠排列,需要制定控件的相对位置 ...
- WPF中通过代码设置控件的坐标
用WPF做贪吃蛇小游戏时,发现了一个问题: 贪吃蛇的移动,我是通过不断刷新Rectangle来实现(贪吃蛇的身体由一组Rectangle组成),因此需要不断调整Rectangle的坐标,但是WPF中没 ...
- android中动态修改ImageView控件的宽高度
本例实现了动态修改ImageView控件的宽高度,有两个按钮,一个按钮实现放大image,一个按钮实现缩小image activity_main.xml <?xml version=" ...
- android中关于时间的控件
1.日期选择器 <DatePicker android:layout_width="wrap_content" android:layout_height="wra ...
- Android开发之动态添加控件
动态添加TextView控件: 一:创建一个Android project项目 activity_main.xml文件: 1.用两个LinearLayout布局分别包裹一对TextView,EditT ...
随机推荐
- SqlServer基础:游标
记录下今天用到的游标: DECLARE @TempID INTDECLARE @Number INTSET @Number=1DECLARE myCursor CURSOR FOR SELEC ...
- docker:从 tomcat 容器连接到 mysql 容器
docker 中的容器互联是一个较为复杂的话题,详细内容将在后续章节中介绍. 续前 2 个章节的内容,我们创建了一个 mysql 容器和一个 tomcat 容器,可以使用 「docker ps」来查看 ...
- Visual Studio Online
删除Visual Studio Online的项目http://taslimi.me/how-to-delete-a-team-project-from-tfs-online-tfs.visualst ...
- Android Push Notification实现信息推送使用
本贴在http://www.cnblogs.com/hanyonglu/archive/2012/03/16/2399655.html下略为改动. Apndroid Push Notification ...
- 用 CSS 做轮播图
对于用 css 实现一个轮播图的缘由,是那时候刚开始接触前端,完全还不懂 js.但是有一个项目(就是一个用来应付面试的作品)需要做一个轮播的效果,当时第一反应就是用 css3 自定义动画 -webki ...
- Leetcode: Remove Elements
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- SQL Sever 身份验证 sa用户设置
1.用windows身份验证登陆数据库找到sa用户 2.鼠标右键sa->属性->常规,设置密码. 3.选择状态->登陆选择已启用 4.选中当前数据库 鼠标右键->属性 5.选择 ...
- Python学习总结15:时间模块datetime & time & calendar (二)
二 .datetime模块 1. datetime中常量 1)datetime.MINYEAR,表示datetime所能表示的最小年份,MINYEAR = 1. 2)datetime.MAXYEAR ...
- Java基础(40):Java中的集合介绍---Collection与Map
集合类说明及区别Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap ...
- 每天一个java基础知识--static
内存总体一共分为了 4个部分(stack segment.heap segment.code segment.data segment) 当我们在程序中,申明一个局部变量的时候,此变量就存放在了 st ...