Android 小技巧
一、利用<ViewStub/>标签,在需要加载布局时再加载,提升app整体渲染速度
<ViewStub
android:id="@+id/stub_import"
android:inflatedId="@+id/panel_import"
android:layout="@layout/progress_overlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
progress_overlay.xml
<ProgressBar android:id="@+id/progress"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
显示:当你准备显示 ViewStub时,调用inflate()方法即可。你还可以设定ViewStub的Visibility为VISIBLE或INVISIBLE,也会触发inflate。注意的是,使用inflate()方法能返回布局文件的根View
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
注意:当ViewStub inflate后,这个ViewStub就从View层次中移除了。因此,没有必要保留一个对ViewStub的引用(如在类的字段里)。
二、利用<include />标签来避免重复渲染
当我们需要为App中的每个View都添加一个header或者footer时,重复地复制粘贴可以解决这个问题,但未免太繁杂。可以试着使用<include />标签:
第一种方式,在<include />标签内指定width及height:
main.xml
- <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width= "fill_parent"
- android:layout_height= "fill_parent" >
-
- <Button
- android:layout_width ="fill_parent"
- android:layout_height ="wrap_content"
- android:layout_gravity ="center_vertical"
- android:onClick ="onShowMap"
- android:text ="@string/show_map" />
-
- <include
- android:layout_width ="fill_parent"
- android:layout_height ="wrap_content"
- android:layout_alignParentBottom ="true"
- android:layout_marginBottom ="30dp"
- layout ="@layout/footer" />
-
- </RelativeLayout>
footer.xml
- <TextView xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width= "0dp"
- android:layout_height= "0dp"
- android:gravity= "center"
- android:text= "@string/footer_text" />
注意:有个小细节需要注意,在footer.xml中,我们将width及height都设为0dp.目的是为了配合<include/>标签中对width及height的定义。
第二种方式,直接引用:
main.xml
- <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width= "fill_parent"
- android:layout_height= "fill_parent" >
-
- <Button
- android:layout_width ="fill_parent"
- android:layout_height ="wrap_content"
- android:layout_gravity ="center_vertical"
- android:onClick ="onShowMap"
- android:text ="@string/show_map" />
-
- <include layout ="@layout/footer" />
-
- </RelativeLayout>
footer.xml
- <TextView xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width= "fill_parent"
- android:layout_height= "wrap_content"
- android:layout_alignParentBottom= "true"
- android:layout_marginBottom= "30dp"
- android:gravity= "center"
- android:text= "@string/footer_text" />
三、解决adb端口占用,连不上,终止连接等问题
1.CMD -> adb nodaemon server 查看adb service是否启动和端口
2.netstat -ano | findstr "5037" 查看是那些进程使用了“5037”端口
3.查看本机正在监听的PID listenering 后面是 该进程ID
4.在 window任务管理器 杀掉 这个 进程