Android第一周实习笔记

这篇博客记录了作者在Android实习第一周的学习笔记,涉及内容包括:设置View的点击事件,添加外部库的两种方法,深入理解build.gradle文件的各个配置项,以及Android Studio中Gradle的基础知识。此外,还提到了Android Studio使用SVN的注意事项,如何避免提交特定文件,以及在布局设计中的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.点击事件通过view获得id,然后设置事件

public void onClick(View view) {
    switch (view.getId()) {
        case R.id.forget_btn_get_code:
            changeLayout(1);
            break;
        case R.id.forget_btn_finsh:
            changeLayout(1);
            break;
    }
}

2.添加外部库:
(1)libs添加所需的.jar包,然后右键add as library
这里写图片描述
这里写图片描述
(2)选择File-project structure,然后找到这个项目的modules,在Dependencies里选“+”,找到那个jar添加进去。
这里写图片描述
(3)重新build。

3.build.gradle
这里写图片描述
一个个讲每个gradle的作用
(1)app/build.gradle
个文件是app文件夹下这个Module的gradle配置文件,也可以算是整个项目最主要的gradle配置文件,我们来看下这个文件的内容:

// 声明是Android程序
apply plugin: 'com.android.application'

android {
    // 编译SDK的版本
    compileSdkVersion 21
    // build tools的版本
    buildToolsVersion "21.1.1"

    defaultConfig {
        // 应用的包名
        applicationId "me.storm.ninegag"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0.0"
    }

    // java版本
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    buildTypes {
        debug {
            // debug模式
        }

        release {
            // 是否进行混淆
            minifyEnabled false
            // 混淆文件的位置
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    // 移除lint检查的error
    lintOptions {
      abortOnError false
    }
}

dependencies {
    // 编译libs目录下的所有jar包
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:21.0.2'
    compile 'com.etsy.android.grid:library:1.0.5'
    compile 'com.alexvasilkov:foldable-layout:1.0.1'
    // 编译extras目录下的ShimmerAndroid模块
    compile project(':extras:ShimmerAndroid')
}

这里需要说明几点:

  1. 文件开头apply plugin是最新gradle版本的写法,以前的写法是apply plugin: ‘android’,如果还是以前的写法,请改正过来。
  2. buildToolsVersion这个需要你本地安装该版本才行,很多人导入新的第三方库,失败的原因之一是build version的版本不对,这个可以手动更改成你本地已有的版本或者打开 SDK Manager 去下载对应版本。
  3. applicationId代表应用的包名,也是最新的写法,这里就不在多说了。
  4. android5.0开始默认安装jdk1.7才能编译,但是由于mac系统自带jdk的版本是1.6,所以需要手动下载jdk1.7并配置下。
  5. minifyEnabled也是最新的语法,很早之前是runProguard,这个也需要更新下。
  6. proguardFiles这部分有两段,前一部分代表系统默认的android程序的混淆文件,该文件已经包含了基本的混淆声明,免去了我们很多事,这个文件的目录在/tools/proguard/proguard-android.txt , 后一部分是我们项目里的自定义的混淆文件,目录就在 app/proguard-rules.txt , 如果你用Studio 1.0创建的新项目默认生成的文件名是 proguard-rules.pro , 这个名字没关系,在这个文件里你可以声明一些第三方依赖的一些混淆规则,由于是开源项目,9GAG里并未进行混淆,具体混淆的语法也不是本篇博客讨论的范围。最终混淆的结果是这两部分文件共同作用的。
  7. compile project(‘:extras:ShimmerAndroid’)这一行是因为9GAG中存在其他Module,不知道Module的概念可以看下这篇博客Android Studio系列教程二–基本设置与运行, 总之你可以理解成Android Library,由于Gradle的普及以及远程仓库的完善,这种依赖渐渐的会变得非常不常见,但是你需要知道有这种依赖的。
  8. 以上文件里的内容只是基本配置,其实还有很多自定义部分,如自动打包debug,release,beta等环境,签名,多渠道打包等,后续会单独拿出来讲解。

    2.extras/ShimmerAndroid/build.gradle
    每一个Module都需要有一个gradle配置文件,语法都是一样,唯一不同的是开头声明的是:applyplugin:‘com.android.library’

3.9GAG/gradle
这个目录下有个 wrapper 文件夹,里面可以看到有两个文件,我们主要看下 gradle-wrapper.properties 这个文件的内容:
这里写图片描述
可以看到里面声明了gradle的目录与下载路径以及当前项目使用的gradle版本,这些默认的路径我们一般不会更改的,这个文件里指明的gradle版本不对也是很多导包不成功的原因之一。

4.9GAG/build.gradle
这个文件是整个项目的gradle基础配置文件,我们来看看这里面的内容
这里写图片描述
内容主要包含了两个方面:一个是声明仓库的源,这里可以看到是指明的jcenter(), 之前版本则是mavenCentral(), jcenter可以理解成是一个新的中央远程仓库,兼容maven中心仓库,而且性能更优。另一个是声明了android gradle plugin的版本,android studio 1.0正式版必须要求支持gradle plugin 1.0的版本。

.5.9GAG/settings.gradle
这个文件是全局的项目配置文件,里面主要声明一些需要加入gradle的module,我们来看看9GAG该文件的内容:

include ':app', ':extras:ShimmerAndroid'

上面gradle解释来自:http://stormzhang.com/devtools/2014/12/18/android-studio-tutorial4/
文件中的 app, extras:ShimmerAndroid 都是module,如果还有其他module都需要按照如上格式加进去。
这里写图片描述

4.Android Studio利用SVN上传更新文件,设置忽略文件:
这里写图片描述
注:忽略文件一般看.gitignore文件。
这里写图片描述
这里写图片描述
**Android Studio 中建议过滤的文件:
- .idea 文件夹
- .gradle 文件夹
- 所有的 build 文件夹
- 所有的 .iml 文件
- local.properties 文件**
一般如下图:
这里写图片描述
当分享者配置了忽略文件并Share Project到SVN后,其他人Check下来就不需要配置了,Commit的时候会自动忽略那些文件。但文件名显示为红色,这时配置下忽略文件就恢复正常了。
Check下来的Project:
这里写图片描述
配置忽略文件后:
这里写图片描述

5.两个方便写代码的插件
这里写图片描述
这里写图片描述

6.Toast.makeText用法:
Toast是一个类,主要用于消息的提示。
makeText(),是Toast的一个方法,用来显示信息,有三个参数。
第一个参数:this,是上下文参数,指当前页面显示。
第二个参数:显示你想要显示的内容。
第三个参数:有Toast.LENGTH_LONG和ToastLENGTH_SHORT参数,主要是定义显示时间的长短。
注:show(),表示显示这个Toast消息提醒,当程序运行到这里的时候,就会显示出来,如果不调用show()方法,这个Toast对象存在,但是并不会显示,所以一定不要忘记。

7.实现下面的布局情况(主要是一行分成几部分,且对称的情况):
这里写图片描述
通过布局Layout里面放置一个Button按钮,然后为Button设置layout_marginLeft和layout_marginRight属性,就可以图中按钮的布局。

<LinearLayout
                android:layout_marginTop="20dp"
                android:orientation="horizontal"
                android:weightSum="10"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <Button
                    android:id="@+id/write_btn_submit_finish"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:gravity="center"
                    android:padding="10dp"
                    android:textColor="@color/white"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:background="@drawable/comm_btn_bg_red"
                    android:text="@string/write_btn_submit"/>

            </LinearLayout>

这里写图片描述
通过设置View“占空”,并通过weight属性就可以实现上面布局。

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/login_btn_regist"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:textColor="@color/login_btn_txt_color"
                android:text="@string/login_btn_regist"/>

            <View
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
            <TextView
                android:id="@+id/login_btn_findpwd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/login_btn_txt_color"
                android:textSize="15sp"
                android:text="@string/login_btn_findpwd"/>
        </LinearLayout>

这里写图片描述

<LinearLayout
                android:layout_marginTop="80dp"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"/>

                <TextView
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    android:textSize="16dp"
                    android:text="1.如你无大陆身份证或者有其他疑问,请联系客服4000-000-0000"/>

                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"/>

            </LinearLayout>

注:一条横线的代码:

<View
                    android:layout_width="match_parent"
                    android:layout_height="3dp"
                    android:background="#efefef"/>

8.实现下面的布局:
按钮居中,左右对称;按钮位于最下端,中间空空间。
这里写图片描述

<LinearLayout
                android:background="@drawable/comm_edit_bg_white"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >

                <TextView
                    android:padding="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="21dp"
                    android:gravity="center"
                    android:text="@string/info_text_phone_num"/>
                <EditText
                    android:padding="10dp"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:background="@drawable/comm_edit_bg_white"
                    android:layout_height="match_parent" />

            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            <Button
                android:id="@+id/write_btn_bind"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:layout_margin="10dp"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:textStyle="bold"
                android:background="@drawable/comm_btn_bg_red"
                android:text="@string/write_btn_submit"/>

        </LinearLayout>

注:中间的空空间通过View控件和weight属性设置,下面按钮通过margin设置layout_margin=”10dp”。还可以通过设置布局layout里面放置一个Button按钮,然后为Button按钮设置layout_marginLeft和layout_marginRight属性,就可以实现图中的布局界面。

9.Android 6.0将Apahce Http Client 移除出SDK解决方法:
这里写图片描述
(注:SDk版本23就是Android6.0)
一般会报” Unable to find optional library: org.apache.http.legacy”错误提示。
(1)在build.gradle中加入下面的配置:
android {
useLibrary ‘org.apache.http.legacy’
}
(2)看看目录E:\software\Android\sdk\platforms**android-23**\optional 下有没有org.apache.http.legacy.jar 和 optional.json
这里写图片描述
如果没有optional.json,则自己新建一个这样的文件,然后加入如下内容:

[  
  {  
    "name": "org.apache.http.legacy",  
    "jar": "org.apache.http.legacy.jar",  
    "manifest": false  
  }  
] 

10.compileSdkVersion和buildToolsVersion:
(1)CompileSdkVersion是你SDK的版本号,也就是API Level,例如API-19、API-20、API-21等等。
(2)buildeToolVersion是你构建工具的版本,其中包括了打包工具aapt、dx等等。这个工具的目录位于..your_sdk_path/build-tools/XX.XX.XX
这个版本号一般是API-LEVEL.0.0。
(3)你可以用高版本的build-tool去构建一个低版本的sdk工程,例如build-tool的版本为20,去构建一个sdk版本为18的例如:compileSdkVersion 18 buildToolsVersion “22.0.1”这样也是OK的。
附注:SDK 目录功能解析:
(1)【build-tools】里面是不同版本(例如21.1.1)的build工具,这些工具包括了aapt打包工具、dx.bat、aidl.exe等等
(2)【platform】是存放不同API-level版本SDK目录的地方
(3)【platform-tools】是一些android平台相关的工具,adb、fastboot等
(4)【tools】是指的安卓开发相关的工具,例如android.bat、ddms.bat(Dalvik debug Monitor Service)、draw9patch.bat等等。

11.eclipse到Android Studio的项目迁移可能遇见的几类问题:
来源:http://www.itnose.net/detail/6352571.html

12.安卓文件名命名规范:
http://yidongkaifa.iteye.com/blog/1809593#userconsent#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值