Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Code dynamic Generation Interface in Android

Shulou Source: shulou.com Published: 2022-06-03 10:23:28 11月15日 Update

This article mainly introduces Android how to use the code dynamic generation interface, the article is very detailed, has a certain reference value, interested friends must read it!

We most often use XML to write UI for Android applications. The advantage is that it is convenient and quick to visualize, and it is easy to maintain and modify, but it is static. If the interface of the program we are going to do is fixed, using XML is the best choice, but if we need to control UI dynamically and flexibly, using code to generate UI dynamically is undoubtedly the best way.

In XML, the five layouts we use: LinearLayout (linear layout), RelativeLayout (relative layout), TableLayout (table layout), AbsoluteLayout (absolute layout) and FrameLayout (frame layout) are also represented by corresponding classes in Android.

For example, I now need to display a table with uncertain rows and columns and their contents, which would not be possible in XML.

Let's first show you the finished product: (the following code only shows you how to implement it, and the contents in the table are ignored)

First, create a new XML file without any controls

Create a new TableLayout in the code:

/ / TODO displays table information private void displayRegeditedInfo () {Iterator iterator = iterable.iterator (); ICells iCells = GlobalVariable.manager. CreatePersonDataCells (IInspectionManager.CS_PERSON_LIST_CELLS); boolean flag = true;// title bar is true, content field false int colorChange = 1 true / used to judge single or double rows to show different colors TableLayout tableLayout = (TableLayout) findViewById (R.id.tableLayout); tableLayout.setStretchAllColumns (true); tableLayout.setShrinkAllColumns (true) The style of while (iterator.hasNext ()) {/ / rows TableRow.LayoutParams params = new TableRow.LayoutParams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); if (flag) / / first displays the title bar of the table, with the content defined by itself {TableRow titleRow = new TableRow (this); for (int I = 0; I < colums) Column names params.setMargins (1,1,1,1); TextView textView = new TextView (this); textView .setBackgroundColor (getResources (). GetColor (R.color.top)); textView.setTextColor (Color.WHITE); textView.setTextSize (31); textView.setLayoutParams (params); textView.setText (columsName); / / column name textView.setTextSize (30); textView.setGravity (Gravity.CENTER_HORIZONTAL) TitleRow.addView (textView); / / add controls to row TableRow} flag = false; tableLayout.addView (titleRow); / / add rows to TableLayout} / / create a new row showing the specific information of each member TableRow personRow = new TableRow (this); for (int I = 0; I < lines; iTunes +) {params.setMargins (1,1,1,1); object / / here I use Object to represent the content displayed in the table. / / Object can be a string, a number, or a photo. Depending on your specific definition, if (object instanceof String) {/ / string centered display TextView textView = new TextView (this); textView.setLayoutParams (params); textView.setTextSize (29) If (colorChange% 2 = = 1) textView.setBackgroundColor (getResources (). GetColor (R.color.second)); else textView.setBackgroundColor (getResources (). GetColor (R.color.third)); textView.setText (object.toString ()); textView.setTextSize (30); textView.setGravity (Gravity.CENTER); personRow.addView (textView) } else if (object instanceof Number) {/ / Digital right display TextView textView = new TextView (this); textView.setPadding (0,0,5,0); / / right inside margin textView.setLayoutParams (params); textView.setText (object.toString ()); textView.setTextSize (30); textView.setTextSize (29); if (colorChange% 2 = = 1) textView.setBackgroundColor (getResources (). GetColor (R.color.second)) Else textView.setBackgroundColor (getResources (). GetColor (R.color.third)); textView.setGravity (Gravity.CENTER_VERTICAL | Gravity.RIGHT); personRow.addView (textView);} else if (object instanceof byte []) {/ / display avatar TableRow.LayoutParams params2 = new TableRow.LayoutParams (60,75); ImageView imageView = new ImageView (this) If (colorChange% 2 = = 1) imageView.setBackgroundColor (getResources (). GetColor (R.color.second)); else imageView.setBackgroundColor (getResources (). GetColor (R.color.third)); Bitmap bitmap = BitmapFactory.decodeByteArray ((byte []) object, 0, ((byte []) object) .length); imageView.setImageBitmap (bitmap); imageView.setLayoutParams (params2); personRow.addView (imageView) } else {/ / Null TextView textView = new TextView (this); textView.setLayoutParams (params); textView.setTextSize (30); if (colorChange% 2 = = 1) textView.setBackgroundColor (getResources (). GetColor (R.color.second)); else textView.setBackgroundColor (getResources (). GetColor (R.color.third)); personRow.addView (textView);}} colorChange++; tableLayout.addView (personRow) }}

You can also add listening events to the entire layout, the entire line, or a space, just setId (int id), and then set up a listener.

These are all the contents of the article "how to use the dynamic Code Generation Interface for Android". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Content layout table code dynamic interface generation information characters strings controls numbers best title program article face monitor different convenient and fast Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Tech Info NVidia Shulou Technology Apple