Android学习笔记(一)

 

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

1、 R.id cannot be resolved? 发现没有“ok”项

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

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

<Button android:id="@+id/ok" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="OK" /> 

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

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

<RadioGroup android:id="@+id/RadioGroup01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_x="3px" android:layout_y="54px" > <RadioButton android:id="@+id/RadioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Windows" /> </RadioGroup> 

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

android:text="@string/Button1"

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

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

To start, create a new folder in your project res/ directory called menu. This is where you should keep all XML files that define your application menus.(创建Xml文件的办法

In a menu XML layout, there are three valid elements: <menu>, <group> and <item>. The item and group elements must be children of a menu, but item elements may also be the children of a group, and another menu element may be the child of an item (to create a Submenu). Of course, the root node of any file must be a menu element.

As an example, we'll define the same menu created in the menu section, above. We start with an XML file named options_menu.xml inside the res/menu/ folder:

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_game" android:title="New Game" /> <item android:id="@+id/quit" android:title="Quit" /> </menu> 

Then, in the onCreateOptionsMenu() method, we inflate this resource using MenuInflater.inflate():

public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater= getMenuInflater(); inflater.inflate(R.menu.options_menu, menu); return true; } 

The getMenuInflater() method returns the MenuInflater for our activity's context. We then call inflate(), passing it a pointer to our menu resource and the Menu object given by the callback.

While this small sample may seem like more effort, compared to creating the menu items in the onCreateOptionsMenu() method, this will save a lot of trouble when dealing with more items and it keeps your application code clean.

You can define menu groups by wrapping item elements in a group element, and create Submenus by nesting another menu inside an item. Each element also supports all the necessary attributes to control features like shortcut keys, checkboxes, icons, and more. To learn about these attributes and more about the XML syntax, see the Menus topic in the document.

4、问题Error in an XML file: aborting build.

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

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

解决办法: 

Project Explorer里删除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,完成啦,这是不是很简单呢!Android学习笔记(一)

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

 

你可能感兴趣的:(Android学习笔记(一))