Android 生成颜色器

<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>
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));
                }

            }
        });

    }  

}

效果呢,博客就不展示了。可以自己试试。黄沙百战穿金甲,不破楼兰终不还

时间: 2024-08-25 11:37:57

Android 生成颜色器的相关文章

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:layo

Android布局管理器-使用LinearLayout实现简单的登录窗口布局

场景 Android布局管理器-从实例入手学习相对布局管理器的使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103838924 线性布局LinearLayout,分为水平和垂直线性布局. 实现效果如下 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将activity修改为LinearLayout

MAC安裝《Genymotion Android模擬器》大玩Android APP (神魔之塔)

链接地址:http://www.minwt.com/mac/10083.html/comment-page-2 MAC» 智慧型裝罝» Android | 2014/02/12 Android是一個開放的平台,因此先前也分享了幾個Android的模擬器,但當初梅干使用Android模擬器,最主要的功能就是用來測試網頁,看網頁在Android手機上是否能正常運作,雖然說這些Android模擬器,也可透過Google Player安裝Android APP,在電腦中就可玩Android APP,但由

JavaScript随机生成颜色的方法

JavaScript随机生成颜色的方法 这篇文章主要介绍了JavaScript随机生成颜色的方法的相关资料,非常不错,代码简单易懂,具有参考借鉴价值,需要的朋友可以参考下 废话不多说了直接给大家贴js代码了,具体代码如下所述: ? 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 37 38 <!DOCTYPE html> <html>

android音乐播放器开发教程

android音乐播放器开发教程 android音乐播放器开发教程,布布扣,bubuko.com

Android多线程文件下载器

本应用实现的是输入文件的网络的地址,点击按钮开始下载,下载过程中有进度条和后面的文本提示进度, 下载过程中按钮不可点击,防止重复的下载,下载完毕后会进行Toast的提示显示, 并且回复按钮的可点击性,进度条也会清空,当然如果下载中途结束应用进程就会进行进度的保存, 下次下载同样的文件时就会从进度记录进行下载,节省流量和时间 应用需要的应用权限: 访问网络权限 <uses-permission android:name="android.permission.INTERNET"/&

Android 基于ijkplayer+Rxjava+Rxandroid+Retrofit2.0+MVP+Material Design的android万能播放器aaa

MDPlayer万能播放器 MDPlayer,基于ijkplayer+Rxjava+Rxandroid+Retrofit2.0+MVP+Material Design的android万能播放器,可以播放本地和在线视频,可以浏览宅男杀手妹纸图片,UI设计遵循 Material Design. GitHub地址:https://github.com/Dawish/MDPlayer UI浏览:         1.UI设计: 列表使用RecyclerView,item为CardView并设置rippl

使用Vitamio打造自己的Android万能播放器

前言 虽然Android已经内置了VideoView组件和MediaPlayer类来支持开发视频播放器,但支持格式.性能等各方面都十分有限,这里与大家一起利用免费的Vitamio来打造属于自己的Android万能播放器! 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 一.实现目标 1.1 支持格式 支持主流的视音频格式:mp3/mp4/mkv/avi/3gp/rmvb/mov

Vitamio打造自己的Android万能播放器

前言 虽然Android已经内置了VideoView组件和MediaPlayer类来支持开发视频播放器,但支持格式.性能等各方面都十分有限,这里与大家一起利用免费的Vitamio来打造属于自己的Android万能播放器! 声明 欢迎转载,但请保留文章原始出处:) 石攻玉 :http://www.cnblogs.com/stone4/ 正文 一.实现目标 1.1 支持格式 支持主流的视音频格式:mp3/mp4/mkv/avi/3gp/rmvb/mov/flv等. 1.2 支持功能 1.2.1 当然