zoukankan      html  css  js  c++  java
  • [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按钮:




     

  • 相关阅读:
    设计模式(Design Pattern)扫盲
    SharePoint 2007 采用表单验证 (1) 失败:(
    发布一款给图片批量加水印的程序PicNet V1.0
    转篇文章,VS2005开发的dll如何安装进GAC
    cnblogs排名进入1500,纪念一下
    转载一篇提高baidu/google收录的文章
    关于.NET(C#)中字符型(Char)与数字类型的转换, CLR via c# 读书笔记
    《天风文章》V1.2.0 新闻/文章类asp.net2.0站点系统源码 (100%开源)
    推荐个.Net的论坛系统 Discuz!NT
    C#实现对图片加水印的一段代码.
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3194310.html
Copyright © 2011-2022 走看看