展会信息港展会大全

【Android应用层学习】-Intent和Layout
来源:互联网   发布日期:2015-11-15 09:56:38   浏览:1638次  

导读:Android切换显示窗体-Intent 看书-学习-实操-总结-消化 Intent的基本作用 首先,Android应用程序是由数个Activity所组成的,当用户切换显示窗体时,就需要调用Intent类来完成。 Intent可以带有数据,简单地说,它是android设备不同组件连接的桥梁。 Intent用...

Android切换显示窗体-Intent

看书-学习-实操-总结-消化

Intent的基本作用

首先,Android应用程序是由数个Activity所组成的,当用户切换显示窗体时,就需要调用Intent类来完成。

Intent可以带有数据,简单地说,它是android设备不同组件连接的桥梁。

Intent用法说明

Intent使用分为显式和隐式两种。

1.显式Intent启动Activity

显式启动Activity,通常就是在一个应用程序中由其中Activity启动另一个Activity。每个Activity都要在manifest当中给以定义。

显式启动一个Activity的过程为:创建一个新的Intent,并指定当前Activity和要启动的Activity,进而使用startActivity(Intent intent)方法进行启动。将这个过程添加在操作代码中(如button,menuItem等的响应部分)

例如,从Activity01切换到Activity02

Intent intent = new Intent();

intent.setClass(Activity01.this,Activity02.class);

Activity01.this.startActivity(intent);

上面这代码是不带数据的跳转,下面以一个简单的例子记录数据在Activity中的传递方法。

Activity01传入string数据

Intent intent = new Intent();

intent.putExtra("TEXT",text);

intent.setClass(Activity01.this,Activity02.class);

Activity01.this.startActivity(intent);

在Activity02中接受数据

Intent intent = getIntent();

String str = intent.getStringExtra("TEXT");

2.隐式Intent启动Activity

隐式调用Intent首先需要了解intent的携带的一个重要参数action,在android.intent.action下包括许多Activity方法,比如VIEW,WEB_SEARCH,CALL等等。系统会根据所指定的action来自动去匹配最合适的程序Activity来执行该动作。

例如android源码中Music应用程序的VideoBrowserActivity.java中,当点击列表中的某个视频文件后,跳转去调用系统自带播放器播放。

Intent intent = new Intent(Intent.ACTION_VIEW);

mCursor.moveToPosition(position);

String type = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));

intent.setDataAndType(ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id), type);

Log.i( "VIDEOFILENAME", mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE) ) );

startActivity(intent);

--------------------------------------------------------------------------------

Layout

关于layout, 在此记录下常用的三种:

1.LinearLayout

LinearLayout分为vertical和horizontal两种

不同的layout可以嵌套使用

例子

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<LinearLayout

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

<TextView

android:text="red"

android:gravity="center_horizontal"

android:background="#aa0000"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"/>

<TextView

android:text="green"

android:gravity="center_horizontal"

android:background="#00aa00"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"/>

<TextView

android:text="blue"

android:gravity="center_horizontal"

android:background="#0000aa"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"/>

<TextView

android:text="yellow"

android:gravity="center_horizontal"

android:background="#aaaa00"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"/>

</LinearLayout>

<LinearLayout

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

<TextView

android:text="row one"

android:textSize="15pt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

<TextView

android:text="row two"

android:textSize="15pt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

<TextView

android:text="row three"

android:textSize="15pt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

<TextView

android:text="row four"

android:textSize="15pt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

</LinearLayout>

</LinearLayout>

一个LinearLayout中嵌套水平和垂直两种LinearLayout排列

2.TableLayout

例子

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

<LinearLayout

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

<TextView

android:text="red"

android:gravity="center_horizontal"

android:background="#aa0000"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1" />

<TextView

android:text="green"

android:gravity="center_horizontal"

android:background="#00aa00"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1" />

<TextView

android:text="blue"

android:gravity="center_horizontal"

android:background="#0000aa"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1" />

<TextView

android:text="yellow"

android:gravity="center_horizontal"

android:background="#aaaa00"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1" />

</LinearLayout>

<LinearLayout

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

<TableLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:stretchColumns="0">

<TableRow>

<TextView

android:text="@string/row1_column1"

android:padding="3dip" />

<TextView

android:text="@string/row1_column1"

android:padding="3dip"

android:gravity="center_horizontal">

</TextView>

<TextView

android:text="@string/row1_column2"

android:gravity="right"

android:padding="3dip" />

</TableRow>

<TableRow>

<TextView

android:text="@string/row2_column1"

android:padding="3dip" />

<TextView

android:text="@string/row2_column2"

android:gravity="right"

android:padding="3dip" />

</TableRow>

</TableLayout>

</LinearLayout>

</LinearLayout>

3.RelativeLayout

相对布局的布局方式是设定单元和单元的上下左右关联,至少要定义两个位置之间的关联。

对于相对布局中可以定义的参数,Mars老师的一个归纳很详细

android:layout_above 将该控件的底部至于给定ID的控件之上

android:layout_below 将该控件的顶部至于给定ID的控件之下

android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐

android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐

android:layout_alignBaseline 该控件的baseline和给定ID的控件的baseline对齐

android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘

android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐

android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐

android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐

android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐

android:layout_alignParentLeft 如果该值为true,则将该控件的左边与父控件的左边对齐

android:layout_alignParentRight 如果该值为true,则将该控件的右边与父控件的右边对齐

android:layout_alignParentTop 如果该值为true,则将空间的顶部与父控件的顶部对齐

android:layout_centerHorizontal 如果值为真,该控件将被至于水平方向的中央

android:layout_centerInParent 如果值为真,该控件将被至于父控件水平方向和垂直方向的中央

android:layout_centerVertical 如果值为真,该控件将被至于垂直方向的中央

google官方的一个例子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:padding="10px" >

<TextView android:id="@+id/label"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Type here:" />

<EditText android:id="@+id/entry"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@android:drawable/editbox_background"

android:layout_below="@id/label" />

<Button android:id="@+id/ok"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/entry"

android:layout_alignParentRight="true"

android:layout_marginLeft="10px"

android:text="OK" />

<Button android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toLeftOf="@id/ok"

android:layout_alignTop="@id/ok"

android:text="Cancel" />

</RelativeLayout>

效果如下

作者:Wayne

赞助本站

人工智能实验室

相关热词: android开发 教程

相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港