[Android學習筆記1]Activity+Layout+Button

來源:互聯網
上載者:User

線性布局(LinearLayout)是指view對象在父view中可按水平或垂直方向線性排列。

相對布局(RelativeLayout)是指view對象的排列依賴於各對象之間的相對位置。

下面是展示兩者的小例子,同時展示如何啟動一個新的Activity和監聽Button按鍵事件的方式。

 AndroidManifest.xml檔案:

<?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       //LinearLayout布局的Activity                      android:name="com.luoye.layout.Linear"            android:label="Linear" >        </activity>                <activity      //RelativeLayout布局的Activity            android:name="com.luoye.layout.Relative"            android:label="Relative" >        </activity>                    </application></manifest>

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

 線性布局的布局檔案:

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

相對布局的布局檔案:

<?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 menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}         //實現Button的onClick事件,在布局檔案中Button元素中聲明了public void onClickListener(View v){Intent intent = new Intent();switch(v.getId())    //按鍵來源判斷{case R.id.button1:intent.setClass(this, Linear.class);break;case R.id.button2:intent.setClass(this, Relative.class);break;}startActivity(intent);   //啟動對應布局的新的Activity}}

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);}}

主面板效果:

點擊LinearLayout按鈕:

點擊RelativeLayout按鈕:


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.