通过代码生成TableRow

xiaoxiao2021-02-28  4

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>
转载请注明原文地址: https://www.6miu.com/read-2400327.html

最新回复(0)