Android Layout Detailed
1. Reusing layouts
When a layout file is used in multiple places, it is best to <include> tags to reuse the layout.
For example:workspace_screen.xml layout file, which is reused three times in another layout file, use the following layout code:
<linearlayout
Androd:layout_width= "Fill_parent"
androd:layout_height= "Fill_parent" >
<!-- references three times Workspace_screen--
<include android:id= "@+id/cell1 layout=" @layout/workspace_screen "/>
<include android:id= "@+id/cell2 layout=" @layout/workspace_screen "/>
<include android:id= "@+id/cell3 layout=" @layout/workspace_screen "/>
</LinearLayout>
In the above code,<include>The label also uses aAndroid:idproperty, in effect, this property specifies theWorkspace_screen.xmlof the root node in the layout file .Android:idThe value of the property. The Johungen node has been setAndroid:idproperty value, then<include>Label'sAndroid:idproperty value will be overwrittenWorkspace_screen.xmlof the root node in the layout file .Android:idThe value of the property. <include>The label can also override the referenced layout file root node amount of all layout-related properties (that is, the "Android:layout_"starts with the property. Overriding property values allows you to have different layout styles for the views in the referenced layout file. For example, the following layout file code references theImage_holder.xmlfile two times, but only the first one<include>tags override some of the properties:
<!--covers width and height in the root node--
<include layout= "@layout/image_holder"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"/>
<!-- below does not overwrite any properties--
<include layout= "@layout/image_holder"/>
Note: If you like the dimensions of the layout, you must also overwrite android:layout_width android:layout_height
<include> tags are important when designing device-related layout files, for example,landscapeand vertical screens (portrait You can use a different layout file, but it is possible that the layout of the cross-view is the same. This allows The layout to be used in the Res/layout directory for both horizontal and vertical screens, while the res/layout-land and res/ layout files in the Layout-port directory can refer to these layouts using the <include> tags.
2. Layout aliases
InResall subdirectories under the directory are resource directories, such asres/values,Res/layoutand so on. These are the default resources that are stored in the wood. However, in some cases the system needs to use additional resources, for example, programs that support internationalization if the current environment is Chinese, all string resources are required fromRes/values-zhdirectory to read, if the environment is in English, then go toRes/valuse-ento read in. WhichRes/values-zh,Res/valuse-enis the localized resource directory. A localized directory is a directory of resource files that meet specific requirements, such as the size of the screen, the differentAndroidversions, different locales, different screen orientations, and more. If the system direction does not meet the current specific requirements of the localized resource directory, it will be from the default resource directory to find resources. For example, the local resource directory is now onlyRes/values-zhand theRes/values-en, while the current locale is French, but there is nores/values-frdirectory, all systems will look for the resource directory in the default resource directory (res/values) to find the appropriate string resource.
The so-called layout alias is to specify the same resource for different layout files id id access to different layout resources. use layout aliases must be aware that a reference to a layout file can only be placed in the localized resource directory, not in the default resource directory (res/ Values ). For example:
If there are two layout filesin the res/layout directory:main_layout.xml and main_layout_en.xml, and use Setcontentview (r.layout.main_layout) in the main window to associate Main_layout.xml with the current window. Now create a res/values-en directory, and then create a refs.xml file in that directory (the resource file name can be any name), and finally Refs.xml file, enter the following:
<resources>
<!-- Assign an alias named main_layout to the main_layout_en.xml file--
<item name= "Main_layout" type= "layout" > @layout/main_layout_en</item>
</resources>
If the current environment is exactly English, the system will use the resources in the Res/values directory, so the main_layout_en.xml file will be assigned an alias. If the alias is exactly the same as the resource ID of a layout file , then it is equivalent to modifying the resource ID Pointer, that is, if in English context,R.layout.main _layout is not referring to main_layout.xml, but main_layout_en.xml.
Generally if the file in the directory is too messy, you need to sort out two ways:
① set up several subdirectories and then categorize the directory files into these newly created directories.
② does not move the files in the metadirectory, but instead establishes an index for each file (which can be stored in a database or other file), and then group management of those indexes. The advantage of doing this is that you don't need to move files, and the same file can belong to different categories. If you take the first approach, you will need to copy the directory for each category that the file belongs to, wasting space.
3. Dynamic load Layout
For example, the main layout file (activity_load_layout.xml) contains only a <LinearLayout> tag and does not contain any child tags ( Item.xml ), because all views are dynamically loaded.
If you need to dynamically mount and add views, Tongcheng calls the layoutinflatable.inflate method in the main window class (loadlayoutactivity) to create a new view , The prototype of the inflate method is as follows:
Public View Inflate (int resource, viewgroup root);
Resource: Represents the View IDto mount;Root: Represents the parent view of the view to be mounted. Null if there is no parent view .
4. Dynamically Set layout properties
When you add a view dynamically, you do not set the layout in a static way, and you need to use the Layoutparams class if you want to reset the layout properties . Note, however, that the main window layout uses the linearlayout layout, so use android.widget.LinearLayout.LayoutParams class. the code for the modified loadlayoutactivity class is as follows:
public class Loadlayoutactivity extends Activity {
@Override // Set all controls to center display
protected void I=oncreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
LinearLayout parent = (linearlayout) getlayoutinflater (). Inflate (r.layout.sctivity_load_layout, NULL);
for (int i=0; i<10; i++) {
View view = Getlayoutinflater (). Inflate (r.layout.activity_load_layout, NULL);
TestView TextView = (testview) View.findviewbyid (R.id.textview);
Text.settext ("text" +i);
Create a layoutparams Object
Android.widget.LinearLayout.LayoutParams layoutparams = new Linearlayout.layoutparams (layoutpatams.wrap_content, Layoutparams.wrap_content);
set The value of the gravity field (centered horizontally)
layoutparams.gravity = Gravity.center_horizontal;
Dynamic add view is a layout parameter that specifies the parent view (linearlayout)
Parent.addview (view, layoutparams);
}
Setcontentview (parent);
}
}
5. Right-to-left layouts (RTLlayout)
starting with Android4.2 , theAndroid SDK supports a right -to-left UI layout.