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>
本文介绍了Android开发中关于UI设计的一些实用技巧,包括如何使用Drawable来改变界面元素的颜色和背景,获取设备屏幕尺寸的方法,以及通过定义样式(Style)来统一和简化界面设计的过程。
987

被折叠的 条评论
为什么被折叠?



