`
_小菜鸟
  • 浏览: 33972 次
社区版块
存档分类
最新评论

我的android 第7天 - 控件美化 和 编码性能优化

阅读更多

我的android 第7天 - 控件美化 和 编码性能优化

控件美化

1.控件的背景

1>直接用图片 android:background="@drawable/black" 

  图片最好使用9patch工具进行处理(android-sdk\tools\draw9patch.bat)

2>直接用颜色 android:background="#ffffff"

3>使用shape

  <!-- android:shape 默认是矩形-->

    <shape android:shape="rectangle">

    <!-- 设置圆角半径-->

  <corners android:radius="5px"/>

  <!-- 总体背景为红色-->

  <solid android:color="#ff0000"/>

  <!-- 边框-->

  <stroke android:width="2px"android:color="#0000ff"/>

  </shape>

4>使用selector

 <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 按钮被按下时用蓝色图片-->

    <item android:state_pressed="true"android:drawable="@drawable/blue_deal"/>

   

    <!-- 其他情况用黑色图片(没有写状态的item要放到最后面)-->

    <item android:drawable="@drawable/black_deal"/>

</selector>

 

2.按钮的文字颜色

1>可以直接写颜色值 android:textColor="#ffffff"

2>colors.xml中定义颜色  <colorname="white">#ffffff</color>  android:textColor="@color/white"

3>使用selector切换按钮文字颜色

 * 在res下新建一个color文件夹,在此文件夹下新建一个selector,如btn_text.xml

  <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"android:color="#ffffff"/>

    <item android:color="#000000"/>

  </selector>

android:textColor="@color/btn_text"

 

*注意:selector的文件名不能跟colors.xmlname属性值一样

编码性能优化

 

1.尽量用静态方法替代动态方法

 

2.尽量用直接类型,不要用接口类型(比如用ArrayList替代List)

 

3.尽量使用public变量,不使用getter、setter

 

4.尽量将成员变量转为局部变量后再使用

 

5.对于循环

  使用 int size = list.size();

   for (int i = 0; i <size; i++) 

  代替 for (int i = 0; i <list.size(); i++) 

 

 

 

 

下载视频代码

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics