ITeye博客

android 用到的技巧集

yahaitt · 2009-08-19 18:10 · 酷爱Android · Markdown

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. getWindowManager().getDefaultDisplay().getMetrics(dm);
  3. Stirng width = dm.widthPixels;
  4. 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>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。