Desktop softwares and information
Programming
error RC1106: invalid option: -ologo
Aug 30th
After switch to vs2010, if you encounter the error:
Error 1 error RC1106: invalid option: -ologo
not only set the “Suppress Startup Banner” to “No” in Properties->Resources, but also need to set the “Enable Incremental Linking” to “No” in Properties->Linker.
给Android程序添加不同的广告平台
Jun 4th
本文假设已有一个广告,比如admob,然后添加另一广告,比如有米,用来在根据不同的语言自动调用不同的广告平台。
1. 在attrs.xml中追加一个declare-stylea,原有的属性不需要再定义。结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | < ?xml version="1.0" encoding="UTF-8"?> <resources> <declare -styleable name="com.admob.android.ads.AdView"> <attr name="testing" format="boolean" /> <attr name="backgroundColor" format="color" /> <attr name="textColor" format="color" /> <attr name="keywords" format="string" /> <attr name="refreshInterval" format="integer" /> <attr name="isGoneWithoutAd" format="boolean" /> </declare> <declare -styleable name="net.youmi.android.AdView"> <attr name="changeAdAnimation" format="boolean"/> <attr name="backgroundTransparent" format="integer"/> </declare> </resources> |
2. 为不同的区域创建不同的layout
首先在默认的layout目录中创建ads.xml文件,用于存放广告代码。在工程目录上右击,New->Android XML File,输入文件名ads.xml,结束。
然后为特定的语言创建ads.xml。在工程目录上右击,New->Android XML File,输入文件名ads.xml选择资源类型为Layout,添加Region为cn,添加Language为zh,结束。
3. 把针对于非中文的广告配置移动到layout/ads.xml文件中,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | < ?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:admobsdk="http://schemas.android.com/apk/res/com.deskangel.dacalc" android:layout_width="wrap_content" android:layout_height="wrap_content"> <com .admob.android.ads.AdView android:id="@+id/ad" android:layout_width="fill_parent" android:layout_height="wrap_content" admobsdk:backgroundColor="#000000" admobsdk:textColor="#FFFFFF" admobsdk:keywords="Android application" admobsdk:refreshInterval="60" admobsdk:testing="false" /> </merge> |
4. 把针对中文的广告配置移动到layout-zh-rCN/ads.xml文件中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | < ?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:umadsdk="http://schemas.android.com/apk/res/com.deskangel.dacalc" android:layout_width="wrap_content" android:layout_height="wrap_content"> <net .youmi.android.AdView android:id="@+id/ad" android:layout_width="fill_parent" android:layout_height="wrap_content" umadsdk:refreshInterval="30" umadsdk:changeAdAnimation="false" umadsdk:backgroundTransparent="255" umadsdk:testing="false" umadsdk:isGoneWithoutAd="true" /> </merge> |
5. 在需要显示广告的地方添加:
1 2 3 4 5 | <include android:layout_height="wrap_content" layout="@layout/ads" android:id="@+id/incAds" android:layout_width="fill_parent" > </include> |
大功告成!非常简单的配置。
Android下的ListView一般应用指南
Apr 27th
一,显示:
ListView的Layout需要两个xml。第一个xml,在其中添加ListView本身,把ListView添加到需要显示的位置;第二个xml,则是定义ListView需要显示的内容的Layout。
定义ListView内容的Layout和一般的Layout一样,除了一些限制,比如不要在里面添加EditText之类带输入焦点的View。
info_list.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:layout_height="wrap_content"
android:id="@+id/txtName" android:layout_width="fill_parent" android:layout_weight="0.7" android:text="name" android:layout_marginRight="1px" android:background="#c0ffffb0" android:textColor="#000000"></TextView>
<TextView
android:layout_height="wrap_content"
android:id="@+id/txtExp"
android:layout_width="fill_parent" android:layout_weight="0.3" android:text="expression" android:background="#c0ffb600" android:textColor="#000000"></TextView>
</LinearLayout>
把ListView内容的Layout和ListView绑定在一起就可以显示了。一般的应用,使用SimpleAdapter就可以了:
SimpleAdapter listItemAdapter = new SimpleAdapter(this,
listItem,
R.layout.inf_list,
new String[] {"lstItmDate", "lstItmWeek"},
new int[] {R.id.txtvwWeek,R.id.txtvwWeekInfo});
SimpleAdapter的构造需要使用到一个List (listItem),使用如下的定义可以创建一个List:
ArrayList> listItem = new ArrayList>();
ArrayList需要使用到hashmap:
HashMap mapBase = new HashMap();
HashMap用来存储一对数值,比如:
mapBase.put("lstItmDate", "2010 04 26");
mapBase.put("lstItmWeek", "Monday");
可以很明显的看出来,HashMap中的”lstItmDate”和”lstItmWeek”与SimpleAdapter中的对应。
把HashMap添加到ArrayList中,显示部分就完成了:
listItem.add(mapBase);
所以,作为总结,一个ListView需要涉及如下几个部分:
- HashMap
- ArrayList
- SimpleAdapter
- ListView
滚动到TextView底部
Apr 22nd
在Android,一个单独的TextView是无法滚动的,需要放在一个ScrollView中。ScrollView提供了一系列的函数,其中fullScroll用来实现home和end键的功能,也就是滚动到顶部和底部。
但是,如果在TextView的append后面马上调用fullScroll,会发现无法滚动到真正的底部,这是因为Android下很多(如果不是全部的话)函数都是基于消息的,用消息队列来保证同步,所以函数调用多数是异步操作的。当TextView调用了append会,并不等text显示出来,而是把text的添加到消息队列之后立刻返回,fullScroll被调用的时候,text可能还没有显示,自然无法滚动到正确的位置。
解决的方法其实也很简单,使用post:
final ScrollView svResult = (ScrollView) findViewById(R.id.svResult);
svResult.post(new Runnable() {
public void run() {
svResult.fullScroll(ScrollView.FOCUS_DOWN);
}
});
EditText在ListView中内容被清空的问题
Mar 31st
遇到这个问题是在设计一个Android程序的Ui的时候,当把EditText放在ListView中之后,就遇到了两个问题:
- 点击EditText,虽然弹出了软键盘,但是没有立刻获得输入焦点
- 输入完成之后,按回车或是返回键退出软键盘的时候,EditText框中的输入会被清空
Google无果之后,只有自己研究,谁知这两个问题就花了我几个小时的测试。测试了Android v1.5~v2.1之后,发现原来这两个问题都和ListView的一个参数有关系,即只要没有把ListView的android:layout_height设置成“fill_parent”,就万事大吉。
这个限制没有丝毫道理,应该是ListView的一个讨厌的bug。
但是等一下,ListView是一个scroll container,如果item的个数较多,出现了滚动条,会不会有问题?应该是没有问题,这完全不搭嘎嘛!可惜事实不是这样的。如何避免这个问题,还需要再行研究。
Recent Comments