New UI-Java-only code loading layout, newui-java Layout

Source: Internet
Author: User

New UI-Java-only code loading layout, newui-java Layout

New UI-loading layout of pure Java code

-- Reprinted please indicate the source: coder-pig. You are welcome to repost it. Please do not use it for commercial purposes!


The piggy Android development and exchange group has been established. You are welcome to join us. You can be a newbie, cainiao, or a great god.

After all, the power is limited. There will certainly be a lot of flaws in writing. You are welcome to point out, brainstorm, And let pig's blog post.

For more details, help more people, O (∩ _ ∩) O thank you!

Piggy Android Development Exchange Group:Piggy Android Development GroupGroup Number:421858269

New Android UI instance catalog:Http://blog.csdn.net/coder_pig/article/details/42145907



This section introduces:

We are used to generating the layout we need using XML, but in some specific situations

You need to use Java code to dynamically add components or layout to our layout! In this section, we will learn the simplest

Use Java code to compile our entire interface!

Ps: in fact, it is not recommended that you use Java code to compile the Android interface layout. The first point is

There will be a lot of code, and it is easy to mess up, and it is not conducive to business separation, we stillWe recommend that you use xml to complete the layout, and then

Use Java code to modify the componentsOf course, sometimes you may need to use Java to dynamically add

Components, but it is not recommended to use Java code to directly write the layout. You have to consider the feelings of people who are reading the code ~



Body of this section:


For example, the pure Java code loading layout is very simple, and the process is nothing more than the following:



Step 1:

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

Create component:Button btnOne = new Button (this );



Step 2:

You can then set relevant attributes for the container or component:

For example, LinearLayout, we can set the component arrangement direction:Ly. setOrientation (LinearLayout. VERTICAL );

The component can also be used. For example, we can set the display text for the Button:BtnOne. setText ("button 1 ");

For details about how to set attributes, see the Android API. Generally, you only need to add the property set in xml. For example

SetPadding (left, top, right, bottom );



Step 3:

Add a component or container to the container. In this case, you may need to set the position for adding the component or set its size:

We need to use a class:LayoutParamsWe can regard it as an information package for layout containers! Encapsulation location and size

And other information! First demonstrateSET SIZEMethod :(The preceding LinearLayout can be changed based on different containers.)

LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);



That's easy. Set locationWhen setting the location, we usually only consider RelativeLayout!

Used at this timeLayoutParams addRule () method!You can add multiple addRule!

Set the position of the component in the parent container:

For example, set the method of the component:

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 other components for their methods:(A disadvantage is that it is a reference component. Manually set an id,Yes, manual !!!!)

For example, after btnOne is set to center, BtnTwo is placed below btnOne and on the right 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"); // set an id value btnOne for button 1. setId (123); // set the position of Button 1, center in the parent container RelativeLayout. layoutParams rlp1 = new RelativeLayout. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); rlp1.addRule (RelativeLayout. CENTER_IN_PARENT); // you can specify the position of Button 2 at the bottom of Button 1 and align it with RelativeLayout on the right of the parent container. layoutParams rlp2 = new RelativeLayout. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); rlp2.addRule (RelativeLayout. BELOW, 123); rlp2.addRule (RelativeLayout. ALIGN_PARENT_RIGHT); // Add the component to the external container rly. addView (btnTwo, rlp2); rly. addView (btnOne, rlp1); // set the View loaded by the current View to rlysetContentView (rly );}}



Step 4:

CallSetContentView ()Method to load the layout object!

In addition, if you want to remove the View from a container, you can callContainer. removeView (component to be removed );


Run:







Now, let's talk about the content of loading layout using pure Java code. Thank you ~






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.