zoukankan      html  css  js  c++  java
  • Android之activity中新建控件

    了解了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>
     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 {
    19     private TextView text_column;
    20     private TextView  text_row ;
    21     private TableLayout tabLay;
    22     private int length = 64;
    23     @Override
    24     protected void onCreate(Bundle savedInstanceState) {
    25         super.onCreate(savedInstanceState);
    26         setContentView(R.layout.layout2);
    27        //获取控件按钮
    28        Button btn_commit = (Button) findViewById(R.id.btn_commit);
    29        
          //获取控件textview 30 text_column = (TextView) findViewById(R.id.text_column); 31 text_row= (TextView) findViewById(R.id.text_row); 32 33 //给按钮设置点击事件 34 btn_commit.setOnClickListener(new OnClickListener(){ 35 36 @Override 37 public void onClick(View v) { 38   

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

        

  • 相关阅读:
    RxJava Android(RxAndroid) 开发全家桶
    Android Retrofit RxJava实现缓存
    Android Touch事件传递机制详解 下
    Android Touch事件传递机制详解 上
    Android Framework 记录之二
    XMind 8 Update 7 Pro 激活码
    leetcode 2-> Add Two Numbers
    leetcode 1 -> Two Sum
    leetcode 3-> Longest Substring Without Repeating Characters
    Python enumerate() 函数
  • 原文地址:https://www.cnblogs.com/lgk1002/p/6095066.html
Copyright © 2011-2022 走看看