半方大的空白    全方大的空白    不断行的空白格  < 小于 < <> 大于 > >& &符号 & &" 双引号 " "© 版权 © ©® 已注册商标 ® ®™ 商标(美国) ™ ™× 乘号 × ×÷ 除号 ÷ ÷
半方大的空白    全方大的空白    不断行的空白格  < 小于 < <> 大于 > >& &符号 & &" 双引号 " "© 版权 © ©® 已注册商标 ® ®™ 商标(美国) ™ ™× 乘号 × ×÷ 除号 ÷ ÷
Android字符串资源及其格式化 在Android项目布局中,资源以XML文件的形式存储在res/目录下。为了更好的实现国际化及本地化,字符串集通常以XML文件的形式存储在res/values/目录下。 1、纯文本字符串 一般来说,使用纯文本字符串仅仅需要res/values目录下的一个XML文件(通常命名为res/values/strings.xml,可以使用其它的文件名替换strings),根元素为resources,希望编码为资源的每个字符串都有一个string子元素。String元素包含name特性,它标示了此字符串的唯一名称,还有一个文本元素,包含字符串的文本。 字符串的表示分以下三种情况: a) 普通字符串(不含双引号(”)及单引号(’))。其在XML文件中如代码一所示定义。 代码一: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World!</string> </resources> <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World!</string> </resources> b) 字符串仅含单引号。其在XML文件中如代码二或代码三(使用转义字符反斜杠“/”)所示定义。 代码二: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">"Hello' World!"</string> </resources> <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">"Hello' World!"</string> </resources> 代码三: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello/' World!</string> </resources> <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello/' World!</string> </resources> c) 其它情况下的字符串。其在XML文件中如代码三所示定义,即使用一个前置反斜杠进行转义。代码四给出了一个示例。 代码四: <?xml version="1.0" encoding="utf-8"?> <resources>
1、在java中stream代表一种数据流(源),javaio的底层数据元,---(想像成水龙头)2、任何有能力产生数据流(源)的javaio对象就可以看作是一个InputStream对象既然它能产生出数据,我们就可以将数据取出,java对封装的通用方法就read()方法了--(出水龙头)3、任何有能力接收数据源(流)的javaio对象我们就可以看作是一个OutputStream对象同样,它能接收数据,我们就可以调用它的write方法,来让它接收数据--(进水龙头了,呵呵)4、当然,我们可以在Inputstream和OutputStream数据源的基础上,从实际需要触发,来重新封装出不同性能机制的输入、输出流了,java.io包中提供了很丰富的输入、输出流对象,如:基于字节流的stream:DataOutputStream----DataInputStream:FileOutputStream-----FileInputStream:.............等,可以用InputStream和OutputStream从JDK文档查阅基于字符流的stream(典型的以write和reader来标识的):FileWriter---FileReader:StringWriter---StringReader:.........等,你自己可以用Writer和Reader从JDK文档里头查看说明stream应该是水龙头里的水资源,InputStream:是一个出水龙头(把水封装在里头)的一个实物对象,该对象的read方法呢,就想成这个出水龙头这一机制对象的开关钮,你read或openStream(其他对象包容InputStream对象的对象方法)一下呢,就等于打开了出水龙头的按钮,水就出来了,里头封装的水是什么性质的呢,你就用相应的容器来装,如string或byte[].....OutputStream:你就在InputStream基础上反着想就ok了 -------------------------------------------------------------------------------------------------------------------------OutputStream(1)输出数据 void write(int b)往流中写一个字节b void write(byte b[])往流中写一个字节数组b void write(byte b[],int off,int len)把字节数组b中从下标off开始,长度为len的字节写入流中 (2) flush()刷空输出流,并输出所有被缓存的字节 由于某些流支持缓存功能,该方法将把缓存中所有内容强制输出到流中。(3)关闭流 close()流操作完毕后必须关闭。------------------------------------------------------------------------------------------------------------------------InputStream(1)从流中读取数据 int read() 读取一个字节,返回值为所读得字节 int read(byte b[]) 读取多个字节,放置到字节数组b中,通常读取的字节数量为b的长度,返回值为实际独取的 字节的数量。 int read(byte b[] ,int off,int len)读取len个字节,放置到以下标off开始字节数组b中,返回值为实际 读取的字节的数量。 int available() 返回值为流中尚未读取的字节的数量。 long skip(long n);读指针跳过n个字节不读,返回值为实际跳过的字节数量(2)关闭流 close() 流操作完毕后必须关闭(3)使用输入流中的标记 void mark(int readlimit)纪录当前指针的所在位置.readlimit表示读指针读出的readlimit个字节后 所标记的指针位置才实效。 void reset() 把读指针重新指向用mark方法所记录的位置 boolean markSupported() 当前的流是否支持读指针的记录功能。----------------------------------------------------------------------------------------------------------------------- Java IO通过Stream(流)来实现。关于流,可以理解为是一种“数据的管道”。管道中流动的东西可以是基于字节,也可以是基于字符的等。就好像管道里面可以流动水,也可以流动石油一样。 而对应于流还有一个概念:输入、输出设备。这些设备可以是磁盘文件、键盘(输入设备)、显示器(输出设备)、打印机(输出设备)、网络套接字等等。 下面,我们就来了解“流”。 Java中定义了两种类型的流:字节型,和字符型。
1. 下载android.jar对于版本的source code http://git.source.android.com/?p=platform/frameworks/base.git;a=snapshot;h=android-2.3.1_r1;sf=tgz URL中的android-2.3.1_r1替换成任意你需要版本的sdk tag名称即可。点击tage名称列表查看所有有效的tag名称。 利用这个URL下载到了源代码压缩包base-android-2.3.1_r1-3c67ae1.tar.gz。 2. 设置android.jar源代码目录 在SDK安装目录\android-sdk-windows\platforms\android-9中创建sources目录 解压base-android-2.3.1_r1-3c67ae1.tar.gz,拷贝base-android-2.3.1_r1-3c67ae1\core\java下的文件到刚刚创建的sources目录 在Eclipse中,右键点击项目properties->Java Build Path->Libraries->Andriod 2.3.1 展开Andriod 2.3.1->android.jar->source attachment,点击Edit 按钮,设置android.jar对应的源代码目录。 注,如果以上设置完成之后还是无法显示.如果错误提示为:Could not open the editor: The editor class could not be instantiated. This usually indicates a missing no-arg constructor or that the editor's class name was mistyped in plugin.xml. 则说明你 .class文件的查看方式有问题,可能自己进行过自定义设置进入 preferences--general--Editors--File associations选中*.class 重置下面的associated editors 为class file viewer 其他eclipse 的单词拼写检查有点弱智,有时非常烦,禁掉它。General --> Editors --> Text Editors --> Spelling去掉 Enable spell checking 前面的勾
There is a lovely method on the android.text.Html class, fromHtml(), that converts HTML into a Spannable for use with a TextView. However, the documentation does not stipulate what HTML tags are supported, which makes this method a bit hit-or-miss. More importantly, it means that you cannot rely on what it will support from release to release. I have filed an issue requesting that Google formally document what it intends to support. In the interim, from a quick look at the source code, here’s what seems to be supported as of Android 2.1: * <a href="..."> * <b> * <big> * <blockquote> * <br> * <cite> * <dfn> * <div align="..."> * <em> * <font size="..." color="..." face="..."> * <h1> * <h2> * <h3> * <h4> * <h5> * <h6> * <i> * <img src="..."> * <p> * <small> * <strike> * <strong> * <sub> * <sup> * <tt> * <u>
方法一<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" <include android:id="@+id/includedContainer1" layout="@layout/layoutFile" /></RelativeLayout> 方法二使用mergesome_activity.xml:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> // some views <include layout="@layout/view_part"/> // probably more views</LinearLayout>view_part.xml: <merge xmlns:android="http://schemas.android.com/apk/res/android"> // the views to be merged</merge>
在Eclipse中使用ADT开发Android很方便,但也有些不如意的地方,如XML文件的格式化效果就很不理想。默认情况下,使用ADT的可视化界面布局,自动生成的xml文件中,一个控件的配置全动挤到一行上(效果如下所示),阅读、修改起来都很不方便。<?xml version="1.0" encoding="utf-8"?> <LinearLayout ...> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" ...></TextView> </LinearLayout> 而我希望最佳的格式化效果如下,控件的每个属性配置占一行方便阅读修改,没有子元素的控件直接使用 /> 关闭: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/TextView01" android:text="Some Content" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 为了达到上述的格式化效果,需要修改Eclipse的一些配置及进行一些额外的操作: 1)修改Eclipse的XML格式化配置 这一步的配置是使格式化的效果为控件的每个属性配置占一行。进入 Window/Preferences,展开到 XML/XML Files/Editor, 勾选 “Split multiple attributes each on a new line” 即可,如下图所示: 经此配置后,每次使用快捷键 Ctrl+Shift+F 键格式化后每个属性配置就会占一行。 2)压缩节点的声明方式 这步的目的是将没有子节点的元素的声明方式进行压缩,如将 “<TextView ...></TextView>” 转化为 “<TextView .../>”。 方法为在XML文件内空白地方点击鼠标右键,选择 ”Source/Cleanup Document...“,如下图所示: 之后将弹出如下界面: 勾选“Compress empty element tags”,点击 OK 按钮即可。 为了方便,可以为此操作添加一个快捷键,进入 Window/Preferences,展开到 General/Keys,如下图配置自己喜欢的快捷键即可:
做应用时,可能会需要动态改变控件的背景图片,如果仅仅是简单的点击,选中之类的事件,如果靠程序中写监听的代码就显得太麻烦了,在这种情况下,你可以使用selector动态改变控件背景拉:) 1。在res/drawable目录下建一个mybutton.xml文件,根据需要,不同的状态下建立不同的item,并对应相应的图片 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:drawable="@color/transparent" /> <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. 注意这句话--> <item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/selector_background_disabled" /> <item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/lselector_background_disabled" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/selector_background_transition" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/selector_background_transition" /> <item android:state_focused="true" android:drawable="@drawable/selector_background_focus" /> </selector> 2。在构造layout是引用这个xml <ImageButtonandroid:id="@+id/ImageButton01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/mybutton"></ImageButton> 这样就ok拉
String.getBytes(charsetName),这个方法很多人都用过,可是有没有试过temp.getBytes("Unicode");这样的用法,它的返回值很奇怪,第1和第2个字节是-1或-2,比如下面的代码,你能想象出它的结果吗? String temp = "a"; try { byte[] unicodes = temp.getBytes("Unicode"); System.out.println("unicodes=" + unicodes.length); for (int i = 0; i < unicodes.length; i++) { System.out.println(unicodes[i]); } unicodes = temp.getBytes("UnicodeLittleUnmarked"); System.out.println("unicodes=" + unicodes.length); for (int i = 0; i < unicodes.length; i++) { System.out.println(unicodes[i]); } unicodes = temp.getBytes("UnicodeBigUnmarked"); System.out.println("unicodes=" + unicodes.length); for (int i = 0; i < unicodes.length; i++) { System.out.println(unicodes[i]); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } 输出结果: unicodes=4-1-2970unicodes=2970unicodes=2097 为什么会有这种结果呢?蓝色的返回了四个字节,-1,-2,是字节顺的一种表示,这是由sun的类库实现,指示如果没有指定字节就使用默认的UnicodeLittle(在Window平台,别的平台我没测试),但为了标识这种字节顺,就使用了-1,-2在前面表示。 黑色的字,是UnicodeLittleUnmarked的结果,其返回字节只是两个字节,这与Unicode的编码相符合,注意到97,0与使用Unicode时后两个字节顺序一样。 红色的字,是使用UnicodeBigUnmarked的结果,字节顺与Little相反,也没有-1,-2. 由以上应该知道,temp.getBytes("Unicode");应该小心使用,应该注意它返回的-1,-2,这两个字节,因为在一些网络程序中,特别是当对方是由java以外的语言编写,有可能不会使用这种方式来标识字节顺,因此要了解对方的细节,这样才能保证数据的准确传递。
1.有符号和无符号 c#中字节byte的范围是0~255; java中字节byte的范围是-128~127: 2.高低位顺序不同 比如16位整数10用16进制000A,在.net转换成byte数组是00 0A,而java/flash/flex等转换是0a 00 NET 转JAVA BitConverter.ToInt32(bytes, 0);(C#) JAVA:public static int ToInt32(byte[] bytes, int startIndex) { int l = (int) bytes[startIndex] & 0xFF; l += ((int) bytes[startIndex + 1] & 0xFF) << 8; l += ((int) bytes[startIndex + 2] & 0xFF) << 16; l += ((int) bytes[startIndex + 3] & 0xFF) << 24; return l; }