[Android笔记1]Activity+Layout+Button

线性布局(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 {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public 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{
	
	@Override
	protected 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{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.relative);
	}
}

主面板效果:

点击LinearLayout按钮:

点击RelativeLayout按钮:




 

 

posted @ 2013-07-16 20:25  坚固66  阅读(240)  评论(0编辑  收藏  举报