androidstudio基础笔记(代码片段)

www? www?     2023-01-14     722

关键词:


Android studio程序及jdk:

提取码:dfjk

Android studio常用快捷键:

一、安装后修改配置

(1)第一种修改方式:

下面两个博客可以一次性解决版本不同的问题,可以参考一下

参考1:

参考2:

(2)第二种修改方式:

*每次创建项目都要重新修改

二、用户界面基础

(1)UI界面

​ UI(User Interface)是介于用户与硬件而设计彼此之间互动沟通的相关软件。
​ 目的在于用户能够方便有效率地去操作硬件以达成双向互动,完成希望借助硬件完成的工作。
​ 在Android应用中,界面是由布局和控件组成的。

(2)View视图

​ 所有的UI元素都是通过View与ViewGroup构建的。对于一个Android应用的用户界面来说,ViewGroup作为容器盛装界面中的控件,它可以包含普通的View控件,也可以包含ViewGroup。

​ Android应用的每个界面的根元素必须有且只有一个ViewGroup容器。

(3)常用资源存储

(4)Android程序结构


三、界面布局

​ Android系统提供的6种常用布局直接或者间接继承自
ViewGroup,因此它们也支持在ViewGroup中定义的属性,
这些属性可以看作是布局的通用属性。

(1)线性布局(LinearLayout)

​ 线性布局(LinearLayout)主要以水平或垂直方式来显示界面中的控件。

设置控件排列方式:

“vertical”控件垂直排列时,显示顺序依次为从上到下。

”horizontal“控件水平排列时,显示顺序依次为从左到右

android:orientation="vertical"

例如:(textAllCaps=“false” 设置字母小写)

(2)单帧布局(FrameLayout)

​ 帧布局(FrameLayout)用于在屏幕上创建一块空白区域,添加到该区域中的每个子控件占一帧,这些帧会一个一个叠加在一起,后加入的控件会叠加在上一个控件上层
所有控件都默认显示在屏幕左上角。

定义格式:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    属性 ="属性值">
</FrameLayout>

例如:

(3)表格布局(TableLayout)

​ 采用行、列的形式来管理控件,它不需要明确声明包含多少行、多少列,而是通过在TableLayout布局中添加TableRow布局来控制表格的行数,通过在TableRow布局中添加控件来控制表格的列数。

定义格式:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             属性 = ”属性值”>
             <TableRow>
UI控件
             </TableRow>
                 ......
</TableLayout>


(4)相对布局(RelativeLayout)

​ 相对布局(RelativeLayout)是通过相对定位的方式指定子控件
位置,即以其它控件或父容器为参照物,摆放控件位置。

定义格式:

 <RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
属性 = "属性值"
......>
</RelativeLayout>

控件位置属性:

例如:

(5)坐标布局(AbsoluteLayout)

​ 坐标布局的子控件需要指定相对于此坐标布局的横、纵坐标值,
否则将会像FrameLayout那样被排在左上角。手机的应用程序需
要适用于不同的屏幕大小,而这种布局模型不能自适应屏幕尺
寸大小,因此应用的比较少

定义格式:

     <AbsoluteLayout   mlns:android="http://schemas.android.com/apk/res/android"
属性 = "属性值"
......>
</AbsoluteLayout>

例如:

(6)网格布局(GridLayout)

​ 网格布局将整个界面划分成rows*columns个网格。每个网格可
以设置一个组件。除此之外,也可以设置一个组件横跨多少列,
一个组件纵跨多少行。提供了setRowCount(int)和
setColumnCount(int)方法来控制网络的行数量和列数量。该布局
中的子控件可以不用设置宽度和高度。

定义格式:

    <GridLayout   mlns:android="http://schemas.android.com/apk/res/android"
属性 = "属性值"
......>
</GridLayout>

常用属性:

例如:

四、界面控件

(1)TextView以及子类

TextView的子类: CheckedTextView、EditText、Button。
Button的子类:CheckBox,RadioButton

1.常用属性:


*single:单行模式
*ellipsize:设置文字缩略方式
值为marquee时,需要配合marqueeRepeatLimit、focusable、focusableInTouchMode属性实现文字滚动效果
*autoLink:设置文本内容中的邮箱、电话等链接

*background:默认情况下,TextView是不带边框的,如果想设置边框,需要为TextView设置一个背景图片,该图片只是一个边框。 为文本框设置背景资源,背景图片可以使用shape资源文件作为图片使用

2.shape资源文件属性:

(1)stroke :边框效果 相当于html中的盒子模型的border
属性: android:width:描边的宽度
android:color:描边的颜色
android:dashWidth:表示边框的样式是虚线的宽度,
值为0时,表示为实线。
值大于0则为虚线。
android:dashGap :表示描边为虚线时, 虚线之间的间隔 即"- - - "

