Details of New UI-layout RelativeLayout (relative layout), ui-relativelayout

Source: Internet
Author: User

Details of New UI-layout RelativeLayout (relative layout), ui-relativelayout

Http://download.csdn.net/detail/zpj779878443/8334001New UI-RelativeLayout (relative layout)

-- Reprinted please indicate the source: coder-pig. You are welcome to repost it. Please do not use it for commercial purposes!


The piggy Android development and exchange group has been established. You are welcome to join us. You can be a newbie, cainiao, or a great god.

After all, the power is limited. There will certainly be a lot of flaws in writing. You are welcome to point out, brainstorm, And let pig's blog post.

For more details, help more people, O (∩ _ ∩) O thank you!

Piggy Android Development Exchange Group: Piggy Android Development Exchange Group No.: 421858269

New Android UI instance Daquan Directory: http://blog.csdn.net/coder_pig/article/details/42145907


This section introduces:

In the previous section, we analyzed LinearLayout in detail. Thank you for your feedback,LinearLayout is also ours.

We are more interested in the weight (weight) attribute, proportional division, and screen adaptation.

It is quite helpful, but there is also a problem when using LinearLayout, that is, when the interface is complicated, it needs to be nested with multiple layers

LinearLayout, which reduces the efficiency (rendering speed) of the UI Render, and

Item, the efficiency will be lower. In addition, too many layers of LinearLayout nesting will occupy more system resources, and may lead to stackoverflow;

However, if RelativeLayout is used, only one layer is required.

+ Padding can be used to set the display position of the component, which is convenient! Of course, this is not an absolute one. Let's analyze the specific problem!

To sum up, try to use the weight attribute of RelativeLayout + LinearLayout!



1. Core diagram of this section:


2. Positioning attributes of the parent container:



3. Locate by sibling Components

Well, let's talk about what a sibling component is. The so-called sibling component is a component of a container at the same level,


Component 1 and 2 in the figure are brother components, and component 3 and Component 1 or component 2 are not brother components, so Component 3 cannot pass

Locate component 1 or 2, for example, layout_toleftof = "component 1". An error is returned! Remember!

The most typical example of this sibling component positioning is the plum blossom layout. The following code is implemented:

Run:


Implementation Code:

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/RelativeLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent"> <! -- This is in the center of the container --> <ImageView android: id = "@ + id/img1" android: layout_width = "80dp" android: layout_height = "80dp" android: layout_centerInParent = "true" android: src = "@ drawable/pic1"/> <! -- On the left of the intermediate image --> <ImageView android: id = "@ + id/img2" android: layout_width = "80dp" android: layout_height = "80dp" android: layout_toLeftOf = "@ id/img1" android: layout_centerVertical = "true" android: src = "@ drawable/pic2"/> <! -- On the right of the intermediate image --> <ImageView android: id = "@ + id/img3" android: layout_width = "80dp" android: layout_height = "80dp" android: layout_toRightOf = "@ id/img1" android: layout_centerVertical = "true" android: src = "@ drawable/pic3"/> <! -- On the top of the intermediate image --> <ImageView android: id = "@ + id/img4" android: layout_width = "80dp" android: layout_height = "80dp" android: layout_above = "@ id/img1" android: layout_centerHorizontal = "true" android: src = "@ drawable/pic4"/> <! -- Under the middle image --> <ImageView android: id = "@ + id/img5" android: layout_width = "80dp" android: layout_height = "80dp" android: layout_below = "@ id/img1" android: layout_centerHorizontal = "true" android: src = "@ drawable/pic5"/> </RelativeLayout>


4. Differences between margin and padding:

Beginners may be confused about these two attributes, which are distinguished below:

First, margin indicates the offset. For example, marginleft = "5dp" indicates that the component is offset 5dp from the left edge of the container;

Padding indicates filling, and the filled object targets elements in components, such as text in TextView.

For example, if paddingleft = "5dp" is set for TextView, 5dp space is filled on the left of the element in the component!

Margin is for components in containers, while padding is for elements in components!

The following code demonstrates the differences between the two:

The Code is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:paddingBottom="@dimen/activity_vertical_margin"      android:paddingLeft="@dimen/activity_horizontal_margin"      android:paddingRight="@dimen/activity_horizontal_margin"      android:paddingTop="@dimen/activity_vertical_margin"      tools:context=".MainActivity" >        <Button          android:id="@+id/btn1"           android:layout_height="wrap_content"          android:layout_width="wrap_content"          android:text="Button"          />      <Button          android:paddingLeft="100dp"           android:layout_height="wrap_content"          android:layout_width="wrap_content"          android:text="Button"          android:layout_toRightOf="@id/btn1"          />                              <Button          android:id="@+id/btn2"           android:layout_height="wrap_content"          android:layout_width="wrap_content"          android:text="Button"          android:layout_alignParentBottom="true"          />      <Button          android:layout_marginLeft="100dp"           android:layout_height="wrap_content"          android:layout_width="wrap_content"          android:text="Button"          android:layout_toRightOf="@id/btn2"           android:layout_alignParentBottom="true"         />        </RelativeLayout>
Run:


I believe you can see the differences between the two!



5. A common point is that margin can be set to a negative number.

I believe many of my friends don't know anything about it. We usually get used to positive numbers when setting margin,

Actually, a negative number can be used. Let's write a simple program to demonstrate it. After simulating the software, an advertisement will pop up.

In the upper-right corner of the page, the cancle button's margin is negative!

:


The layout code of the published advertisement Activity. Of course, if you are interested in this, you can go to the demo,

The code may be a bit rough because it is only an implementation result!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.jay.example.relativelayoutdemo.MainActivity"     android:background="#00CCCCFF">    <ImageView        android:id="@+id/imgBack"        android:layout_width="200dp"        android:layout_height="200dp"        android:layout_centerInParent="true"        android:background="@drawable/myicon" />    <ImageView        android:id="@+id/imgCancle"        android:layout_width="28dp"        android:layout_height="28dp"        android:layout_alignRight="@id/imgBack"        android:layout_alignTop="@id/imgBack"        android:background="@drawable/cancel"        android:layout_marginTop="-15dp"        android:layout_marginRight="-10dp" /></RelativeLayout>


Well, the detailed answer to RelativeLayout in this section is here. If you find any omissions or fun

I must mark it and share it with you. O (∩ _ ∩) O thank you!



RelativeLayoutDemo code download: http://download.csdn.net/detail/zpj779878443/8334001




























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.