Android學習筆記(一)

 

   有關用戶界面開發學習中遇到的問題,現將問題解答辦法分享與大家,希望對志同道合的朋友有用!

1、 R.id cannot be resolved? 發現沒有“ok”項

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

解決辦法:在layout中添加如下部分

 

用到button必須在main.xml中添加聲明!

2、在界面佈局中使用RadioButton時應注意XML描寫如下

 

切記注意紅色部分!該部分有兩種表示方法,一是如上邊的表示辦法,另一種則是:

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:

 

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

 

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中不會生成新的變量,這樣程序裏也就沒辦法調用。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章