Android LinearLayout Layout Instance code

Source: Internet
Author: User

First clear 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 control.

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.

Cases

The code is as follows Copy Code

<?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>

Example 2

The code is as follows Copy Code

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1" >
<textview
android:text= "Red"
Android:gravity= "Center_horizontal"
Android:background= "#aa0000"
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:layout_weight= "1"/>

<textview
android:text= "Green"
Android:gravity= "Center_horizontal"
Android:background= "#00aa00"
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:layout_weight= "1"/>

<textview
Android:text= "Blue."
Android:gravity= "Center_horizontal"
Android:background= "#0000aa"
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:layout_weight= "1"/>

<textview
android:text= "Yellow"
Android:gravity= "Center_horizontal"
Android:background= "#aaaa00"
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:layout_weight= "1"/>

</LinearLayout>

<linearlayout
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "2" >

<textview
android:text= "Row One"
Android:textsize= "15pt"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"/>

<textview
android:text= "Row Two"
Android:textsize= "15pt"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"/>

<textview
android:text= "Row Three"
Android:textsize= "15pt"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"/>

<textview
android:text= "Row Four"
Android:textsize= "15pt"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"/>

</LinearLayout>

</LinearLayout>

Set the border for LinearLayout.

1. First in the Res directory to create a new XML file, type selection drawable, will automatically give birth to a drawable file (the SDK I use is Android 4.1), and generate an XML file in which to write the following code:

The code is as follows Copy Code

<?xml version= "1.0" encoding= "Utf-8"?>
<shape xmlns:android= "Http://schemas.android.com/apk/res/android" >

<solid android:color= "#FFFFFF"/>

<stroke
Android:width= "0.01DP"
Android:color= "#FFFFFF"/>

<padding
Android:bottom= "1DP"
android:left= "0.5DP"
android:right= "0.5DP"
android:top= "0DP"/>
</shape>

2. Add in the Control XML command to set the border: android:background= "@drawable/boder"

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.