Android 布局技巧

10 篇文章 0 订阅

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()方法即可。你还可以设定ViewStubVisibilityVISIBLEINVISIBLE,也会触发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
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.     android:layout_width"fill_parent"  
  3.     android:layout_height"fill_parent" >  
  4.   
  5.     <Button  
  6.         android:layout_width ="fill_parent"  
  7.         android:layout_height ="wrap_content"  
  8.         android:layout_gravity ="center_vertical"  
  9.         android:onClick ="onShowMap"  
  10.         android:text ="@string/show_map" />  
  11.   
  12.     <include  
  13.         android:layout_width ="fill_parent"  
  14.         android:layout_height ="wrap_content"  
  15.         android:layout_alignParentBottom ="true"  
  16.         android:layout_marginBottom ="30dp"  
  17.         layout ="@layout/footer" />  
  18.   
  19. </RelativeLayout>  
footer.xml
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <TextView xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.     android:layout_width"0dp"  
  3.     android:layout_height"0dp"  
  4.     android:gravity"center"  
  5.     android:text"@string/footer_text" />  
注意:有个小细节需要注意,在footer.xml中,我们将width及height都设为0dp.目的是为了配合<include/>标签中对width及height的定义
第二种方式,直接引用:
main.xml
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.     android:layout_width"fill_parent"  
  3.     android:layout_height"fill_parent" >  
  4.   
  5.     <Button  
  6.         android:layout_width ="fill_parent"  
  7.         android:layout_height ="wrap_content"  
  8.         android:layout_gravity ="center_vertical"  
  9.         android:onClick ="onShowMap"  
  10.         android:text ="@string/show_map" />  
  11.   
  12.     <include layout ="@layout/footer" />  
  13.   
  14. </RelativeLayout>  
footer.xml
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <TextView xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.     android:layout_width"fill_parent"  
  3.     android:layout_height"wrap_content"  
  4.     android:layout_alignParentBottom"true"  
  5.     android:layout_marginBottom"30dp"  
  6.     android:gravity"center"  
  7.     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任务管理器 杀掉 这个 进程 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值