Skip to content

Commit

Permalink
Modify code format.
Browse files Browse the repository at this point in the history
  • Loading branch information
CharonChui committed Dec 20, 2014
1 parent 84c65c4 commit b246972
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Android学习加强/布局优化.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
- 使用`Relativelayout`代替`LinearLayout`.
平时写布局的时候要多注意,写完后可以通过`Hierarchy Viewer`或在手机上通过开发者选项中的显示布局边界来查看是否有不必要的嵌套。

- 使用`include`
`include`可以用于将布局中一些公共的部分提取出来。在需要的时候使用即可,比喻一些页面统一的`loading`页。
- 使用`include`
`include`可以用于将布局中一些公共的部分提取出来。在需要的时候使用即可,比喻一些页面统一的`loading`页。
`include`标签的`layout`属性指定所要包含的布局文件,我们也可以通过`android:id`或者一些其他的属性来覆盖被引入布局的根节点所对应
的属性值。
```xml
Expand Down Expand Up @@ -40,8 +40,8 @@
</RelativeLayout>
```

- 使用`<merge>`标签
`merge`可以有效的解决布局的层级关系。我们通过一个例子来说明一下:
- 使用`<merge>`标签
`merge`可以有效的解决布局的层级关系。我们通过一个例子来说明一下:
```xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
Expand All @@ -66,10 +66,10 @@
</FrameLayout>
```

我们在一个页面中显示该部分内容,运行后观察`Hierarchy Viewer`。
![image](https://raw.githubusercontent.com/CharonChui/Pictures/master/merge_1.png)
我们在一个页面中显示该部分内容,运行后观察`Hierarchy Viewer`。
![image](https://raw.githubusercontent.com/CharonChui/Pictures/master/merge_1.png)
我们会发现除了我们布局最外层还会有一层`FrameLayout`,这是因为`Activity`内容视图的`parent view`就是一个`FrameLayout`,所以对于我们来说无意中已经多了一层毫无意义的布局。
接下来`merge`的功能就能发挥了,修改代码如下。
接下来`merge`的功能就能发挥了,修改代码如下。
```xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">

Expand All @@ -90,12 +90,12 @@

</merge>
```
接下来我们在用`Hierarchy Viewer`观察就会发现完美的去掉了一层`FrameLayout`
![image](https://raw.githubusercontent.com/CharonChui/Pictures/master/merge_2.png)
接下来我们在用`Hierarchy Viewer`观察就会发现完美的去掉了一层`FrameLayout`
![image](https://raw.githubusercontent.com/CharonChui/Pictures/master/merge_2.png)
当然上面我们使用`merge`是因为跟布局正好是`FrameLayout`并且没有`backgroud`和`padding`等这些属性。如果根本局是`LinearLayout`等,就没法直接使用`merge`了。
在`include`的时候很容易造成布局层级嵌套过多,结合`merge`使用能有效解决这个问题。

- 使用`ViewStub`
- 使用`ViewStub`
`ViewStub`标签与`include`一样可以用来引入一个外部布局,但是`Viewstub`引入的布局默认不会解析与显示,宽高为0,`View`也为`null`,这样就会在解析`layout`时节省`cpu`和内存。简单的理解就是`ViewStub`
`include`加上`GONE`.`ViewStub`常用来引入那些默认不会显示,只在特殊情况下显示的布局,如进度布局、网络失败显示的刷新布局、信息出错出现的提示布局等.
```xml
Expand Down Expand Up @@ -144,7 +144,7 @@
mNetErrorView = findViewById(R.id.network_unreachble);
```

- 减少不必要的`Inflate`
- 减少不必要的`Inflate`
如上一步中`stub.infalte()`后将该`View`进行记录或者是`ListView``item inflate`的时候。

---
Expand Down

0 comments on commit b246972

Please sign in to comment.