通过代码生成TableRow

MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TableLayout tableLayout=findViewById(R.id.tableLayout);
        for (int i=0;i<3;i++){
            addTableRow(tableLayout);
        }

    }

    //dp单位转换为px
    public static int dp2px(Context context, float dpValue){
        return (int)(dpValue * (context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    private void addTableRow(TableLayout tableLayout) {

        TableRow tableRow=new TableRow(this);

        //这里一定要用android.widget.TableRow.LayoutParams,不然TableRow不显示
        tableRow.setLayoutParams(new LinearLayout.LayoutParams(android.widget.TableRow.LayoutParams.MATCH_PARENT, android.widget.TableRow.LayoutParams.MATCH_PARENT));

        int height=dp2px(this, 40);
        int margin=dp2px(this, 0.5f);
        int padding=dp2px(this, 2);

        EditText editText1=new EditText(this);
        editText1.setSingleLine();
        editText1.setBackgroundColor(Color.WHITE);
        android.widget.TableRow.LayoutParams params1=new android.widget.TableRow.LayoutParams(0, height);
        params1.weight=1;
        editText1.setGravity(Gravity.CENTER);
        params1.setMargins(margin,margin,margin,margin);
        editText1.setLayoutParams(params1);
        editText1.setPadding(padding,padding,padding,padding);
        editText1.setInputType(InputType.TYPE_CLASS_NUMBER);
        editText1.setTextSize(13);
        tableRow.addView(editText1);

        android.widget.TableRow.LayoutParams params2=new android.widget.TableRow.LayoutParams(0, height);
        EditText editText2=new EditText(this);
        editText2.setSingleLine();
        editText2.setBackgroundColor(Color.WHITE);
        params2.weight=1;
        editText2.setGravity(Gravity.CENTER);
        params2.setMargins(0,margin,margin,margin);
        editText2.setLayoutParams(params2);
        editText2.setPadding(padding,padding,padding,padding);
        editText2.setInputType(InputType.TYPE_CLASS_NUMBER);
        editText2.setTextSize(13);
        tableRow.addView(editText2);

        android.widget.TableRow.LayoutParams params3=new android.widget.TableRow.LayoutParams(0, height);

        EditText editText3=new EditText(this);
        editText3.setSingleLine();
        editText3.setBackgroundColor(Color.WHITE);
        params3.weight=1;
        editText3.setGravity(Gravity.CENTER);
        params3.setMargins(0,margin,margin,margin);
        editText3.setLayoutParams(params3);
        editText3.setPadding(padding,padding,padding,padding);
        editText3.setInputType(InputType.TYPE_CLASS_NUMBER);
        editText3.setTextSize(13);
        tableRow.addView(editText3);

        tableLayout.addView(tableRow);

    }


}

activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.wly.myapplication.MainActivity"
    android:orientation="vertical">

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_marginTop="10dp"
        android:background="#999">
    <TableRow>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="40dp"
            android:layout_marginTop="0.5dp"
            android:layout_marginLeft="0.5dp"
            android:layout_marginRight="0.5dp"
            android:layout_marginBottom="0.5dp"
            android:background="@color/white"
            android:gravity="center"
            android:textSize="13sp"
            android:textStyle="bold"
            android:textColor="#333333"
            android:text="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="40dp"
            android:layout_marginTop="0.5dp"
            android:layout_marginEnd="0.5dp"
            android:layout_marginBottom="0.5dp"
            android:background="@color/white"
            android:gravity="center"
            android:textSize="13sp"
            android:textStyle="bold"
            android:textColor="#333333"
            android:text="2"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="40dp"
            android:layout_marginTop="0.5dp"
            android:layout_marginEnd="0.5dp"
            android:layout_marginBottom="0.5dp"
            android:background="@color/white"
            android:gravity="center"
            android:textSize="13sp"
            android:textStyle="bold"
            android:textColor="#333333"
            android:text="3"/>

    </TableRow>
    </TableLayout>

</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值