android 用到的技巧集
16lz
2021-01-23
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
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <drawablename="darkgray">#808080FF</drawable>
- <drawablename="white">#FFFFFFFF</drawable>
- </resources>
对文本框的背景色进行设置
- Resourcesresources=getBaseContext().getResources();
- DrawableHippoDrawable=resources.getDrawable(R.drawable.white);
- mTextView01.setBackgroundDrawable(HippoDrawable);
2.获取手机屏幕大小
- DisplayMetricsdm=newDisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- Stirngwidth=dm.widthPixels;
- Stirngheiht=dm.heightPixels;
3.Android style 机制
仅仅是加了一个Style. 一个Style就能够实现控件的显示效果样式么?Android的Style机制真的很强大.
例:
- <?xmlversion="1.0"encoding="utf-8"?>
- -<resources>
- -<stylename="DavidStyleText1">
- <itemname="android:textSize">18sp</item>
- <itemname="android:textColor">#EC9237</item>
- </style>
- -<stylename="DavidStyleText2">
- <itemname="android:textSize">14sp</item>
- <itemname="android:textColor">#FF7F7C</item>
- <itemname="android:fromAlpha">0.0</item>
- <itemname="android:toAlpha">0.0</item>
- </style>
- </resources>
- <?xmlversion="1.0"encoding="utf-8"?>
- -<LinearLayoutxmlns: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
- -->
- <TextViewstyle="@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
- -->
- <TextViewstyle="@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 事件流转机制
- Android的权限控制机制
- Android 修改EditText光标颜色
- Android系统Surface机制的SurfaceFlinger服务渲染应用程序UI的过
- Android进程系列第八篇---LowmemoryKiller机制分析(下)