Introduction to precise layout of android LinearLayout and RelativeLayout combinations

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.