android_demo之生成颜色布局
前面学习了动态生成表格,不单单是要动态生成控件,也同时生成一个事件。
接下来用个小小栗子去了解这个知识点。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="请输入行:"
/> <EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入数字!"
android:numeric="decimal" /> </LinearLayout> <LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="请输入列:"
/> <EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入数字!"
android:numeric="decimal"> <requestFocus />
</EditText> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="一键自动生成表格"
/> </LinearLayout> </LinearLayout> <TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/table1"> </TableLayout> </LinearLayout>
package com.example.dynamictable; import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
private final int MP = ViewGroup.LayoutParams.MATCH_PARENT;
private EditText row;
private EditText column;
private Button bt1;
private TableLayout tableLayout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件Button
bt1=(Button) findViewById(R.id.button1);
//获取文本输入框控件
row=(EditText) findViewById(R.id.editText1);
column=(EditText) findViewById(R.id.editText2); //给button按钮绑定单击事件
bt1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
//Color[] colorBlocks= new Color[4096];
//int row_int=Integer.parseInt(row.getText().toString());
//int col_int=Integer.parseInt(column.getText().toString()); //获取控件tableLayout
tableLayout = (TableLayout)findViewById(R.id.table1);
//清除表格所有行
tableLayout.removeAllViews();
//全部列自动填充空白处
tableLayout.setStretchAllColumns(true);
//生成X行,Y列的表格 int theLength = 64; for(int i=0;i<theLength;i++)
{
TableRow tableRow=new TableRow(MainActivity.this);
//生成颜色
for(int j=0;j<theLength;j++)
{
int result = i*theLength +(j+1)-1;//0~4095
int red = result / 256 ;
int green = (result-red*256) / 16;
int blue = result-red*256 - green*16; //tv用于显示
TextView tv=new TextView(MainActivity.this);
//Button bt=new Button(MainActivity.this); tv.setText("-");
tv.setBackgroundColor( Color.rgb(red*16, green*16, blue*16) ); //Color.rgb(red*16-1, green*16-1, blue*16-1)
//tv.setBackgroundResource(35434);
tableRow.addView(tv);
}
//新建的TableRow添加到TableLayout tableLayout.addView(tableRow, new TableLayout.LayoutParams(MP, WC,1));
} }
}); } }
运行结果:

注意运行的时候会比较慢,当你一直看见那个页面不动,不要激动,请耐心等待,好事多磨。你懂的。
由于考虑到电脑的因素我们就不循环全部颜色出来,只取其中的一部分。看见上面的颜色表,只是一部分而已,

如果你想检测你的机器好不好,不妨试试255做出整个颜色表

相信那会你会想崩溃,因为你的电脑或是手机都好难hold 住。
android_demo之生成颜色布局的更多相关文章
- android_demo 之生成颜色
老师说循环出颜色数字 然后显示出来 他说的什么一脸懵逼(=@__@=) 代码还在手上也还是懵逼 (づ。◕‿‿◕。)づ 不管了 留个脚印在这 以后想起来 至少也知道 直接上代码吧 说 ...
- Android 生成颜色器
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...
- LIRe 源代码分析 7:算法类[以颜色布局为例]
===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...
- LIRe 源代码分析 6:检索(ImageSearcher)[以颜色布局为例]
===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...
- LIRe 源代码分析 5:提取特征向量[以颜色布局为例]
===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...
- LIRe 源代码分析 4:建立索引(DocumentBuilder)[以颜色布局为例]
===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...
- JavaScript随机生成颜色以及十六进制颜色 与RGB颜色值的相互转换
/** * 随机生成颜色 * @return 随机生成的十六进制颜色 */ function randomColor(){ var colorStr=Math.floor(Math.random()* ...
- Android_demo之生成二维码
今天我们来学习一个自动生成二维码 的写法.我们经常能见到各种二维码,比如公众号的二维码,网址的,加好友的,支付的二维码等等.其实每一个二维码只是利用图片的形式展示出来的,实际是一些字符串.而这个字符串 ...
- Thymeleaf利用layout.html文件生成页面布局框架
1.layout.html文件 生成布局 <!DOCTYPE html> <html lang="zh-CN" xmlns:th="http://www ...
随机推荐
- 用git管理自己读的书
福昕阅读器,可以方便地做书签和备注.使用git去管理图书和示例代码,一方面能够很好地保存这些资料,方便查找:另外一方面,无论在家,还是在公司,都能很好地同步自己学习的进度. 福昕阅读器5(视图--&g ...
- SQL优化注意事项
sql语句优化 性能不理想的系统中除了一部分是因为应用程序的负载确实超过了服务器的实际处理能力外,更多的是因为系统存在大量的SQL语句需要优化. 为了获得稳定的执行性能,SQL语句越简单越好.对复杂的 ...
- delphi 文件删除,复制
这是个相对简单的方法,不知道能不能帮到你删除:procedure TForm1.Button1Click(Sender: TObject);begin deletefile('c:\123.txt') ...
- wcf序列化大对象时报错:读取 XML 数据时,超出最大
错误为: 访问服务异常:格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出 错: request.InnerException 消息是“反序 ...
- the import javax.servlet
问题: 在eclipse中编写servlet时出现“the import javax.servlet unresolved” 问题原因: 缺少servlet-api.jar架包. 问题解决办法: 在e ...
- Lucene学习之一:使用lucene为数据库表创建索引,并按关键字查询
最近项目中要用到模糊查询,开始研究lucene,期间走了好多弯路,总算实现了一个简单的demo. 使用的lucene jar包是3.6版本. 一:建立数据库表,并加上测试数据.数据库表:UserInf ...
- Momo自定义DialogFragment
在Fragnment弹窗提示 XML <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- GitHub 上排名前 100 的 Objective-C 项目简介
主要对当前 GitHub 排名前 100 的项目做一个简单的简介, 方便初学者快速了解到当前 Objective-C 在 GitHub 的情况. 项目名称 项目信息 1. AFNetworking ...
- 慕课网-Java入门第一季-6-10 练习题
来源:http://www.imooc.com/ceping/1596 以下关于二维数组的定义和访问正确的是() A int[ ][ ] num = new int[ ][ ]; B int[ ][ ...
- java面向对象基础
1.对象:用来描述客观事物的一个实体,由一组属性和方法构成. 属性:对象具有的各种特征.(成员变量) 方法:对象执行的操作. 2.类:一组具有相同属性和方法的对象的一个归纳类型.对象是类的一个具体表现 ...