疯狂Android讲义-2版-记录

来源:互联网 发布:日程表 知乎 编辑:程序博客网 时间:2024/05/04 14:06

Jvm 编译运行.class文件

Dalvik    把.class文件转成.dx文件运行

BroadcastReceiver组件

    重写onReceiver(context context,intent intent)

    其他组件通过:sendBroadcast(),sendStickyBroadcast(),sendOrderedBroadcast()发送广播消息

    如果BroadcastReceiver 对该消息感兴趣通过(IntentFilter配置),BroadcastReceiver的onReceive()方法将被触发

注册BroadcastReceiver事件

    java代码:Context.registReceiver()

    AndroidManifest.xml文件<receiver.../>元素注册    

ContentProvider内容提供者组件(48Page)

    应用之间数据交换:ContentProvider

    Android为这种应用的数据交换提供一个标准:ContentProvider;

    用户实现自己得ContentProvider时,需实现以下抽象方法

        inser(Uri,ContentValues):向ContentProvider插入数据

        delete(Uri,ContentValues):删除ContentProvider中指定数据

        update(Uri,ContentValues):更新ContentProvider中指定数据

        query(Uri,String[],String,String[],String):查询ContentProvider

    通常配合ContenResolver暴露自己得数据,另一个程序通过ContentPresolver来访问数据

Intent Or IntentFilter

    Activity,Service,BroadcastReceiver 三种组件之间通信通过Intent座位载体

    启动一个Activity时,通过调用:

        Context-startActivity(Intent intent)

        Context-startActivityForResult(Intent intent,int requestCode)

    启动一个Service时,调用:

        Context-startService(Intent intent)

        Context-bindService(Intent service,ServiceConnection conn,int flags)

    启动一个BroadcastReceiver时,调用

        Context-sendBroadcast(Intent intent)

        Context-sendStickyBroadcast(Intent intent)

        Context-sendOrderedBroadcast(Intent intent,String receiverPermission)

    不难看出Intent封装了目标组件信息,通过intent启动或触发另一组件,

        显示Intent:明确指定需要启动或触发的组件类名,系统无需解析Intent,直接找到指定目标组件,启动触发即可

        隐式Intent:只是指定需要启动或者触发组件应满足怎样的条件,系统进行intent解析,解析出触发条件,再去系统中匹配触发条件,然后触发目标组件,被调用的组件通过IntentFilter声明自己所需的条件(隐式Intent)

    Drawable:抽象基类

        |-BitmapDrawable    :代表位图

        |-Drawable.ColorDrawable :代表颜色 

        |-Drawable.ShapeDrawable    : 集合形状Drawable

   ViewGroup:抽象类[主要当做容器类使用,如各种布局管理器]

        ViewGroup容器控制其子组件的布局依赖于ViewGroup.layoutParams 与 ViewGroup.MarginLayoutParmas 两个内部类

       XML属性:

        A:layout_height = [fill_parentmatch_parent|wrap_content]    布局高度

        A:  layout_width = [fill_parentmatch_parent|wrap_content]    布局宽度

        A:  layout_marginBottom : 方法:setMargins(int,int,int,int)指定该子组件下边的页边距

        A:  layout_marginLeft : 方法:setMargins(int,int,int,int)指定该子组件左边页边距

        A:layout_marginRight : 方法:setMargins(int,int,int,int)指定该子组件右边页边距

        A:layout_marginTop : 方法:setMargins(int,int,int,int)指定该子组件上边页边距

    注:DrawView 是一个自定义View 在XML布局文件中添加该组件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/llayout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.showimage.MainActivity" >         <com.example.showimage.DrawView        android:layout_width="match_parent"        android:layout_height="match_parent"        /></LinearLayout>


