Use Java code to create a view. Use java code to create a view.

Source: Internet
Author: User

Use Java code to create a view. Use java code to create a view.
Use Java code to create view 1. Introduction

Knowledge needed

 

 

Ii. Method

1) create a view using java code
* 1. Create a view object first

View view = View. inflate (this, R. layout. activity01, null );

* 2. Fill in the R. layout. activity01 page in the view.

View view = View. inflate (this, R. layout. activity01, null );
* 3. Add various controls (such as TextView and Button) to the view object. You must convert them to the ViewGroup type before adding them.

Create a TextView Control

(RelativeLayout) view). addView (textView );
* 4. Fill in the view object to the page, that is, fill the value of setContentView into the view object,

SetContentView (view );


2) create a control using TextView as an example.
* 1. Create a TextView object

TextView textView = new TextView (this );
* 2. Set layout parameters for TextView objects

LayoutParams layoutParams = new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT );

TextView. setLayoutParams (layoutParams );
* 3. Set other attributes for the TextView object

TextView. setBackgroundColor (Color. CYAN );
TextView. setText ("hello, stranger !!! ");

 

Iii. code example

 

Code

Fry. Activity01

1 package fry; 2 3 4 import com. fry. javaCreateView. r; 5 6 import android. app. activity; 7 import android. graphics. color; 8 import android. OS. bundle; 9 import android. view. view; 10 import android. view. viewGroup. layoutParams; 11 import android. widget. relativeLayout; 12 import android. widget. textView; 13 14 public class Activity01 extends Activity {15 @ Override16 protected void onCreate (Bundle savedInstance State) {17 // TODO Auto-generated method stub18 setTitle ("java code create view"); 19 super. onCreate (savedInstanceState); 20/* 21 * java code to create the view Method 22*1. First create the view object 23*2. Fill in the view with R. layout. activity01 page 24*3. Add various controls (such as TextView and Button) to the view object ), note that you must convert it to the ViewGroup type before adding 25*4. Finally, you can fill the view object to the page, that is, you can fill the value of setContentView into a view object, and 26*27 * Create a control method, take TextView as an example 28*1. Create TextView object 29*2. Set layout parameters for TextView object 30*3. Set other properties for TextView object 3. 1*32 */33 View view = View. inflate (this, R. layout. activity01, null); 34 35 LayoutParams layoutParams = new LayoutParams (LayoutParams. MATCH_PARENT, 36 LayoutParams. WRAP_CONTENT); 37 TextView textView = new TextView (this); 38 textView. setLayoutParams (layoutParams); 39 textView. setBackgroundColor (Color. CYAN); 40 textView. setText ("hello, stranger !!! "); 41 42 43 (RelativeLayout) view). addView (textView); 44 45 46 setContentView (view); 47} 48}

 

Related Article

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.