# Android

# 1. Java基础知识

# 2. Android 开发环境搭建

设置好 Android Studio 开发环境,在 Android Studio 中创建一个新的项目,了解 Android Studio 的基本使用方法。

# 3. Android 应用结构

了解 Android 应用的基本结构,包括布局文件(XML)、活动(Activity)、服务(Service)、广播接收器(BroadcastReceiver)和内容提供程序(Content Provider) 等概念。

# 4. 布局文件

掌握 XML 布局的语法,如LinearLayoutRelativeLayoutFrameLayout等。

# 5. 视图控件

TextViewEditTextButtonImageViewRecyclerView等。

# 6. 活动和Intent

学习活动和Intent的概念,如何使用Intent进行界面之间的数据传递。

# 7. 数据库操作

学习如何使用SQLite数据库进行数据的增删改查操作。

# 8. 网络编程

学习如何使用HttpUrlConnectionOkHttp等库进行网络编程,包括发送和接收数据。

# 9. 多媒体操作

学习如何使用MediaPlayerSoundPool等库进行音频和视频的播放,如何使用Camera API实现照片的拍摄。

# 10. 线程和异步任务

学习如何使用线程和异步任务处理耗时操作,避免在主线程中执行耗时的操作导致程序出现ANR(应用无响应)。

# 记录

  • 设置activity的布局

    setContentView(R.layout.activity_view);
    
    1
  • 获取某个布局

    Button button = findViewById(R.id.button);
    
    1
  • activity跳转

    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, MainActivity2.class);
        startActivity(intent);
      }
    })
    
    1
    2
    3
    4
    5
    6
    7
    8
  • 设置文本组件的内容

    TextView text = findViewById(R.id.text);
    text.setText("你好");
    
    1
    2
  • java代码中设置视图的宽高

    ViewGroup.LayoutParams params = view.getLayoutParams();
    params.width = Utils.dp2px(this, 300); // 默认px单位,需要把dp转为px
    view.setLayoutParams(params);
    
    1
    2
    3
  • RelativeLayout

    相对位置属性 说明
    layout_toLeftOf 在指定视图的左边
    layout_toRightOf 在指定视图的右边
    layout_above 在指定视图的上方
    layout_below 在指定视图的下方
    layout_alignLeft 与指定视图的左侧对齐
    layout_alignRight 与指定视图的右侧对齐
    layout_alignTop 与指定视图的顶部对齐
    layout_alignBottom 与指定视图的底部对齐
    layout_centerInParent 在上级视图的中间
    layout_centerHorizontal 在上级视图的水平方向居中
    layout_centerVertical 在上级视图的垂直方向居中
    layout_alignParentLeft 与上级视图的左侧对齐
    layout_alignParentRight 与上级视图的右侧对齐
    layout_alignParentTop 与上级视图的顶部对齐
    layout_alignParentBottom 与上级视图的底部对齐
  • Button 和 TextView 的区别

    • Button 有默认背景,TextView 无默认背景;
    • Button 文本默认居中对齐,TextView 文本默认左对齐;
    • Button 默认将英文转为大写,TextView 保持原本的英文大小写;
  • Button 和 ImageButton 的区别

    • Button可显示文本和图片,ImageButton只能显示图片;
    • Button通过背景设置的图像会拉伸变形,ImageButton上的图像可自由调整缩放;
    • Button只能通过背景显示一张图像,ImageButton可通过前景和背景显示两张图片;

# 工具类

public class Utils {

    public static int dp2px(Context context, float dpValue) {
        // 获取设备像素密度
        float scale = context.getResources().getDisplayMetrics().density;
        // 四舍五入取整
        return (int)(dpValue * scale + 0.5f);
    }
}
1
2
3
4
5
6
7
8
9
上次更新: 2023/11/27 19:07:10