public class DrawView  extends View{         float currentX = 50;    float currentY = 100;    Paint paint = new Paint();//画笔     public DrawView(Context context) {        super(context);    }         public DrawView(Context context,AttributeSet set) {        super(context);    }         @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);                 paint.setColor(Color.GREEN);        //绘制圆球(X,Y,大小,Paint)        canvas.drawCircle(currentX, currentY, 30, paint);             }         @Override    public boolean onTouchEvent(MotionEvent event) {                 currentX = event.getX();        currentY = event.getY();                 //通知当前组件重绘        invalidate();                 return true;    }}


LinearLayout:[67页介绍]

android:gravity ="

top

buttom

left

right

center

vertical

center_horizontal

fill

clip_vertical

clip_horizontal" 

方法[setGravity(int)] 

android:orientation = "vertical | horizontal" [垂直 | 水平]
android:layout_weight[改元素所占的权重]

TableLayout[表格布局] 继承LinearLayout

android:collapseColumns  方法:setColumnsCollapsed(int,boolean) 设置被隐藏的列的序号,多个序列号逗号隔开

android:shrinkColumns方法:setShrinkAllColumns(boolean)设置允许被收缩的列序号,多个列序号之间逗号隔开

android:stretchColumnssetStretchAllColumns(boolean)设置允许被拉伸的列的列序号,多个列序号逗号隔开

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/llayout"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    tools:context="com.example.showimage.MainActivity" ><!--  <com.example.showimage.DrawView        android:layout_width="match_parent"        android:layout_height="match_parent" />--><!--定义1个表格布局:2列允许收缩,3列允许拉伸  -->    <TableLayout         android:id="@+id/Tlayout1"        android:layout_width="fill_parent"android:layout_height="wrap_content"android:shrinkColumns="1"android:stretchColumns="2"><Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="肚子一行的 Button"    /><TableRow >    <Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="普通"    />    <Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="收缩"    />    <Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="拉伸"    />    </TableRow>            </TableLayout>    <!--定义2个表格布局:2列隐藏  -->    <TableLayout         android:id="@+id/Tlayout2"        android:layout_width="fill_parent"android:layout_height="wrap_content"android:collapseColumns="1"><Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="2列被隐藏:collapseColumns=1"    />        <TableRow >             <Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="TL2普通"    />    <Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="TL2收缩"    />    <Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="TL2拉伸"    />                 </TableRow>    </TableLayout>    <!--定义3个表格布局:指定第2,3列可以被拉伸  -->    <TableLayout         android:id="@+id/Tlayout3"        android:layout_width="fill_parent"android:layout_height="wrap_content"android:stretchColumns="1,2"><Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="Tlayout 3 Button"    /><TableRow >             <Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="TL3普通"    />    <Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="TL3收缩"    />    <Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="TL3拉伸"    />                 </TableRow>        <TableRow >             <Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="TL3-3普通"    />    <Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="TL3-3拉伸"    />                 </TableRow>            </TableLayout></LinearLayout>



FrameLayout:帧布局 继承于ViewGroup[75页]

android:foreground    方法:setForeground(Drawable)设置该帧布局容器的前景图像

android:foregroundGravity 方法 setforegroundGravity(int)定义绘制前景图像gravity属性

RelativeLayout:相对布局

android:gravity  方法:setGravity(int)设置该布局容器内子组件的对其方式

adnroid:ignoreGravity 方法:setIgnoreGravity(int)设置哪个组件不受gravity属性影响

-----为了控制RelativeLayout个子组件的布局分部,提供一个内部类 RelativeLayout.LayoutParams:[true|false]

android:layout_centerHorizontal该子组件是否位于布局容器水平居中

android:layout_centerVertical该子组件是否位于布局容器垂直居中

android:layout_centerInParent该子组件是否位于布局容器中央位置

android:layout_alignParentBottom该子组件是否与布局容器低端对其

android:layout_alignParentLeft该子组件是否与布局容器左边对齐

android:layout_alignParentRight该子组件是否与布局容器右边对齐

android:layout_alignParentTop该子组件是否与布局容器顶端对齐

-----RelativeLayout.LayoutParams 里属性值为其他 UI 组件 ID 的 xml 属性

android:layout_toRightOf="组件ID"该组件在ID组件右侧

android:layout_toLeftOf="组件ID"该组件在ID组件左侧

android:layout_above="组件ID"该组件在ID组件上方

android:layout_below="组件ID"该组件在ID组件下方

android:layout_alignTop="组件ID"该组件与ID组件上边界对齐

android:layout_alignBottom="组件ID"该组件与ID组件下边界对齐

android:layout_alignLeft="组件ID"该组件与ID组件左边界对齐

android:layout_alignRight="组件ID"该组件与ID组件右边界对齐

GridLayout:网格布局 4.0加入[77Page]

xml加入:定义一个6*4的GridLayout

A:alignmentMode 方法:setAlignmentMode

A: rowCount ="6" 6行

A: columnCount="4" 4列

A: layout_columnSpan="4" 内部单个组件占据的列数

<?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:columnCount="4"    android:rowCount="6"     android:id="@+id/gridlayoutid">    <!-- 跨4列文本框,设置前景色,后景色等属性 -->    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="right"        android:layout_marginLeft="4px"        android:layout_marginRight="4px"        android:layout_columnSpan="4"        android:padding="5px"        android:text="0"        android:textSize="50sp"        android:textColor="#000"        android:background="#eee"        />    <!-- 跨4列 清楚按钮 -->    <Button         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_columnSpan="4"        android:text="Clear"        android:gravity="center_horizontal"        /></GridLayout>
public class GridLayoutTest extends Activity {GridLayout gridLayout;String[] chars = new String[]{"7","8","9","/","4","5","6","*","1","2","3","-",".","0","=","+",};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.gridlayout);gridLayout = (GridLayout) findViewById(R.id.gridlayoutid);for (int i = 0; i < chars.length; i++) {Button bn = new Button(this);bn.setText(chars[i]);bn.setTextSize(40);//指定组件所在行GridLayout.Spec rowSpec = GridLayout.spec(i/4+2);//指定组件所在列GridLayout.Spec columnSpec = GridLayout.spec(i%4);GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec,columnSpec);params.setGravity(Gravity.FILL);gridLayout.addView(bn,params);}}}





0 0
原创粉丝点击