• ANDROID 自动生成动态表格for


    简单的栗子去了解这个自动生成的动态的控件(自动生成表格)

    /cs-Layout/res/layout/activity_main.xml

    <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_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="horizontal" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:gravity="center_vertical"
                    android:text="请输入行:"
                    android:textSize="20sp" />
    
                <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_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="horizontal" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:gravity="center_vertical"
                    android:text="请输入列:"
                    android:textSize="20sp" />
    
                <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:id="@+id/button1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="一键自动生成表格" />
            </LinearLayout>
        </LinearLayout>
    
        <TableLayout
            android:id="@+id/flout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </TableLayout>
    
    </LinearLayout>
    activity_main.xml

    /cs-Layout/src/com/example/cs_layout/MainActivity.java

    package com.example.cs_layout;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    
    public class MainActivity extends Activity {
        private Button submit;
        private EditText cloumn;
        private EditText run;
        private TableLayout flout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // cloumn = ;
            submit = (Button) findViewById(R.id.button1);
            cloumn = (EditText) findViewById(R.id.editText1);
            run = (EditText) findViewById(R.id.editText2);
            flout = (TableLayout) findViewById(R.id.flout);
    
            submit.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    flout.removeAllViews();
                    new MainActivity().shows(cloumn, run, flout, MainActivity.this);
    
                }
            });
    
        }
    
        public void shows(EditText cloumn, EditText run, TableLayout flout,
                Activity a) {
            int c = Integer.parseInt(cloumn.getText() + ""); // 将输入进来的数转整数
            int r = Integer.parseInt(run.getText() + "");
    
            TableLayout table = new TableLayout(a); // 表格布局
            table.setWeightSum(1); // 权重
            // table.setColumnStretchable(0, false);
            for (int i = 0; i < c; i++) {
                TableRow tr = new TableRow(a);
    
                for (int j = 0; j < r; j++) {
                    Button b = new Button(a);
    
                    // b.setText(j);
                    tr.addView(b);
                }
                table.addView(tr);
            }
            flout.addView(table);
        }
    
        public void color() {
    
        }
    
    }
    MainActivity.java

  • 相关阅读:
    前端利用百度开发文档给的web服务接口实现对某个区域周边配套的检索
    libevent源码学习(13):事件主循环event_base_loop
    libevent源码学习(11):超时管理之min_heap
    libevent源码学习(10):min_heap数据结构解析
    libevent源码学习(8):event_signal_map解析
    libevent源码学习(9):事件event
    libevent源码学习(6):事件处理基础——event_base的创建
    libevent源码学习(5):TAILQ_QUEUE解析
    仿Neo4j里的知识图谱,利用d3+vue开发的一个网络拓扑图
    element表格内每一行删除提示el-popover的使用要点
  • 原文地址:https://www.cnblogs.com/Seven-cjy/p/6101775.html
Copyright © 2020-2023  润新知