ITeye博客

Android学习笔记(一)

horace20 · 2011-06-09 21:24 · Markdown

有关用户界面开发学习中遇到的问题,现将问题解答办法分享与大家,希望对志同道合的朋友有用!

1、R.idcannotberesolved?发现没有“ok”项

Buttonbutton_ok=(Button)findViewById(R.id.ok);

解决办法:在layout中添加如下部分

用到button必须在main.xml中添加声明!

2、在界面布局中使用RadioButton时应注意XML描写如下

切记注意红色部分!该部分有两种表示方法,一是如上边的表示办法,另一种则是:

android:text="@string/Button1"

再在strings.xml中定义如下:<stringname="Button1">Windows</string>

3、怎样在Xml文件中定义菜单?

Tostart,createanewfolderinyourprojectres/directorycalledmenu.ThisiswhereyoushouldkeepallXMLfilesthatdefineyourapplicationmenus.(创建Xml文件的办法)

InamenuXMLlayout,therearethreevalidelements:<menu>,<group>and<item>.Theitemandgroupelementsmustbechildrenofamenu,butitemelementsmayalsobethechildrenofagroup,andanothermenuelementmaybethechildofanitem(tocreateaSubmenu).Ofcourse,therootnodeofanyfilemustbeamenuelement.

Asanexample,we'lldefinethesamemenucreatedinthemenusection,above.WestartwithanXMLfilenamedoptions_menu.xmlinsidetheres/menu/folder:

Then,intheonCreateOptionsMenu()method,weinflatethisresourceusingMenuInflater.inflate():

ThegetMenuInflater()methodreturnstheMenuInflaterforouractivity'scontext.Wethencallinflate(),passingitapointertoourmenuresourceandtheMenuobjectgivenbythecallback.

Whilethissmallsamplemayseemlikemoreeffort,comparedtocreatingthemenuitemsintheonCreateOptionsMenu()method,thiswillsavealotoftroublewhendealingwithmoreitemsanditkeepsyourapplicationcodeclean.

Youcandefinemenugroupsbywrappingitemelementsinagroupelement,andcreateSubmenusbynestinganothermenuinsideanitem.Eachelementalsosupportsallthenecessaryattributestocontrolfeatureslikeshortcutkeys,checkboxes,icons,andmore.TolearnabouttheseattributesandmoreabouttheXMLsyntax,seetheMenustopicinthedocument.

4、问题ErrorinanXMLfile:abortingbuild.

第一次用更改main.xml的方式,结果每次编译就产生一个main.out.xml,一直错误就是执行不了。

出现以上问题,是因为当前的编辑文件是xml,所以eclipse自动调用xsl运行。所以会出错。

解决办法:

从ProjectExplorer里删除main.out.xml.

运行时,确保正在打开的文件是src下的文件。

如果还不行,运行project->clean…

5、Android工程中添加图片资源

在Android工程中,每添加一个资源,就会在gen目录下的R.java中自动生成一个新的静态整型变量来指向这个资源。程序文件中调用资源的时候,先在R.java中找到变量名,然后根据变量值查找资源。

可以直接在工程的/res/drawable文件夹里拷贝一个图片文件过去。然后在eclipse里右键点Project->Clean。在res–drawable标签下就会多出来你添加的图片。R.java中也会自动添加一个以图片文件名为变量名的整型变量。

另外一个更为简单的办法是,copy相应的资源,然后在工程中选中/res/drawable文件夹,Ctrl+V,完成啦,这是不是很简单呢!

注意:文件名必须全为小写。如果有大写的话,R.java中不会生成新的变量,这样程序里也就没办法调用。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。