[Android学习笔记1]Activity+Layout+Button
来源:互联网 发布:jq 数组是否包含 编辑:程序博客网 时间:2024/05/17 06:38
线性布局(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按钮:
- [Android学习笔记1]Activity+Layout+Button
- Android layout学习笔记
- Android学习笔记:Button
- android *** Layout 05 button
- Android学习笔记--Tab Layout
- android笔记 layout文档学习
- android学习笔记之layout
- android 学习笔记 1 activity
- android学习笔记1(activity)
- Android学习笔记1:Activity
- OPhone/Android的学习(1)—初步知识,TextView,Button,Layout及事件响应
- Android学习笔记----Activity
- android学习Activity笔记
- Android学习笔记-Activity
- Android 学习笔记--Activity
- 学习笔记-setContentView错误 找不到R.layout.activity
- Android学习笔记---Layout的一些理解
- android学习笔记之layout篇
- 传智播客Java自学笔记第15天(待编辑)
- 传智播客Java自学笔记第16天(待编辑)
- Knockout 新版应用开发教程之Computed Observables
- NYOJ 一笔画问题
- 传智播客Java自学笔记第17天(待编辑)
- [Android学习笔记1]Activity+Layout+Button
- 传智播客Java自学笔记第18天(待编辑)
- ubuntu重启apache
- 【Java】switch语句的参数类型
- 传智播客Java自学笔记第19天(待编辑)
- 0-1背包问题
- NYOJ 最少步数
- STM32学习顺序
- 传智播客Java自学笔记第20天(待编辑)