了解了5大布局,我们会发现这些布局都是静态的,如何让系统自动生成控件呢?这就需要activity来帮忙了

  今天我们讲的就是用activity新建布局

  用案例来说吧!

  实现一个输入行和列自动生成表格并生成背景颜色

  效果如图

    

  代码如下:

  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
> <EditText android:id="@+id/text_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:ems="10"
android:hint="@string/text_row"
android:inputType="number"> <requestFocus />
</EditText> <EditText
android:id="@+id/text_column"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:ems="10"
android:hint="@string/text_column"
android:inputType="number"
/> <Button
android:id="@+id/btn_commit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="@string/btn_commit"
/>
</LinearLayout> <TableLayout
android:layout_weight="1"
android:id="@+id/tablelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" > </TableLayout> </LinearLayout>
 package com.example.linearlayout;

 import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private TextView text_column;
private TextView text_row ;
private TableLayout tabLay;
private int length = 64;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
//获取控件按钮
Button btn_commit = (Button) findViewById(R.id.btn_commit);

      //获取控件textview
text_column = (TextView) findViewById(R.id.text_column);
text_row= (TextView) findViewById(R.id.text_row); //给按钮设置点击事件
btn_commit.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
                 //获取文本值
String column = text_column.getText().toString().trim();
String row = text_row.getText().toString().trim();
   //判断是否为空
if(column.length() != 0 && row.length() != 0){
//获取控件布局
tabLay= (TableLayout) findViewById(R.id.tablelayout); //先清空
tabLay.removeAllViewsInLayout();
tabLay.setStretchAllColumns(true);
//强转整型
int x =Integer.parseInt(text_column.getText().toString()); //??
int y =Integer.parseInt(text_row.getText().toString());   //for循环嵌套
for(int m = 0 ; m < y; m++ ){ TableRow tr = new TableRow(MainActivity.this); for(int n = 0 ; n < x ; n++ ){ TextView tv = new TextView(MainActivity.this); //tv.setText("("+n+","+m+")");
tv.setText(" ");
tr.addView(tv);
  //获取参数,设置背景颜色值
int result = m*n +2*n-1;
int red = result / 16/16 ;
int green = ( result - red*16*16)/16;
int blue = result - red*16*16 - green*16; tv.setBackgroundColor(Color.rgb(red*16, green*16, blue*16)); }
tabLay.addView(tr); } }else{
Toast.makeText(MainActivity.this,"请输入行和列",1).show();
}
}
}); }
}

    

Android之activity中新建控件的更多相关文章

  1. Android在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom。

    根据业务的需要,要在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom属性. 我们知道在xml中设置的方法为:android:d ...

  2. Android 在OnCreate()中获取控件高度与宽度

    试过在OnCreate()中获取控件高度与宽度的童鞋都知道,getWidth()与getHeight()方法返回是0,具体原因 看一下Activity的生命周期 就会明白. 上代码: 方法一: int ...

  3. Android 在Fragment中修改Activity中的控件

    在当前的Fragment中调用getActivity方法获取依附着的那个Activity,然后再用获取到的Activity去findViewById拿到你需要的控件对其操作就行了.

  4. Android在OnCreate中获取控件的宽度和高度

    在Android中,有时需要对控件进行测量,得到的控件宽度和高度可以用来做一些计算.在需要自适应屏幕的情况下,这种计算就显得特别重要.另一方便,由于需求的原因,希望一进入界面后,就能得到控件的宽度和高 ...

  5. Qt中在UI文件中新建控件并命名,但在代码中无法识别UI中的控件?

    代码中添加FilePathLineEdit控件,显示标准文件选择对话框显示选择的文件路径,但在槽函数中ui->FilePathLineEdit->setText("FilePat ...

  6. Android在onCreate中获取控件的宽高

    在某些需求下,我们需要在onCreate的时候就获取到控件的宽高,但是如果直接用view.getWidth()或view.getHeight()会得到0.这是因为在onCreate执行的时候,控件还没 ...

  7. activity 中获取控件的宽高

    1.第一种方式: TextView textview3 = findViewById(R.id.textview3); textView3.post(new Runnable() { @Overrid ...

  8. Android在onCreate()中获得控件尺寸

    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceSt ...

  9. fragment中获取activity中的控件

随机推荐

  1. Deleting backup_label on restore will corrupt your database!

    The quick summary of this issue is that the backup_label file is an integral part of your database c ...

  2. Android学习五:Content Provider 使用

    1ContentProvider相关知识1.1在安卓应用中,通过文件方式对外共享数据,需要进行文件操作读写数据:采用sharedpreferences共享数据,需要使用sharedpreference ...

  3. input:-webkit-autofill 导致chrome的输入框背景颜色变成黄色

    填写form表单时发现chrome的一个好坑啊! 当你之前有填写过表单,获取焦点时,input会有一个记录之前填写过的文本的下拉列表式的东东,就像这样:(抱歉丑了点,隐私问题打上了马赛克) 按理说,这 ...

  4. ObjectID和Handle

    一个dwg对应一个arx database,也就是一套9个符号表和一个有名词典. 一个CAD session中是可以加载多个database的.加载后每个对象都有一个handle和一个objectid ...

  5. [Freemarker] - 使用struts的component调用freemarker的ftl模板方法

    struts中的component标签,可以用来调用freemarker的ftl模板文件,使用component标签传参可以这样写: 使用property方式写法: <s:component t ...

  6. SQL 创建一个只读账户 .

    1.进入sqlserver management studio 2.选择安全性->登录名->右键新建登录名 3.在常规里输入用户名和密码 4.在"用户映射"里“映射到此 ...

  7. PHP中的Libevent学习

    wangbin@2012,1,3 目录 Libevent在php中的应用学习 1.      Libevent介绍 2.      为什么要学习libevent 3.      Php libeven ...

  8. [Tex学习]编号

    \documentclass{ctexart}\usepackage{enumerate}\begin{document}\begin{enumerate}[{case}1]\item new\ite ...

  9. [Tex学习笔记]数学公式再次测试

    \begin{align*}\sum_{n=0}^{\infty}\frac{(n!)^{2}2^{n+1}}{(2n+1)!}&=\sum_{n=0}^{\infty}\int_{0}^{1 ...

  10. 基础_ _返回键back

    ============ 2, android中的back键处理 很多网友不明白,如何在Android平台上捕获Back键的事件. Back键是手机上的后退键,一般的软件不捕获相关信息可能导致你的程序 ...