(2)padding :内部边距,即内容与边的距离
属性: android:left :左内边距
android:top :上内边距
android:right:右内边距
android:bottom :下内边距

(3)corners : 圆角
属性:android:radius:半径
android:topLeftRadius :左上角半径
android:topRightRadius :右上角半径
android:bottomLeftRadius :右下角半径
android:bottomRightRadius :左下角半径

(4)solid :内部填充
属性 android:color :填充颜色
(5)gradient : 渐变色
属性: android:startColor : 起始颜色
android:endColor :结束颜色
android:centerColor :渐变中间颜色
android:angle :渐变角度(PS:当angle=0时,渐变色是从左向右。
然后逆时针方向转,当angle=90时为从下往上。angle必须为45的
整数倍)
属性: android:type : 渐变类型(取值:linear、radial、sweep)
linear :线性渐变,这是默认设置
radial :放射性渐变,以开始色为中心。
sweep : 扫描线式的渐变。
android:gradientRadius :渐变色半径.当 android:type=“radial” 时才使用。单独使用 android:type="radial"会报错。

​ 属性 android:useLevel :如果要使用LevelListDrawable对象,就要设置为true。设置为true无渐变。false有渐变色
​ android:centerX : 渐变中心X点坐标的相对位置
​ android:centerY : 渐变中心Y点坐标的相对位置

3.CheckedTextView:

继承了TextView,增加了check功能
属性:
*android:checked=“true” :是否被选
*android:checkMark=“?android:attr/listChoiceIndicatorMultiple”: 设置勾选状态
*android:clickable=“true”:是否可以被点击
注意:添加CheckedTextView的OnClickListener事件。
在onClick方法中调用toggle()方法:用于切换选择的状态

Toast是Android系统提供的轻量级信息提醒机制,用于向用户提示即时消息,它显示在应用程序界面的最上层,显示一段时间后自动消失不会打断当前操作,也不获得焦点。

例如:

private CheckedTextView ch_tv; 
ch_tv=findViewById(R.id.ch_tv);
        ch_tv.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                //状态切换
                ch_tv.toggle();
                if(ch_tv.isChecked())
                    Toast.makeText(TvActivity.this,"音效已开启",Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(TvActivity.this,"音效已关闭",Toast.LENGTH_LONG).show();
            
        );
 <CheckedTextView
            android:id="@+id/ch_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:checkMark="?android:attr/listChoiceIndicatorMultiple"
            android:checked="false"
            android:clickable="true"
            android:text="是否打开音效"
            android:textSize="30sp"
            />

4.文本框(EditText)

–EditText:继承了TextView,可以编辑内容的文本框
–EditText组件的常用属性:

5.按钮及点击事件(Button)

–Button控件表示按钮,它继承自TextView控件,既可以显示文本,又可以显示图片,同时也允许用户通过点击来执行操作,当Button控件被点击时,被按下与弹起的背景会有一个动态的切换效果,这个效果就是点击效果 。
Button的4种点击事件实现方式:

6.背景选择器(selector)

Button的点击效果:

–通过background属性实现
①使用selector文件实现Button点击和释放是显示不同效果
②该属性可以是颜色,静态图片和shape的边框文件。但是这情况下单击按钮不会出现任何效果。

–selector定义的基本格式:

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
 <item  android:state_selected=true”
 android:drawable="@color/color1"/>
<item android:state_focused="true" 
      android:drawable="@color/color2" />
<item android:state_pressed="true" 
       android:drawable="@color/color3“/>
<item android:drawable="@color/color1" /> 
</selector>

–selector使用注意事项:

①每个item对应一种状态
②在定义selector的时候,根据不同的目的,选择不同的item属性,例如,为了定义组件的背景,需要使用android:drawable
③statelist中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后

7.单选框(RadioButton)

–RadioButton为单选按钮,android:checked属性指定是否选中的状态。
–RadioGroup是单选组合框,可容纳多个RadioButton,并把它们组合在一起,实现单选状态。
–语法格式

  <RadioGroup
android:属性名称 ="属性值"
                   ......>
<RadioButton
        android:属性名称 ="属性值"
                          ....../>
......
          <RadioGroup/>

例:

xml文件:

 <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/man"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男"
                android:textSize="25sp"
                />

            <RadioButton
                android:id="@+id/women"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"
                android:textSize="25sp" />
        </RadioGroup>

单选点击事件:

private RadioGroup radioGroup;
 radioGroup=findViewById(R.id.radioGroup);
      radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) 
                if(checkedId==R.id.man)
                    Toast.makeText(TvActivity.this,"你选择的性别为:男",Toast.LENGTH_LONG).show();
                if(checkedId==R.id.women)
                    Toast.makeText(TvActivity.this,"你选择的性别为:女",Toast.LENGTH_LONG).show();
            
        );

