2009
-
08
-
19
android 用到的技巧集
关键字: android
1.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
- <?xml version="1.0" encoding="utf-8" ?>
- <resources>
- <drawable name="darkgray">#808080FF</drawable>
- <drawable name="white">#FFFFFFFF</drawable>
- </resources>
对文本框的背景色进行设置
- Resources resources = getBaseContext().getResources();
- Drawable HippoDrawable = resources.getDrawable(R.drawable.white);
- mTextView01.setBackgroundDrawable(HippoDrawable);
2.获取手机屏幕大小
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- Stirng width = dm.widthPixels;
- Stirng heiht = dm.heightPixels;
3.Android style 机制
仅仅是加了一个Style. 一个Style就能够实现控件的显示效果样式么?Android的Style机制真的很强大.
例:
- <?xml version="1.0" encoding="utf-8" ?>
- - <resources>
- - <style name="DavidStyleText1">
- <item name="android:textSize">18sp</item>
- <item name="android:textColor">#EC9237</item>
- </style>
- - <style name="DavidStyleText2">
- <item name="android:textSize">14sp</item>
- <item name="android:textColor">#FF7F7C</item>
- <item name="android:fromAlpha">0.0</item>
- <item name="android:toAlpha">0.0</item>
- </style>
- </resources>
- <?xml version="1.0" encoding="utf-8" ?>
- - <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">
- - <!-- 应用模式1的TextView
- -->
- <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" />
- - <!-- 应用模式2的TextView
- -->
- <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" />
- </LinearLayout>