Advanced article-user interface: Basic layout in 3.android-layout

Source: Internet
Author: User

1.RelativeLayout

in Relativelayout, a component can be positioned based on the parent boundary, and in addition to such positioning, it can be positioned relative to other controls. The feature is that you can drag space wherever you like, and then stop where you are.

Add child objects to the code:

Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;Importandroid.widget.RelativeLayout;ImportAndroid.widget.TextView; Public classMain2activityextendsappcompatactivity {Privaterelativelayout Root; PrivateTextView TV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Root=NewRelativelayout ( This);//Initialize a relativelayoutSetcontentview (root);//make the activity load this layoutTV=NewTextView ( This); Tv.settext ("Android Entry Step"); Relativelayout.layoutparams LP=NewRelativelayout.layoutparams (relativelayout.layoutparams.wrap_content,relativelayout.layoutparams.wrap_content) ;//creating a layout Parameter objectLp.leftmargin= 200;//adjust the position of the TextViewLp.topmargin = 500; Root.addview (TV,LP);//add TextView to Layout    }}

2.FramLayout

framlayout features can overlap regardless of how many control objects are dragged in. And only nine positions can be placed. Its features and functions can be achieved in relativelayout, but the framlayout is more lightweight and easy to use.

For example, place two images in a framlayout, and click on the images to switch between each other.

Java source code:

Importandroid.support.v4.app.Fragment;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;Importandroid.widget.FrameLayout;ImportAndroid.widget.ImageView; Public classMain2activityextendsappcompatactivity {Privateframelayout Root; PrivateImageView IVA; PrivateImageView IVB; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_main2); Root= (framelayout) Findviewbyid (r.id.root);//get to Parent container framlayout by IDIVA = (ImageView) Findviewbyid (R.id.iva);//Get ComponentsIVB =(ImageView) Findviewbyid (R.ID.IVB); Root.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {//Click the toggle to perform the image               if(iva.getvisibility () = =view.visible)               {SHOWB (); }Else{ShowA ();    }            }        }); }    Private voidShowA () {//How to display a pictureiva.setvisibility (view.visible);    Ivb.setvisibility (view.invisible); }    Private voidShowb () {//How to display B picturesiva.setvisibility (view.invisible);    Ivb.setvisibility (view.visible); }}

XML Source code

<?XML version= "1.0" encoding= "Utf-8"?><Framelayoutxmlns: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"Android:id= "@+id/root"Tools:context= "Com.example.lzc.myapplication.Main2Activity">    <ImageViewAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:src= "@drawable/img1"Android:id= "@+id/iva"/>    <ImageViewAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:src= "@drawable/img1"Android:id= "@+id/ivb"/></Framelayout>

3.LinearLayout

Linerlayout is a linear layout in Android. The horizontal or vertical direction of its child objects is lined up. The attribute android:orientation can control the direction of the arrangement. Android:orientation= "Vertical" is arranged vertically. Android:orientation= "Horizontal" is arranged horizontally. There is an important attribute in the linear layout for the specific gravity. That is, layout_weight, which specifies the proportions of the partition parent container.

If the two control, control A has layout_weight, control B does not, then the width of B is its own width, the rest of the space for a.

Example layout:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal">        <EditTextAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"/>        <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Go"/>    </LinearLayout>    <WebViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"></WebView></LinearLayout>

Advanced article-user interface: Basic layout in 3.android-layout

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.