[079] use code to create an Android Control

Source: Internet
Author: User

Generally, when creating controls, we complete them in the XML file. The implementation is quite convenient, and the modification can quickly see the effect, however, a major disadvantage is that you cannot dynamically create controls. For example, if I want to store data in tableLayout from a database, because I do not know the number of rows and columns, therefore, you cannot create a widget in XML. In addition, you need to create controls with similar styles and functions at the same time. If you keep copying them in XML, it is still annoying, because it can be easily created and modified by using cyclic statements in the Code, the following describes how to dynamically create Android controls through code.

When we use XML to create a control, we usually first set the layout and then set the layout length and width. When setting the control, we also need to set the length and width of the control. In XML, we use android: layout_width indicates the layout width, which is implemented by code below. for other settings, you can view them in the control and layout section of [063] Android API <I>.

Directory:

  1. ViewGroup. LayoutParams
  2. LinearLayout. LayoutParams
  3. Example

Bytes ---------------------------------------------------------------------------------------------------------

When there are too many threads, there are too many threads.
Please refer to the following link for more information: when there are too many threads, there are too many threads, too many threads.
When there are too many threads, there are too many threads.

● LinearLayout. LayoutParams:

1. used to create the layout of LinearLayout. assign values and call values in the LinearLayout. setLayoutParams (ViewGroup. LayoutParams params) method.

Java. lang. Object
Using android. view. ViewGroup. LayoutParams
Using android. view. ViewGroup. MarginLayoutParams
Using android. widget. LinearLayout. LayoutParams

2. Constructors:

  • LinearLayout. LayoutParams(Int width, int height): You can use constants or numbers.
  • LinearLayout. LayoutParams(Int width, int height, float weight ):

3. Constants:

  • FILL_PARENT: Return Value: int.
  • MATCH_PARENT: Return Value: int.
  • WRAP_CONTENT: Return Value: int.

4. Fields:

  • Gravity: Alignment style. (int)
    Gravity.CENTER: Center of left-right horizontal.
    Gravity.CENTER_HORIZONTAL: Horizontal center.
  • Weight: Weight. (float)
  • BottomMargin:
  • LeftMargin:
  • RightMargin:
  • TopMargin:

5. Methods:

  • SetMargins(Int left, int top, int right, int bottom): sets margins with pixel values.

Bytes ---------------------------------------------------------------------------------------------------------

When there are too many threads, there are too many threads.
Please refer to the following link for more information: when there are too many threads, there are too many threads, too many threads.
When there are too many threads, there are too many threads.

● ViewGroup. LayoutParams:

1. It is a base class of other LayoutParams, so such instances can be used for the layout of any controls. Other LayoutParams are only based on this, and some corresponding items are added.

2. Constructors:

  • LinearLayout. LayoutParams(Int width, int height): You can use constants or numbers.

3. Constants:

  • FILL_PARENT: Return Value: int.
  • MATCH_PARENT: Return Value: int.
  • WRAP_CONTENT: Return Value: int.

4. Fields:

  • Height: Height. (int)
  • Width: Width. (int)

Example

TableLayout. removeAllViews (); // clear all internal controls dbAdapter. open (); Cursor cursor = dbAdapter. getAllStudents (); int numberOfRow = cursor. getCount (); // total number of rows int numberOfColumn = 5; // total number of columns for (int row = 0; row <numberOfRow; row ++) {// loop TableRow tableRow = new TableRow (Database. this); // create a tableRow object tableRow. setLayoutParams (new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT); // Add the layout for (int column = 0; column <numberOfColumn; column ++) {// within each row, textView textView = new TextView (Database. this); // creates a textView object. setText ("" + cursor. getString (column) + ""); // if (column = 0) {textView. setBackgroundColor (Color. RED); textView. setTextColor (Color. CYAN); textView. setGravity (Gravity. CENTER);} if (column = 1) {textView. setBackgroundColor (Color. YELLOW); textView. setTextColor (Color. BLACK);} if (column = 2) {textView. setBackgroundColor (Color. GREEN); textView. setTextColor (Color. BLACK);} if (column = 3) {textView. setBackgroundColor (Color. rgb (100, 0,200); textView. setTextColor (Color. BLACK);} if (column = 4) {textView. setBackgroundColor (Color. CYAN); textView. setTextColor (Color. BLACK);} tableRow. addView (textView);} tableLayout. addView (tableRow); cursor. moveToNext ();

Bytes ---------------------------------------------------------------------------------------------------------

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.