8.复选框(CheckBox)

–CheckBox表示复选框,它是Button的子类,用于实现多选功能,通过android:checked属性指定CheckBox控件是否选中的状态。

private CheckBox checkBox1;
final Set<String>  hoppy = new HashSet<String>();


checkBox1= findViewById(R.id.yu);
        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
                if (isChecked)
                    hoppy.add("羽毛球");
                else
                    hoppy.remove("羽毛球");
                Toast.makeText(TvActivity.this,"你的爱好是:"+hoppy,Toast.LENGTH_LONG).show();
            
        );

xml文件:

<CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="羽毛球"
            android:id="@+id/yu"/>

换种写法:

 private String hobbys;
    private TextView hobby;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkbox);
        CheckBox checkBox1 = findViewById(R.id.checkbox1);
        CheckBox checkBox2 = findViewById(R.id.checkbox2);
        CheckBox checkBox3 = findViewById(R.id.checkbox3);
        hobby = findViewById(R.id.hobby);
        checkBox1.setOnCheckedChangeListener(this);
        checkBox2.setOnCheckedChangeListener(this);
        checkBox3.setOnCheckedChangeListener(this);
        hobbys = new String();
    

    @Override
    public void onCheckedChanged(CompoundButton bottonView, boolean b) 
        String text = bottonView.getText().toString();
        if (b) 
            if (!hobbys.contains(text)) 
                hobbys = hobbys + text;
                hobby.setText(hobbys);
            
         else 

            if(hobbys.contains(text))
                hobbys = hobbys.replace(text,"");
                hobby.setText(hobbys);
            
        
    

xml文件:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择兴趣爱好"
        android:textColor="#FF8000"
        android:textSize="18sp"/>
   <CheckBox
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/checkbox1"
       android:text="羽毛球"
       android:textSize="18sp"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox2"
        android:text="篮球"
        android:textSize="18sp"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox3"
        android:text="乒乓球"
        android:textSize="18sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FF8000"
        android:textSize="22sp"
        android:text="您选择的兴趣爱好为:"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hobby"
        android:textSize="18sp"
        />

(2)ImageView以及子类

​ ImageView表示图片,它继承自View,可以加载各种图片资
源。

1.ImageButton

​ ImageButton显示图片,它继承自ImageView,拥有
ImageView的属性和方法,不过ImageButton有默认的按钮
外观。

​ 在ImageButton中载入图片后,图片周围有白边,影响到美观。解决这个问题的方法有两种:
•一种方法是将ImageButton的背景改为所需要的图片。
​ 如:android:background="@drawable/XXX"
•第二种方法就是将ImageButton背景改为透明, 这个方法更常用。在XML里;
<ImageButton android:background="#00000000" …/>

例如:

private ImageButton imageButton;
    private ImageView imageView;
    private int[] images=R.drawable.dog4,R.drawable.dog3,R.drawable.dog2,R.drawable.dog1;
    private int count = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_image);

        imageButton = findViewById(R.id.Image_button);
        imageView = findViewById(R.id.Image_View);
        imageButton.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                if(count==3) count = -1;
                imageView.setImageResource(images[++count]);
            
        );
    

xml:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dog1"
        android:id="@+id/Image_View"
        androidstudio基础imageview使用(代码片段)

AndroidStudio基础ImageView使用知识点1:<ImageViewandroid:layout_width="match_parent"android:layout_height="wrap_content"/>在ImageView中“match_parent”是占满宽度/高度,“wrap_co 查看详情

androidstudio基础toast信息(代码片段)

AndroidStudio基础Toast信息Toash弹出信息是给用户看的,Logcat信息是开发相关人员查看。使用按钮进行Toast弹出信息第一步:在布局XML文件中设置<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android= 查看详情

androidstudio基础控件飘红处理办法(代码片段)

问题AndroidStudio又作妖,在最新的AndroidStudio版本中[AndroidStudioArcticFox2020.3.1Patch2]基础控件在XML里面飘红。把项目删除,重新从gitclone也不能处理。处理需要在项目的gradle文件中配置如下classpath"org.jetbrains.kotlin:kotlin-gradle-p... 查看详情

androidstudio基础listview之simpleadapter(代码片段)

AndroidStudio基础ListView之SimpleAdapterListView、数据源(数组或集合)、单元格布局、数据适配器的关系图,当中数字1、2、3、4、5(代码调用)为五个步骤进行实现。第一步:在布局xml中设置ListView<?xmlversion&#... 查看详情

androidstudio使用小技巧笔记externaltools(代码片段)

