Android Studio 视图解析

Android Studio一共有三种视图.我们来分别分析每一种视图的作用 .

一.Project视图.(白色字体的目录/文件可不关注)


图片中的链接
.gitignore文件说明: http://www.html-js.com/article/2030
Android的Module推荐这么写.gitignore文件
<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">/</span><span class="pln" style="color: rgb(72, 72, 76);">build</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">*.</span><span class="pln" style="color: rgb(72, 72, 76);">iml</span></code></div></div>
Android的项目.gitignore文件推荐这么写
<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">#</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">Built</span><span class="pln" style="color: rgb(72, 72, 76);"> application files  </span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">*.</span><span class="pln" style="color: rgb(72, 72, 76);">apk  </span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">*.</span><span class="pln" style="color: rgb(72, 72, 76);">ap_  </span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">#</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">Files</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">for</span><span class="pln" style="color: rgb(72, 72, 76);"> the </span><span class="typ" style="color: teal;">Dalvik</span><span class="pln" style="color: rgb(72, 72, 76);"> VM  </span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">*.</span><span class="pln" style="color: rgb(72, 72, 76);">dex  </span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">#</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">Java</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">class</span><span class="pln" style="color: rgb(72, 72, 76);"> files  </span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">*.</span><span class="kwd" style="color: rgb(30, 52, 123);">class</span><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">#</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">Generated</span><span class="pln" style="color: rgb(72, 72, 76);"> files  </span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);">bin</span><span class="pun" style="color: rgb(147, 161, 161);">/</span><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);">gen</span><span class="pun" style="color: rgb(147, 161, 161);">/</span><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">#</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">Gradle</span><span class="pln" style="color: rgb(72, 72, 76);"> files  </span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">gradle</span><span class="pun" style="color: rgb(147, 161, 161);">/</span><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);">build</span><span class="pun" style="color: rgb(147, 161, 161);">/</span><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">#</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">Local</span><span class="pln" style="color: rgb(72, 72, 76);"> configuration file </span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">sdk path</span><span class="pun" style="color: rgb(147, 161, 161);">,</span><span class="pln" style="color: rgb(72, 72, 76);"> etc</span><span class="pun" style="color: rgb(147, 161, 161);">)</span><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);">local</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">properties  </span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">#</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">Proguard</span><span class="pln" style="color: rgb(72, 72, 76);"> folder generated by </span><span class="typ" style="color: teal;">Eclipse</span><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);">proguard</span><span class="pun" style="color: rgb(147, 161, 161);">/</span><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">#</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">Log</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">Files</span><span class="pln" style="color: rgb(72, 72, 76);">  </span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);">*.</span><span class="pln" style="color: rgb(72, 72, 76);">log </span></code></div></div>

二.Packages视图



该视图下和Project最大的区别就是隐藏了中的配置文件,属性文件和系统自身的目录.
只是显示了当前的Module列表和Module 下面的目录,文件.

三.Android视图

Andrid视图是新版的AS新推出来的一个视图.很多人都是从Ecplise的开发中转过来的,习惯了Project视图.
使用了一下Android视图.发觉确实有它的去多优点.


由于大部分的目录,文件都在第一个个视图中详细进行的标注.这里就不在一一标注了.
可以看到在Android视图下的一级目录分两个区域,一个是Module列表区域,一个是Gradle脚本区域

Module目录下分三个子目录,分别对应mainfests文件,java源码,res资源目录.
图中,有多个目录和文件名是相同的.但是后面带有一个标识.例如diesns.xml文件有两个,一个后面带有(w820dp)
意思说手机的可用宽度至少要820dp的情况下才会引用该文件下的dimens值.具体请参考链接:
我们比较关心这样的文件是怎么建立的.其实前面已经说了,Project的所有视图都是真实的目录.而在
Android视图下,很大目录都是不存在,是由Android视图自动合并在了一起,方便我们理解和布置.
eg:

切换到Project视图中,就是在该目录下的一个dimens文件

Android视图自动把它们合并到一起,表示这几个文件的作用是一样的,只不过会因为设备的不同,而引用不同目录下的文件.


Gradle脚本目录下有多个Gradle文件和属性文件.
看其后面的提示,从上到下分别为:项目空间的gradle配置文件,第一个module和第二个module配置文件.
gralde属性文件,设置文件,以及本地SDK路径属性文件.
这些文件在Project中已经详细的说明了.这里就不一一解释.

Adnroid视图最大的好处就是去掉一些我们本身不关心的文件和目录.(配置,属性文件等)并且把一些资源文件,源文件很清晰的合并在一起.
隐藏了一些自动自动生成的文件和目录(例如R文件等)可以让我们更方法的管理整个项目/module.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio 中,视图的测量方法是通过重写视图的 `onMeasure()` 方法来实现的。`onMeasure()` 方法用于确定视图在父容器中的大小。 在自定义视图中,你可以重写 `onMeasure()` 方法,并在其中调用 `setMeasuredDimension()` 方法来设置视图的宽度和高度。 下面是一个示例: ```java public class CustomView extends View { // ... @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 调用父类的onMeasure()方法获取建议的宽度和高度 super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 解析宽度和高度的测量模式和大小 int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); // 根据测量模式调整视图的大小 int width, height; if (widthMode == MeasureSpec.EXACTLY) { // 宽度为精确值 width = widthSize; } else { // 宽度为自适应或包裹内容 // TODO: 根据需要计算宽度 width = ...; } if (heightMode == MeasureSpec.EXACTLY) { // 高度为精确值 height = heightSize; } else { // 高度为自适应或包裹内容 // TODO: 根据需要计算高度 height = ...; } // 设置测量后的宽度和高度 setMeasuredDimension(width, height); } // ... } ``` 在 `onMeasure()` 方法中,你可以根据需要计算视图的宽度和高度,并使用 `setMeasuredDimension()` 方法设置测量后的宽度和高度。其中,测量模式有三种:`MeasureSpec.EXACTLY` 表示精确值,`MeasureSpec.AT_MOST` 表示自适应或包裹内容,`MeasureSpec.UNSPECIFIED` 表示没有限制。 请根据你的实际需求进行相应的计算和设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值