Android之activity中新建控件

【Android之activity中新建控件】古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述Android之activity中新建控件相关的知识,希望能为你提供帮助。
了解了5大布局,我们会发现这些布局都是静态的,如何让系统自动生成控件呢?这就需要activity来帮忙了
今天我们讲的就是用activity新建布局
用案例来说吧!
实现一个输入行和列自动生成表格并生成背景颜色
效果如图

Android之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" > < EditTextandroid: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>

 
1 package com.example.linearlayout; 2 3 import android.app.Activity; 4 import android.graphics.Color; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.view.MenuItem; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.LinearLayout; 12 import android.widget.TableLayout; 13 import android.widget.TableRow; 14 import android.widget.TextView; 15 import android.widget.Toast; 16 17 18 public class MainActivity extends Activity { 19private TextView text_column; 20private TextViewtext_row ; 21private TableLayout tabLay; 22private int length = 64; 23@Override 24protected void onCreate(Bundle savedInstanceState) { 25super.onCreate(savedInstanceState); 26setContentView(R.layout.layout2); 27//获取控件按钮 28Button btn_commit = (Button) findViewById(R.id.btn_commit); 29
//获取控件textview 30text_column = (TextView) findViewById(R.id.text_column); 31text_row= (TextView) findViewById(R.id.text_row); 32 33//给按钮设置点击事件 34btn_commit.setOnClickListener(new OnClickListener(){ 35 36@Override 37public void onClick(View v) { 38

//获取文本值 39String column = text_column.getText().toString().trim(); 40String row = text_row.getText().toString().trim(); 41//判断是否为空 42if(column.length() != 0 & & row.length() != 0){ 43//获取控件布局 44tabLay= (TableLayout) findViewById(R.id.tablelayout); 45 46//先清空 47tabLay.removeAllViewsInLayout(); 48tabLay.setStretchAllColumns(true); 49//强转整型 50intx =Integer.parseInt(text_column.getText().toString()); //?? 51inty =Integer.parseInt(text_row.getText().toString()); 52 53 54//for循环嵌套 55for(int m = 0 ; m < y; m++ ){ 56 57TableRow tr = new TableRow(MainActivity.this); 58 59for(int n = 0 ; n < x ; n++ ){ 60 61TextView tv = new TextView(MainActivity.this); 62 63//tv.setText("("+n+","+m+")"); 64tv.setText(" "); 65tr.addView(tv); 66//获取参数,设置背景颜色值 67int result = m*n +2*n-1; 68int red = result / 16/16 ; 69int green = ( result - red*16*16)/16; 70int blue = result - red*16*16 - green*16; 71 72tv.setBackgroundColor(Color.rgb(red*16, green*16, blue*16)); 73 74 75} 76tabLay.addView(tr); 77 78} 79 80}else{ 81Toast.makeText(MainActivity.this,"请输入行和列",1).show(); 82} 83} 84}); 85 86} 87 }

 


    推荐阅读