Relativelayout is relative layout. The components in this layout are always determined by relative sibling components and parent containers. when defining the widget location, you need to refer to the location of other controls.
This program implements a relative layout of plum blossom
<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <! -- Define that the component is located in the middle of the parent container --> <textview Android: Id = "@ + ID/view01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: background = "@ drawable/leaf" Android: layout_centerinparent = "true"/> <! -- Define the left alignment of the widget above view01 and view01 --> <textview Android: Id = "@ + ID/view02" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Background = "@ drawable/leaf" Android: layout_above = "@ ID/view01" Android: layout_alignleft = "@ ID/view01"/> <! -- Define that the component is located at the bottom of the view01 component --> <textview Android: Id = "@ + ID/view03" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: background = "@ drawable/leaf" Android: layout_below = "@ ID/view01" Android: layout_alignleft = "@ ID/view01"/> <! -- Define the component to the left of the view01 component --> <textview Android: Id = "@ + ID/view04" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: background = "@ drawable/leaf" Android: layout_toleftof = "@ ID/view01" Android: layout_aligntop = "@ ID/view01"/> <! -- Define the component to the right of the view01 component --> <textview Android: Id = "@ + ID/view05" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: background = "@ drawable/leaf" Android: layout_torightof = "@ ID/view01" Android: layout_aligntop = "@ ID/view01"/> </relativelayout>
Dynamically set the widget spacing in the activity
Import android. app. activity; import android. OS. bundle; public class relativelayouttest extends activity {@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); findviewbyid (R. id. view01 ). setpadding (5, 5, 5, 5); // you can specify the findviewbyid (R. id. view02 ). setpadding (5, 5, 5, 5); findviewbyid (R. id. view03 ). setpadding (5, 5, 5, 5); findviewbyid (R. id. view04 ). setpadding (5, 5, 5, 5); findviewbyid (R. id. view05 ). setpadding (5, 5, 5, 5 );}}