`
yahaitt
  • 浏览: 756556 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

android 用到的技巧集

阅读更多

1.Drawable的使用

 

 

android.graphics.drawable

 

Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms.

 

最经常会处理Drawable作为类型的资源回收绘制到屏幕上的东西; Drawable类提供了一个通用的API来处理一个基本的视觉资源,可以采取多种形式。(讲的有点抽象)讲白点就是获取res下的参数

 

例:改变TextView文字颜色-引用Drawable颜色常熟及背景色

    values下的color.xml

 

  1. <?xml version="1.0" encoding="utf-8" ?>   
  2.  <resources>  
  3.  <drawable name="darkgray">#808080FF</drawable>   
  4.  <drawable name="white">#FFFFFFFF</drawable>   
  5.  </resources>  

    对文本框的背景色进行设置


  1. Resources resources = getBaseContext().getResources();  
  2. Drawable HippoDrawable = resources.getDrawable(R.drawable.white);  
  3. mTextView01.setBackgroundDrawable(HippoDrawable);   

 

2.获取手机屏幕大小

 

  1. DisplayMetrics  dm = new DisplayMetrics();  
  2.   
  3. getWindowManager().getDefaultDisplay().getMetrics(dm);  
  4.   
  5. Stirng width = dm.widthPixels;  
  6.   
  7. Stirng heiht = dm.heightPixels;  

 
3.Android style 机制

仅仅是加了一个Style. 一个Style就能够实现控件的显示效果样式么?Android的Style机制真的很强大.
例:

 

  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <resources>  
  3. <style name="DavidStyleText1">  
  4.   <item name="android:textSize">18sp</item>   
  5.   <item name="android:textColor">#EC9237</item>   
  6.   </style>  
  7. <style name="DavidStyleText2">  
  8.   <item name="android:textSize">14sp</item>   
  9.   <item name="android:textColor">#FF7F7C</item>   
  10.   <item name="android:fromAlpha">0.0</item>   
  11.   <item name="android:toAlpha">0.0</item>   
  12.   </style>  
  13.   </resources>  

 


  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/white" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">  
  3. <!--  应用模式1的TextView  
  4.   -->   
  5.   <TextView style="@style/DavidStyleText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:text="@string/str_text_view1" />   
  6. <!--  应用模式2的TextView  
  7.   -->   
  8.   <TextView style="@style/DavidStyleText2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:text="@string/str_text_view2" />   
  9.   </LinearLayout>  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics