In the previous article, we have introduced some code layout. Next, we will introduce
1. Common component settings
Buttons, imageview, and other components are basically the same as those defined in XML.
Some common settings are provided for your reference:
Setvisibility (view. Visible) // whether it is visible
Requestfocus () // obtain the focus
Setgravity (gravity. center_vertical) // internal location alignment
Setpadding (10, 5, 5, 5); // The internal distance from each side
Setid (1); // Set ID
For Android: layout_margintop settings in XML, the component does not have this setting method. In fact, it can be seen from the literal meaning of layout_margintop that it sets the location for the parent class layout.
Here is an example of setting layout_margintop:
mLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);mLayoutParams.addRule(RelativeLayout.RIGHT_OF, ID_IMAGE_HEAD);mLayoutParams.topMargin = 5;mLinearLayout.addView(mTextView, mLayoutParams);
If you have an impression on the Position Setting of the textview component in the previous article, it is not hard to understand here. It is to tell the parent class object where the current component will be placed for the parent class layout.
Here is only an example of how to set the maximum number of words to be entered for the edittext component:
mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(8) });
The maximum number of words that can be entered is 8.
2. Relative Layout
In addition to linear la s that are frequently used, relative la s are often used, especially when the relative location design is very suitable for model adaptation requirements.
When writing XML with relative layout, We need to mark each of the most recent settings so as to tell other components the relative location relationship. This is also true in the Code layout. You Need To Set ID labels for them independently and do not duplicate them. Otherwise, the location relationship will be incorrect.
The following is an example for your understanding:
// Create the relativelayout object relativelayout mrelativelayout = new relativelayout (this); mrelativelayout. setlayoutparams (New linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. fill_parent); // textview component: textview mtextview = new textview (this); mtextview. settextsize (30); mtextview. settextcolor (-1); mtextview. settext ("Hello World"); // set a unique ID for mtextview. setid (1); // set the location relationship relativelayout for it. layoutparams mlayoutparams = new relativelayout. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); // 20 mlayoutparams from the top. topmargin = 20; // 20 mlayoutparams to the left. leftmargin = 20; // Add the mrelativelayout component. addview (mtextview, mlayoutparams); // textview component: mtextview = new textview (this); mtextview. settextsize (20); mtextview. settextcolor (0xffff0000); mtextview. settext ("Hello World"); // set a unique ID for mtextview. setid (2); // set the location relationship mlayoutparams = new relativelayout. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); // 20 mlayoutparams from the top. topmargin = 20; // 20 mlayoutparams to the left. leftmargin = 20; // set it to mlayoutparams under the white text. addrule (relativelayout. below, 1); // The mlayoutparams is displayed horizontally in the center. addrule (relativelayout. center_horizontal); // Add the mrelativelayout component. addview (mtextview, mlayoutparams );
: