Android入门基础(二)

xiaoxiao2021-02-28  81

一、android五大布局 1、线性布局LinearLayout

LinearLayout是线性布局控件,她包含的子控件将以横向或竖向方式排列。

常用属性: android:orientation=”“——决定子类控件的排布方式 android:gravity=”“——决定子类的xy位置 常用的属性值: center_vertical:垂直(Y轴)居中 center_horizontal:水平(X轴)居中 center:水平垂直都居中 right:子类控件位于当前布局的右边 bottom:子类控件位于当前布局的下面 如:android:gravity="bottom/center_horizontal"

子类控件在LinearLayout中常用属性 android:layout_gravity=”bottom”——指本身在当前父容器的XY的一个位置 android:layout_weight=”1”——指本身控件占当前父容器的一个比例 【注意】 android:layout_weight 与 android:layout_height,如

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_weight="2" android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button1" /> <Button android:layout_weight="1" android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button2" /> </LinearLayout>

当android:layout_height="wrap_content"时,

当android:layout_height="match_parent"时

2、相对布局RelativeLayout

RelativeLayout是相对布局控件,它包含的子控件将以控件之间的相对位置或者子类控件相对父类容器的位置的方式排列

子类控件在RelativeLayout中常用属性(相对父容器的一个位置) android:layout_alignParentLeft=”true”子类控件相对当前父类容器靠左边 android:layout_alignParentTop=”true”子类控件相对父类容器靠上边 android:layout_marginLeft=”41dp”子类控件距离父类容器左边的距离 android:layout_marginTop=”33dp”子类控件距离父类容器上边的距离 android:layout_centerInParent=”true”子类控件相对当前父类容器即水平居中又垂直居中 android:layout_centerHorizontal=”true”子类控件相对当前父类容器水平居中 android:layout_centerVertical=”true”子类控件相对当前父类容器垂直居中

子类控件相对子类控件的一个位置 android:layout_below=”@+id/button1”该控件位于给定id控件的底部 android:layout_toRightOf=”@+id/button1”该控件位于给定id控件的右边 android:layout_above=”@+id/button1”该控件位于给定id控件的上面 android:layout_toLeftOf=”@+id/button1”该控件位于给定id控件的左边 android:layout_alignBaseline=”@+id/button1”该控件的内容与给定id控件的内容在一条线上 android:layout_alignBottom该控件的底部边缘与给定id控件的底部边缘对齐 android:layout_alignLeft该控件的左边缘与给定id控件的左边缘对齐

3、帧布局FrameLayout 帧布局中,所有的子元素都不能被指定放置位置,它们统统放在这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ProgressBar android:layout_gravity="center" android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_gravity="center" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="80%" /> </FrameLayout>

4、绝对布局AbsoluteLayout 绝对布局又叫坐标布局,可以直接指定子元素的绝对位置(xy),由于手机屏幕尺寸差别较大,使用绝对定位的适应性会比较差,在屏幕的适配上有缺陷,这里我们不多加深究。 AbsoluteLayout子类控件的属性: android:layout_x=”30dp”——控制当前子类控件的x位置 android:layout_y=”30dp”——控制当前子类控件的y位置

5、表格布局TableLayout

TableLayout表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象TableLayout的属性(全局属性) android:collapseColumns=”1,2”隐藏从0开始的索引列,列直接必须用逗号隔开:1,2,5 android:shrinkColumns=”1,2”收缩从0开始的索引列,当可收缩的列太宽(内容过多)不会被挤出屏幕,列直接必须用逗号隔开:1,2,5。你可以通过”*”代替收缩所有列。注意一列能同时表示收缩和拉伸。 android:stretchColumns=”1,2”拉伸从0开始的索引列,以填满剩下的多余空白空间,列直接必须用逗号隔开:1,2,5。你可以通过”*”代替收缩所有列。注意一列能同时表示收缩和拉伸。TableLayout的局部属性(内部控件所用属性) android:layout_column=”1”该控件显示在第2列 android:layout_span=”2”该控件占据2列

二、Activity

Activity是一个应用程序组件,提供用户与程序交互的界面Android四大组件:Activity、Service、BroadcastReceiver、Content ProviderActivity的创建使用:继承Android的Activity类、重写方法、设置显示布局、在AndroidManifest文件中,注册ActivityActivity的生命周期 onCreate();创建 onStart();运行 onResume();获取焦点 onPause();失去焦点 onStop();暂停 onDestroy();销毁 onReStart();

Activity的四种状态 活动状态(Active/Running)Activity处于页面最顶端,获取焦点 暂停状态(Paused)Activity失去焦点,但对客户可见 停止状态(Stopped)Activity被完全遮挡,但保留所有状态和成员信息 非活动状态(Killed)Activity被停止

Activity生命周期的执行过程

三、页面跳转

Intent实现页面跳转

Intent可以理解为信使(直译为意图),由 Intent来协助完成Android各个组件间的通讯

Intent实现页面跳转方法:

1> startActivity(Intent)

2> startActivityForResult(intent,requestCode); 与onActivityResult()和setResult()方法连用 实例在下一篇博客(●’◡’●)

转载请注明原文地址: https://www.6miu.com/read-79209.html

最新回复(0)