---
title: "Android学习笔记（一）"
author: "horace20"
date: "2011-06-09T21:24:00Z"
tags: []
slug: "android"
draft: false
lastmod: "2014-09-15T11:09:42Z"
id: 2116533
url: "https://horace20.iteye.com/blog/2116533"
---

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

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

Buttonbutton\_ok=(Button)findViewById(R.id.ok);

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

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

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

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

android:text="@string/Button1"

再在strings.xml中定义如下：&lt;stringname="Button1"&gt;Windows&lt;/string&gt;

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

Tostart,createanewfolderinyourprojectres/directorycalledmenu.ThisiswhereyoushouldkeepallXMLfilesthatdefineyourapplicationmenus.（创建Xml文件的办法）

InamenuXMLlayout,therearethreevalidelements:&lt;menu&gt;,&lt;group&gt;and&lt;item&gt;.Theitemandgroupelementsmustbechildrenofamenu,butitemelementsmayalsobethechildrenofagroup,andanothermenuelementmaybethechildofanitem(tocreateaSubmenu).Ofcourse,therootnodeofanyfilemustbeamenuelement.

Asanexample,we'lldefinethesamemenucreatedinthemenusection,above.WestartwithanXMLfilenamedoptions\_menu.xmlinsidetheres/menu/folder:

Then,intheonCreateOptionsMenu()method,weinflatethisresourceusing[MenuInflater.inflate()]():

The[getMenuInflater()]()methodreturnsthe[MenuInflater]()forouractivity'scontext.Wethencall[inflate()](),passingitapointertoourmenuresourceandtheMenuobjectgivenbythecallback.

Whilethissmallsamplemayseemlikemoreeffort,comparedtocreatingthemenuitemsintheonCreateOptionsMenu()method,thiswillsavealotoftroublewhendealingwithmoreitemsanditkeepsyourapplicationcodeclean.

Youcandefine[menugroups](http://control.blog.sina.com.cn/admin/article/article_add.php#groups)bywrappingitemelementsinagroupelement,andcreateSubmenusbynestinganothermenuinsideanitem.Eachelementalsosupportsallthenecessaryattributestocontrolfeatureslikeshortcutkeys,checkboxes,icons,andmore.TolearnabouttheseattributesandmoreabouttheXMLsyntax,seetheMenustopicinthedocument.

4、问题ErrorinanXMLfile:abortingbuild.

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

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

解决办法：

从ProjectExplorer里删除main.out.xml.

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

如果还不行,运行project-&gt;clean…

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

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

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

另外一个更为简单的办法是，copy相应的资源，然后在工程中选中/res/drawable文件夹，Ctrl+V，完成啦，这是不是很简单呢！![Android学习笔记（一）](http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___6708ZHE1SIGG.gif "Android学习笔记（一）")

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