Using Padding and Margins with Views and Layouts :
Padding adds spacing to a View (or a widget subclassed
therefrom), so that that View’s content is
offset by a certain number of pixels on each of the four
sides. This way, the content doesn’t touch
the edges of the View and look unprofessional. In other
words, padding adds space around the
outside of a View’s content, and you can choose to do so on
any of the four sides of the View object.
When using padding, the padding is considered to be part of
the View, which may affect the way
Android lays out the View. Remember, we are talking here
about a View Class or Object, that is, a
user interface widget, not the view on your Android device’s
screen!
Padding values are only available to Views, not to ViewGroups
(and thus not available in screen layout
containers).
This is logical because View objects (usually
widgets) are contained inside something
(ViewGroups) but contain nothing inside of them, that is,
they are what they are (Button, Clock, Radio
Button, Text Field, etc.).
ViewGroups instead support margins, which allow the same
results as padding to be obtained,
except that the margins are not considered part of the ViewGroup.
For me, this makes UI design more
organized and easy to remember: Views use padding values and
ViewGroups use margin values and
also can use padding values.
Setting Padding in Views:
Padding can be set for your View objects (usually widgets)
in three ways.
The first and easiest way to set a padding value for a UI
element widget is to use the Properties Editor pane in Eclipse, where all of the
properties of a selected UI element in the Graphical Layout Editor will be displayed on the right side
(or wherever you place that pane) of the Eclipse IDE.
The second way to set properties of a UI widget is in the
XML editor pane, where when you type
android: a window containing all of the parameter options
(including padding settings) will pop-up
as soon as you type the colon symbol after you type in
android (in lower case). Then you can
double-click on the parameter that you want and type in its
value or even type in the entire
parameter from memory without using the helper that works in
the XML editing pane.
Most of the time you will use one of these two methods to set padding;
using Java for setting padding values is not a “Best Practice,” unless you are doing something really
vanguard and need to have real-time
control over padding values.
That said, padding can also be set via your Java code, using
the .setPadding() method with four
values, for left, top, right, and bottom. Think of going
around a clock, starting at 9:00, separated by
commas. So, to put a four-pixel border inside your view, you
would use the following (remember that
the order of parameters is left, top, right, bottom):
objectName.setPadding(4,4,4,4);
You can also separate each side in the Java methods, just
like you can with the XML tag parameters.
So, to get the padding for the left side of the view, use .getPaddingLeft().
To set just the padding on
the top to eight pixels, you would write something like
this:
ObjectName.setPaddingTop(8);
Setting Margins in ViewGroups :
For ViewGroups, including layout containers (the subject of
this chapter), the easiest way to set
margins during development is via the XML parameters for any
ViewGroup object.
Four layout margin values are available in XML:
- android:layout marginBottom
- android:layout marginLeft
- android:layout marginRight
- android:layout marginTop
We used the android:layout_marginTop in our RelativeLayout example
earlier to
space the text entry UI element down and away from the text
label UI element.
No comments:
Post a Comment