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 ...
随机推荐
- [logstash-input-file]插件使用详解(转)
最小化的配置文件 在Logstash中可以在 input{} 里面添加file配置,默认的最小化配置如下: 1 2 3 4 5 6 7 8 9 10 11 input { file ...
- Android 用Animation-list实现逐帧动画
第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog.csdn.net/aminfo/article/details/7847761 图片素材: 文件名称: ic ...
- android AlarmManager 详解
在开发互联网应用时候,我们常常要使用心跳来保证客户端与服务器的连接.怎么完成心跳很关键,在说道客户端心跳功能时,如果使用Timer或者专门开起一个线程来做心跳的工作,会浪费CPU工作时间,而且也会更多 ...
- Android --slidedatetimepicker时间控件应用
下载参考SlideDateTimePicker时间选择器 1.创建 beginTimeTxt=(EditText)findViewById(R.id.search_begintime_edittext ...
- 面向对象编程(七)——Static关键字
Static变量
- ios app下载跳到itunes
<body class="box"> <div class="text"> <a href="https://itune ...
- sql server常见服务
根据您决定安装的组件,SQL Server 安装程序将安装以下服务: SQL Server Database Services - 用于 SQL Server 关系数据库引擎的服务. 可执行文件为 & ...
- Swift实战-小QQ(第1章):QQ登录界面
1.新建小QQ项目 2.将所需用到的图片资源(resource)文件夹,添加到项目中.并新建一个登录页面:LoginViewController.swift 3.修改LoginViewControll ...
- 某个点到其他点的曼哈顿距离之和最小(HDU4311)
Meeting point-1 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- ServiceController1
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...