Introduction to the precise layout method of Android LinearLayout and relativelayout combination _android

Source: Internet
Author: User
first, the difference between several concepts
Padding margin is the meaning of the margin, the key question is to understand what the relative margin.
Padding is the margin of the control's content relative to the edge of the control.
Margin is the margin of the control's edge relative to the parent space.

The Android:gravity property is a qualification to the view content. such as a button above the text. You can set the text in view on the left, right and so on. This is the property that did this.
Android:layout_gravity is used to set the position of a child view in this view relative to the parent view. For example, a button in the LinearLayout, you want to put the button on the left, on the right side of the position can be in LinearLayout through the property settings.
Copy Code code 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 a List_item layout in a ListView, displaying all the APK resources in a ListView, displaying icons, names, and ratings for each resource item. Add android:gravity= "center_vertical" to the outermost linearlayout of the ListItem, and the contents are centered vertically. Setting android:layout_width= "Fill_parent" in the Relativelayout with ID rl_score to fill the remaining space; android:gravity= "right" setting content relative to Rl_ Score right alignment, android:padding= "10DP" setting the margin of the content in Relativelayout relative to the relativelayout edge is 10DP.
Although this layout is simple, it is often used.

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.