资源利用步骤


使用资源  资源层次: 他们都在 R.java中有自己的标识符  而真正 名字为id的标识符只用控件 另外注意 R.java 是代码和资源的接口,也就是说要想用xml文件创建的资源只能用 R.xx.xx调用 而在xml 文件中引用其他 xml中的资源只需要 @xxx/xxx 即可 Layout --- R.layout.XXXX (layout 必须在AndroidManifest中添加相应 Activity标签 否者有异常) widget --- R.id.XXXX    在添加控件时要两点可能要注意: 1.id应当指定 一个名字, 2.text属性应当引用string资源中的一个对象) String --- R.string.xxxx Menu  ---  R.Menu.xxxx style  --- R.style.xxxx drawable --R.drawable.xxxx attr  -----R.attr.xxx   控件资源 布局资源 setContentView(R.layout.login_system); button = (Button)findViewById(R.id.cancelButton); button.setOnClickListener(new onClickListenerz{....});   颜色资源 1.在res\values 文件夹下定义Color.xml 文件 #f00 #0000ff 2.在布局xml中使用  android:text = "这是一种颜色字体" android:id = "@+id/textview01" android:textColor = "@color/blue_text" />   3.在Activity类中使用 import ...R getWindow().setBackgroundDrawableResource(R.color.red);   同理其他资源一样:先建立 xml 文件 然后下面写入内容如,之后就可以在代码和 xml中使用了     尺寸资源 dimen的使用 Resource r = getResource(); float f  = r.getDimen(R.dimen.xxx);   图片资源drawable的使用 Resources r = getResources(); Drawable d = r.getDrawable(R.drawable.xxx); ((ImageView)findViewById(R.id.xx)).setImageDrawable(d)   自定义资源的使用 1.建立test.xml 文件   2.代码中使用资源 Resources r = getResources(); XmlResourceparser xrp = r.getXml(R.xml.test); 然后的便是 xml 的解析  见 android 开发详解(60)   ///////////////////////////////////////menu/////////////////////////////////////////// onCreateOptionMenu  //选项菜单。(菜单键) 如果该函数返回false 将会不显示菜单,因此我们可以截获菜单按键(onKeyDown或者onMenuOpened), 再在该函数中显示我们自定义的PopupWindow然后返回false layout = getLayoutInflater().inflate(R.layout....,null); PopupWindow = new PopupWindow(layout,getWindowMananger().getDefaultDisplay().getWidth(),...getHeight()); pop.showAtLocation(layout,Gravity.Bottom,0,0);   onCreateContextMenu (长时间按住!必须要注册) registerForContextMenu(someView);   加载menu资源  getMenuInflater().inflate(R.menu.xxx,menu);       ///////////////////////////////////////对话框///////////////////////////////////////////  new AlertDialog.Builder(this).setIcon(R...).setTitle("title").setXXXXButton("确定",new DialogInterface.OnClickListener(){...}).setMessage("some message").show(); 还有setItem setSigleItem。。。等等     核心是Builder 子类 以及控制字段AlertController ,利用反射可以修改对话框的固有特性!   Field field = alertDialog.getClass().getDeclaredField("mAlert");   field.setAccessible(true);   Object obj = field.get(alertDialog);  //参数指定为字段所在的类的对象    field.set(obj,new AlertController()); //这样便替换了 mAlert 对象了     ///////////////////////////////////////Toast/////////////////////////////////////////// Toast.makeText(this,"some message",Toast.LENGTH_LONG).show();   它是根据一个view 来显示信息的 详细步骤如下 Toast  toast = new Toast(this); toast.setDuration(Toast.LENGTH_LONG);//设置“持续”时间长短、 toast.setView(someView); //someView 才是携带信息的控件 他可以是textView 包含图片文字信息 toast.show();   //toast.show 源代码中指明了 系统中有一个toast 队列show方法只是将该mTN 加入到这个队列中,enqueueToast,然后控制显示关闭 (因此mTN对象中必然是有一个 show 和hide 方法的!!) //因此我们控制类中的mTN 变量就可以控制toast的现实和关闭了。同样利用反射 Field field = toast.getClass().getDeclaredField("mTN"); field.setAccessible(true); Object obj = field.get(toast); Method method = obj.getClass().getDeclaredMethod("show",null); method.invoke(obj,null); ///这样相当于直接控制mTN对象调用show方法显示 toast show参数是null!!       ///////////////////////////////////////notification/////////////////////////////////////////// //记住三个对象就行了  PendingIntent(悬而未决的意图),Notification,NotificationManager, NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification =new Notification(icon,"message",System.getCurrentMillis); PendingIntent intent = PendingIntent.getActivity(this,0,getIntent(),0);   notification.setLastestEventInfo(this,"msg","msg",intent);//关联PendingIntent 和Notification manager.notify(id,notification); //manager 显示notification     ///////////////////////////////////////打开activity/////////////////////////////////////////// Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:121123321")); //参数指定action startActivity(intent); 打开自定义activity 必要条件 : 1.在androidManifest 中注册该Activity,指定intent_filter(action 和 category) 2.编写界面类继承Activlity 3.在onCreate 函数中setContent 加载 xml 布局文件!   ///////////////////////////////////////broadCast 广播/////////////////////////////////////////// 1.编写接受者类 继承BroadCastReceiver override onRecieve方法 处理监听到的动作 2.在AndroidManifest中添加 ... 指定intent_filter的action,用来注册接受者接受的消息(动作!!!)如:android.provider.Telephony.SMS_RECEIVERD 也可以用代码注册:registerReceiver(myReceiverObject,new IntentFilter("...someAction")); 只要接受者对象的生命还没有结束那么将会一直监听该动作 3.在androidManifest中打开相应的权限   发送广播: 用于自定义广播 Intent intent = new Intent("myaction"); //指定特殊的action intent.addCategory("mycategory"); intent.putExtra("name","some data"); sendBrodcast(intent); //这便是重点     ///////////////////////////////////////content provider 内容提者/////////////////////////////////////////// android sdk 提供了很多 content provider 可以读写系统的信息,如:联系人,短信, 支持 增删改查!   contentProvider的使用 1.在androidManifest 文件中打开权限 2.之后便可以对响应的数据进行四种操作了 Cursor cursor =  getContentResolver().query(uri<以content://开头的字符串>,String[]