简洁版:使用ExternalTools解决进程占用类问题:ExternalTools的gradlestop等命令添加几个快捷键.再遇到之类进程占用问题,直接一个快捷键解决.啰嗦版:使用AS的过程,会偶尔的\\经常的出现编译错误,例如,R.jar被另一个进程占用,或者xx.apk无... 查看详情

androidstudio使用小技巧笔记externaltools(代码片段)

简洁版:使用ExternalTools解决进程占用类问题:ExternalTools的gradlestop等命令添加几个快捷键.再遇到之类进程占用问题,直接一个快捷键解决.啰嗦版:使用AS的过程,会偶尔的\\经常的出现编译错误,例如,R.jar被另一个进程占用,或者xx.apk无... 查看详情

androidstudio使用小技巧笔记externaltools(代码片段)

简洁版:使用ExternalTools解决进程占用类问题:ExternalTools的gradlestop等命令添加几个快捷键.再遇到之类进程占用问题,直接一个快捷键解决.啰嗦版:使用AS的过程,会偶尔的\\经常的出现编译错误,例如,R.jar被另一个进程占用,或者xx.apk无... 查看详情

androidstudio基础listview之arrayadapter(代码片段)

AndroidStudio基础ListView之ArrayAdapterListView、数据源(数组或集合)、单元格布局、数据适配器的关系图,当中数字1、2、3、4、5(代码调用)为五个步骤进行实现第一步:在布局xml中设置ListView<?xmlversion=&... 查看详情

2022年全网最细androidstudio安装配置学习笔记(代码片段)

目录一、下载及安装二、配置三、AndroidStudio工程的创建一、下载及安装单击链接:https://developer.android.google.cn/studio/进入下载页面,点击DownloadAndroidStudio按钮,进行下载,如下图所示:二、配置第一次安装完成... 查看详情

androidstudio基础使用自定义对话框(代码片段)

AndroidStudio基础使用自定义对话框兼容低版本的APP运行第一步:新建新的空白activity,布局XML设置如下该APP的启动界面activity_main.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android 查看详情

androidstudio基础使用单选对话框alertdialog(代码片段)

AndroidStudio基础使用单选对话框AlertDialog(继承上一节的代码)第一步:在布局xml定义ID值<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.a 查看详情

androidstudio基础复选框checkbox(代码片段)

AndroidStudio基础复选框CheckBox知识点1:在一个布局xml文件中不允许出现重复的ID值。但是在不同的布局XML中可以出现同名的ID值。知识点2:在每个项目的MainActivity.java文件中只能绑定一个布局XML文件。1、复选框checkbox的切换... 查看详情

androidstudio开发基础知识(持续更新中~)(代码片段)

阅读本文需要有Java和前端的基础,本文是我学习Android时的笔记。大纲一、UI开发AS快捷键布局1.线性布局(LinearLayout)2.相对布局(RelaviteLayout)3.针布局(FrameLayout)4.表格布局(TableLayout)5.绝... 查看详情

androidstudio基础单选按钮radiobutton(代码片段)

AndroidStudio基础单选按钮RadioButton1、单选按钮RadioButton:多个只能选择一个,必须结合RadioGroup控件使用,才能实现单选的特性。第一种情况:没有结合RadioGroup控件使用,默认垂直布局第二种情况:结合RadioGr... 查看详情

【基础笔记】androidstudio拍照、选择相册(第三方框架)

参考技术A①选择指定后缀文件②在图片选择器中选择图片或视频③接受返回的文件FilePicker①、添加依赖②、Activity实现takephoto③实例图片④Git地址TakePhoto 查看详情

零基础用androidstudio实现简单的本地视频播放器(代码片段)

用AndroidStudio的VideoView组件实现简单的本地视频播放器一、创建AndroidStudio项目二、在界面布局文件activity_main.xml中定义VideoView组件三、编辑MainActivity.java四、在AndroidManifest.xml中配置相应的权限五、导出apk包六、对遇到的坑的思考... 查看详情

androidstudio基础对话框alertdialog最基本的使用(代码片段)

AndroidStudio基础对话框AlertDialog最基本的使用1、对按钮添加一个点击事件,需要配合Activity.java进行实现第一步:在该布局XML中对按钮增加ID值,进行唯一性参照物 <?xmlversion="1.0"encoding="utf-8"?><Li... 查看详情

基于androidstudio开发安卓控件的基础属性1(代码片段)

安卓控件1.TextView控件2.Button控件2.1控件属性2.2控件的事件处理2.2.1点击事件2.2.2长按事件2.2.3触摸事件3.EditText控件4.ImageView控件4.1src资源4.2scaleType属性4.3maxHeight属性4.4maxWidth属性4.5adjustViewBounds属性5.ProgressBar进度条5.1进度条的最大... 查看详情