New ui-Plain Java code load layout

Source: Internet
Author: User

New ui-Plain Java code load layout

--Reprint Please indicate source: Coder-pig, welcome reprint, do not use for commercial use!


Piglet Android Development Exchange Group has been established, welcome everyone to join, both novice, rookie, Big God can, piglet a person's

Strength is limited after all, the writing out of things will certainly have a lot of flaws, welcome to point out, brainstorm, let the pig's blog

More detailed, help more people, O (∩_∩) o Thank you!

Piglet Android Development Exchange Group: Piglet Android Development Exchange Group Group number: 421858269

new Android UI Instance Daquan directory: http://blog.csdn.net/coder_pig/article/details/42145907



Introduction to this section:

We've grown accustomed to using XML to generate the layouts we need, but in some specific cases, we

You need to use Java code to dynamically add components or layouts to our layouts! We'll learn the simplest in this section.

Use Java code to write our entire interface!

PS: In fact, it is not recommended that you completely use Java code to write the Android interface layout, the first point is

code will be much, and easy to mess, and not conducive to the separation of business, we still We recommend that you use XML to complete the layout, and then

Modify the components inside the Java code , of course, there may be times when you need to use Java to dynamically add

components, but it is still not recommended to use Java code to write the layout directly, you have to consider the code of the people feel it ~



The text of this section:


Title, pure Java code loading layout is very simple, the process is only the following:



Step 1:

Create container :linearlayout ly = new LinearLayout (this);

Create component :button btnone = New button (this);



Step 2:

You can then set related properties for a container or component:

For example LinearLayout, we can set the arrangement direction of the components:ly.setorientation (linearlayout.vertical);

and components can, for example, button we can set the display text:btnone.settext ("button 1");

For a way to set properties, see the Android API, usually the XML setting property needs to be added just before: set, for example

Setpadding (left, top, right, bottom);



Step 3:

Add a component or container to a container, and at this point we may need to set the location of the next component or set his size:

We need to use a class:layoutparams, we can think of it as a packet of information in the layout container! Package location and size

Such information as a Class! The method of setting the size is shown first: ( the previous linearlayout can be changed according to the different containers )

Linearlayout.layoutparams LP1 = new Linearlayout.layoutparams (layoutparams.wrap_content, LayoutParams.WRAP_CONTENT) ;



It's simple, and that's it. Set Location, set the position, usually we're only thinking about relativelayout!.

This is the time to use the layoutparams addrule () Method! can add multiple addrule () Oh!

Set the position of the component in the parent container:

For example, set the way the component should be:

Relativelayout rly = new Relativelayout (this); Relativelayout.layoutparams LP2 = new Relativelayout.layoutparams (layoutparams.wrap_content, LayoutParams.WRAP_ CONTENT); Lp2.addrule (Relativelayout.align_parent_bottom); Button Btnone = New button (this); Rly.addview (Btnone, LP2);



refer to the other components for their way:(one drawback is that the reference component manually set an ID,IS manual!!!!)

For example: After setting the Btnone Center, let Btntwo be at the bottom of btnone and the right side of the parent container! The code is as follows:

Package Com.jay.example.trendsinflateviewdemo;import Android.app.activity;import Android.os.bundle;import Android.view.viewgroup.layoutparams;import Android.widget.button;import Android.widget.linearlayout;import Android.widget.relativelayout;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Relativelayout rly = new Relativelayout (this); Button Btnone = New button (this); Btnone.settext ("button 1"); Button Btntwo = New button (this), Btntwo.settext ("button 2"),//setting an ID value for button 1 Btnone.setid (123);//Setting the position of the button 1, In the parent container, center relativelayout.layoutparams RLP1 = new Relativelayout.layoutparams (layoutparams.wrap_content, layoutparams.wrap_content); Rlp1.addrule (relativelayout.center_in_parent);//Set the position of button 2, under button 1, and align the parent container to the right relativelayout.layoutparams RLP2 = new Relativelayout.layoutparams (layoutparams.wrap_content, layoutparams.wrap_content); Rlp2.addrule (Relativelayout.below, 123); Rlp2.addrule (Relativelayout.align_parent_ right);//sets the groupAdd to the external container Rly.addview (Btntwo, RLP2), Rly.addview (Btnone, RLP1);//The view that the current view loads is Rlysetcontentview (rly);}} 



Step 4:

Call the Setcontentview () method to load the layout object!

Also, if you want to remove a view from a container, you can call the container. Removeview (the component to remove);


Run:







Well, about loading the layout with plain Java code, here we go, thank you.






New ui-Plain Java code load layout

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.