First, clarify the differences between several concepts:
Padding margin is the meaning of margins. The key issue is to understand what the margins are.
Padding is the margin of the control content relative to the control edge.
Margin is the margin of the control edge relative to the parent space.
Android: The gravity attribute limits the content of the view. For example, you can set the text on the button to the left and right of the view.
Android: layout_gravity is used to set the position of the child view in the view relative to the parent view. for example, if a button is in linearlayout, you can set it in linearlayout by placing it on the left and right.
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "horizontal" android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: gravity = "center_vertical">
<ImageView android: id = "@ + id/ivLogo" android: layout_width = "50dp"
Android: layout_height = "50dp" android: src = "@ drawable/icon"
Android: paddingLeft = "5dp"/>
<RelativeLayout android: id = "@ + id/rl_name"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: gravity = "right"
Android: padding = "10dp">
<TextView android: id = "@ + id/tvApplicationName"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"
Android: textSize = "16dp"/>
</RelativeLayout>
<RelativeLayout android: id = "@ + id/rl_score"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: gravity = "right"
Android: padding = "10dp">
<TextView android: id = "@ + id/tvRating" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: text = "5.0"/>
<RatingBar android: id = "@ + id/ratingbar" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: numStars = "5"
Style = "? Android: attr/ratingBarStyleSmall "android: layout_below =" @ id/tvRating "/>
</RelativeLayout>
</LinearLayout>
The above layout file is the list_item layout in a ListView. All APK resources are displayed in a ListView, and icons, names, and scores are displayed for each resource item. Add android: gravity = "center_vertical" to the outermost LinearLayout of the listItem, and set the content to be vertically centered. In RelativeLayout with id rl_score, set android: layout_width = "fill_parent" to fill in the remaining space. android: gravity = "right" sets the right alignment of content relative to rl_score. android: padding = "10dp" sets the margin of the content in RelativeLayout to 10dp relative to the edge of RelativeLayout.
This layout is simple but often used.