反射的使用
1.使用反射机制获取安卓内部资源
(1) 获取系统Toast的内部资源(layout/view)
Toast的布局文件:com.android.internal.R.layout.transient_notification
Toast的文本视图:com.android.internal.R.id.message
Toast的相关源码:
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
在安卓的应用程序中无法直接使用这些系统的内部资源,但可以通过反射机制获取并使用,代码如下:
try{
Class<?> clazz = Class.forName("com.android.internal.R$layout");
Field field = clazz.getField("transient_
Field field = clazz.getField("transient_