Linearlayout refers to the linear arrangement of view objects in the parent view in the horizontal or vertical direction.
Relativelayout refers to the arrangement of view objects dependent on the relative positions between objects.
The following example shows how to start a new activity and listen to button events.
Androidmanifest. xml file:
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. luoye. layout "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "Android: targetsdkversion = "17"/> <application Android: allowbackup = "true" Android: icon = "@ drawable/ic_launcher" Android: Label = "@ string/app_name" Android: theme = "@ style/apptheme"> <activity Android: Name = "com. luoye. layout. mainactivity "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> <activity // activity Android: Name =" com. luoye. layout. linear "Android: Label =" linear "> </activity> <activity // activity Android: Name =" com. luoye. layout. relative "Android: Label =" relative "> </activity> </Application> </manifest>
Layout file of the main acitvity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Layout Show" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="LinearLayout" android:onClick="onClickListener" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RelativeLayout" android:onClick="onClickListener" /></LinearLayout>
Linear layout file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:background="#FF0000" /> <TextView android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:background="#0000FF" /> <TextView android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:background="#FFFF00" /> <TextView android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:background="#008000" /> </LinearLayout>
Layout file of relative layout:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv1" android:text="Type here:" android:paddingLeft="16dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/et1" android:layout_below="@id/tv1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="16dp" android:paddingRight="16dp" /> <Button android:id="@+id/ok" android:layout_below="@id/et1" android:layout_alignParentRight="true" android:layout_marginRight="16dp" android:text="ok" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/cancel" android:layout_toLeftOf="@id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/ok" android:text="cancel" /> </RelativeLayout>
Mainactivity. Java
Package COM. luoye. layout; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. menu; import android. view. view; public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true;} // implement The onclick event of the button. In the layout file, the button element declares public void onclicklistener (view V) {intent = new intent (); Switch (v. GETID () // click the source to judge {case R. id. button1: intent. setclass (this, linear. class); break; case R. id. button2: intent. setclass (this, relative. class); break;} startactivity (intent); // start the new activity of the corresponding layout }}
Linear. Java
package com.luoye.layout;import android.app.Activity;import android.os.Bundle;public class Linear extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.linear);}}
Relative. Java
package com.luoye.layout;import android.app.Activity;import android.os.Bundle;public class Relative extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.relative);}}
Main panel effect:
Click the linearlayout button:
Click relativelayout: