安卓初级入门

一节
安卓布局:
线性布局(关键字:LinearLayout )
其中有个属性叫orientation,是用来设置屏幕是垂直的,还是水平的(垂直:vertical,水平:horizontal)
布局中总会有一些控件:例如button ,table 他们会有长宽高
在安卓中,宽度是layout_width,长度layout_height,
值也分为了两种表现形式,一种是wrap_content对应自己本身内容,一种是match_parent和父级布局一样的宽度
text=“按钮2”,为控件给值
例子:有两个button其中一个
Button 的属性 android:layout_width=“0dp”,你就将 android:layout_weight=“1”
另外一个 Button
android:layout_width=“0dp” android:layout_weight="1"会使两个button在界面上展现成对称大小的样式
android:layout_weight 是权重,占界面的几分之几的比重

<?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="horizontal"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="按钮1"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="按钮2"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="按钮4"/>
    </LinearLayout>
</LinearLayout>

二节

  1. Android的基本组件
    1.1 Activity
    1.1.1 Activity代表手机的一个屏幕
    1.1.2 一个Android程序由多个Activity组成,即:一个Android程序由多屏内容组成
    1.1.3 Activity相当于一个展板,本身没有可视的内容,你把画(View)贴到展板上就可以展示出来了
    1.2 Service
    1.2.1 不可见的组件
    1.2.2 在后台运行
    1.2.3 为其他组件提供后台服务
    1.2.4 监控其他组件的运行状态
    1.2.5 后台播放音乐
    1.3 BroadcastReceiver:广播消息监听器
    1.3.1 作用:用来监听广播,eg:收短信的广播 往手机推送消息
    1.4 ContentProvider:内容提供者
    1.4.1 帮助不同的应用程序间交换数据,eg:获取通讯录信息
    1.5 Intent和IntentFilter 意图
    Activity、Service、BroadcastReceiver之间通信的载体,传递数据

  2. View和ViewGroup介绍
    View类似于swing中的JPanel,代表一个空白的矩形区域,Android应用中的绝大部分UI组件都继承于View或其子类
    View还一个重要的子类ViewGroup,它通常作为其它组件的[容器]使

    简单来讲View是所有控件的父控件,ViewGroup可以包含其它的View对象,并且是所有布局控件的父类
    Activity相当于一个展板,本身没有可视的内容,你把画(View)贴到展板上就可以展示出来了
    setContentView(view)

  3. 自定义视图(onDraw)
    3.1 颜色四种设置方式
    canvas.drawColor(Color.RED);//系统颜色(Color.RED)

    canvas.drawColor(Color.rgb(100, 150, 200));//rgb
    canvas.drawColor(Color.argb(100, 100, 150, 200));//argb,其中第一参数为透明度
    canvas.drawColor(Color.parseColor("#44E21D"));//16进制
    3.2 画板背景
    canvas.drawColor(Color.BLUE)
    3.3 画笔设置
    3.3.1 实心或空心
    paint.setStyle(Style.FILL);//实心
    paint.setStyle(Style.STROKE);// 空心
    3.3.2 粗细
    paint.setStrokeWidth(10)
    3.3.3 抗锯齿
    paint.setAntiAlias(true);
    3.3.4 颜色设置
    paint.setColor(Color.parseColor("#103864"));

  4. 画图形
    4.1 画圆
    canvas.drawCircle(200, 100, 100, paint)
    4.2 画矩形
    canvas.drawRect(0, 200, 200, 400, paint);
    canvas.drawRect(rect, paint);
    左上角、右下角
    4.3 画多边形(三角形)
    4.3.1 drawLine方式
    4.3.2 drawPath方式
    Path
    moveTo(float x, float y):将起始点设置到x,y点
    lineTo(float x, float y):从当前点绘制一条线段到x,y点
    reset():清除path设置的所有属性
    close():回到初始化形成封装的曲线
    4.3.3 如果画笔为实心,画线与Path方式的区别
    drawLine=空心,drawPath=实心
    4.5 画文字
    drawText()
    paint.setTextSize(50);// 设置文字大,单位为dp
    4.6 画图
    drawBitmap 位图
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.qq);
    注意事项:图片名不能是汉字或空格

  5. 什么时候要子类中通过super再调用父类的方法呢?(重要重要)
    如果父方法中有代码就要如此~如果父方法中有代码就要如此~如果父方法中有代码就要如此
    重要的事情说三遍,例如:onDraw可以不用再重新调用super.onDraw(),因为父方法中一行代码都没有

    注1:其实最简单的办法就是不管三七二十一,都调用super.xxx()方法即可

安卓工程目录
模块
manifests
仅有一个文件,系统清单文件,相当于web.xml
java
放源文件
res(放系统资源文件,一个很重要的的类R,自动生成,resource的缩写)
drawable(放图片资源)
layout(放布局文件)

dp(密度,相当于width中的百分比)
sp(密度,相当于width中的的数值xxx.px)

package com.example.t216_android01;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class myView extends View {

    private Paint paint;
    public myView(Context context) {
        super(context);
        paint=new Paint();//笔,实例化一支笔
        paint.setColor(Color.GREEN);//设置笔所画东西的颜色
//        paint.setStyle(Paint.Style.STROKE);//设置圆的空心或者实心
//        paint.setAntiAlias(true);//设置抗锯齿
        paint.setStrokeWidth(10);//设置粗细
    }

    @Override
    protected void onDraw(Canvas canvas) {//画图形的面板
        super.onDraw(canvas);
//        canvas.drawCircle(100,150,50,paint);//画圆的方法,cx,cy坐标位置 radius是半径
//        canvas.drawRect(100,100,300,400,paint);//画矩形的方法
        canvas.drawLine(100,100,100,100,paint);//画直线的方法
        canvas.drawLine(100,100,100,100,paint);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml文件里其实是默认的 为了让大家更容易理解,写了一个简单的Demo.步骤如下: 第1步:新建一个Android工程ApplicationDemo,目录结构如下: 第2步:新建一个工具类ToolsUtil.java,代码如下 package com.tutor.application; import android.content.Context; import android.widget.Toast; /** * @author carlshen. * 应用的一些工具类. */ public class ToolUtils { /** * 参数带Context. * @param context * @param msg */ public static void showToast(Context context,String msg){ Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } /** * 调用全局的Context. * @param msg */ public static void showToast(String msg){ Toast.makeText(MainApplication.getContext(), msg, Toast.LENGTH_SHORT).show(); } } 第3步:新建一个View命名为MainView.java就是我们Activity现实的View.代码如下: package com.tutor.application; import android.app.Activity; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; /** * @author carlshen. * 自定义的MainView. */ public class MainView extends FrameLayout implements View.OnClickListener{ private Context mContext; private Activity mActivity; /** * 参数Button. */ private Button mArgButton; /** * 全局Button. */ private Button mGlobleButton; /** * 退出Button. */ private Button mExitButton; public MainView(Context context){ super(context); setupViews(); } public MainView(Context context, AttributeSet attrs) { super(context, attrs); setupViews(); } private void setupViews(){ //获取View的上下文. mContext = getContext(); //这里将Context转换为Activity. mActivity = (Activity)mContext; LayoutInflater inflater = LayoutInflater.from(mContext); View v = inflater.inflate(R.layout.main, null); addView(v); mArgButton = (Button)v.findViewById(R.id.arg_button); mGlobleButton = (Button)v.findViewById(R.id.glo_button); mExitButton = (Button)v.findViewById(R.id.exit_button); mArgButton.setOnClickListener(this); mGlobleButton.setOnClickListener(this); mExitButton.setOnClickListener(this); } public void onClick(View v) { if(v == mArgButton){ ToolUtils.showToast(mContext, "我是通过传递Context参数显示的!"); }else if(v == mGlobleButton){ ToolUtils.showToast("我是通过全局Context显示的!"); }else{ mActivity.finish(); } } } 这里MainView.java使用的布局main.xml代码如下: <?xml version="1.0" encoding="utf-8"?> 第4步:修改ApplicationDemoActivity.java,代码如下: package com.tutor.application; import android.app.Activity; import android.os.Bundle; public class ApplicationDemoActivity extends Activity { private static Context aContext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainView mMainView = new MainView(this); setContentView(mMainView); aContext = getApplicationContext(); } /**获取Context. * @return */ public static Context getContext(){ return aContext; } } 第5步:运行上述工程效果如下:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值