小問題總結

1、一般在啓動虛擬機的時候可以通過cmd中的命令來啓動對應的虛擬機並加載sdcard虛擬鏡像。但是在eclipse中的對某個具體的project可以在它的run configurations中的target標籤中,設置application additional command中的額外的命令選項,在這裏加上對應的sdcard的啓動命令 : -sdcard F:\Android\mySmallSDcard ,那麼以後在通過eclipse啓動模擬器時,就會自動加載sdcard虛擬鏡像。
2、對於UI的微調可以通過 windows自帶的 畫圖程序 來修改 ,可以對某個區域的顏色進行復制黏貼。從UI設計的角度來看,圖片的漸變色增加了立體感,而圖片和背景色本身的重疊能夠消除圖片中的不好看的顏色。
3、在eclipse中的source標籤中,shift left和shift right對於整體移動代碼塊非常方便。通過使用Android中layout文件的可視化視圖能夠大概的看到layout文件的效果,但是與實際的效果還是有差距的。
4、爲了能夠實現在多個用標籤表示的程序之間隨意跳轉的功能,其實比較方便的一種方法就是:在調用startActivity(..)從一個Intent跳轉到另一個Intent後,調用finish()結束跳轉前的Intent.

5、Android UI設計中的佈局
(1)FrameLayout :最簡單的佈局模型,在這種佈局下每個添加的子控件都被放在佈局的左上角,並覆蓋在前一子控件的上層。另外需要注意的是:當使用ImageView顯示圖片時,應當使用android:src指定顯示的圖片,而不是使用android:background,否則和內容相關的操作將不起作用
(2)RelativeLayout:子控件會根據它們所設置的參照控件和參數進行相對佈局。參照控件可以是父控件,也可以是其他子控件,但被參照的控件必須要在它之前定義。相對佈局模型涉及到的屬性設置比較多。
-- 將父控件作爲參照控件的屬性,例如:android:layout_centerInParent="true",將當前控件放置於父控件的橫向和縱向的中央部分。這樣的屬性只能設置爲boolean類型的值,true或false。
-- 將其他控件作爲參照控件的屬性,例如:android:layout_below="@id/aclock",將當前控件放置於id引用名爲aclock的下方,其值必須是一個id的引用名。
-- 以尺寸值作爲屬性值,例如:android:layout_marginLeft="40px"
(3)LinearLayout:是最常用的一種佈局方式,提供了控件水平或者垂直排列的模型,同時可以設置子控件的weight佈局參數控制各個子控件在佈局中的相對大小。
常用的一些屬性有:android:layout_weight 寬度
    android:layout_height 高度
    android:orientation   排列方式
    android:gravity       對齊方式:如果無子控件的View設置這個屬性,表示其內容的重力傾向,即對齊方式;若是有子控件的View,則設置的是其子控件的對齊方式。多個gravity的值可以通過“|”來組合使用。


6、菜單和對話框
(1)菜單:Android平臺所提供的菜單大致可分爲三類:選項菜單(Options Menu),上下文菜單(Context Menu),子菜單(Submenu)。

--1、選項菜單:可以在底部彈出帶圖標的選項菜單,最多隻能顯示6個菜單項,但設置了6個以上的菜單項後,就只能顯示5個菜單項,最後一個菜單項就變成了“更多”字樣,而點擊“更多

”,就會彈出擴展選項菜單,擴展菜單選項不支持圖標,但可以有單選框和複選框。

第一次調用選項菜單時,Activity會調用onCreateOptionsMenu(),需要重新實現override,並在裏面初始化選項菜單就可以了。
如:
 /*
  * Options Menu
  */
 private final int MENU_START = Menu.FIRST;
 private final int MENU_STOP = Menu.FIRST+1;
 private boolean startOrStop = true ;
 
 @Override
 public boolean onCreateOptionsMenu(Menu menu){
  menu.add(0, MENU_START, 0, "錄音").setIcon(android.R.drawable.ic_media_play);
  menu.add(0, MENU_STOP, 1, "停止").setIcon(android.R.drawable.ic_media_pause).setEnabled(false);
  return true;
 }
需要注意的是:菜單項的Id必須是唯一的,爲的是事件偵聽。
MenuItem可選的設置有:單選框或複選框、快捷鍵、短標題、圖標、監聽菜單項單擊事件(onMenuItemClickListener),設置Intent。
除了上面的提到的給菜單項設置單擊事件偵聽,也可以重新實現onOptionsItemSelected。
如:
 @Override
 public boolean onOptionsItemSelected(MenuItem item){
  super.onOptionsItemSelected(item);
  switch(item.getItemId()){
  case MENU_START:
   /*
    * Do some thing
    */
   startOrStop = false ;
   break;
  case MENU_STOP:
   /*
    * Do some thing
    */
   startOrStop = true ;
   break;
  default:
   break;
  }
  return false ;
 }
在第一次初始化了選項菜單後,如果需要動態更改菜單選項的話,就可以重新實現onPrepareOptionsMenu(),它會在每次顯示選項菜單之前調用。可以在這個函數中根據程序的運行情況即時

地更新菜單項的內容,如標題,是否可用等等。
如:
 @Override
 public boolean onPrepareOptionsMenu(Menu menu){
  super.onPrepareOptionsMenu(menu);
  
  MenuItem item1 = menu.findItem(MENU_START);
  MenuItem item2 = menu.findItem(MENU_STOP);
  
  if( startOrStop == true ){
   item1.setEnabled(true);
   item2.setEnabled(false);
  }
  else{
   item1.setEnabled(false);
   item2.setEnabled(true);
  }
   
  
  return true ;
 }

7、經常遇到且很有用的幾個類及其介紹

--java.lang.Runnable
Represents a command that can be executed. Often used to run code in a different Thread.
表示可以被執行的一條命令,通常在另外一個不同的線程中執行代碼。主要覆蓋public void run()函數。


--android.os.Message
Defines a message containing a description and arbitrary data object that can be sent to a Handler. 
This object contains two extra int fields and an extra object field that allow you to not do allocations in many cases. 
While the constructor of Message is public, the best way to get one of these is to call Message.obtain() or one of the Handler.obtainMessage() methods, 
which will pull them from a pool of recycled objects.
定義了一個Message,它包含了一個描述和任意數據對象,可以被髮送給某個Handler。這個對象包含了兩個額外的int域和一個額外的對象域,在很多情況下避免了空間分配。
雖然Message的構造函數是公共的,但是最好的方法是通過Message.obtain()或Handler.obtainMessage()中的某一個來得到,系統會從一個回收的對象池中拉一個對象出來。
Message.what  :  User-defined message code so that the recipient can identify what this message is about.
Message.obj   :  An arbitrary object to send to the recipient. When using Messenger to send the message across processes this can only be non-null if it 
     contains a Parcelable of a framework class (not one implemented by the application).


--android.os.Handler
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single 
thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the 
thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue. 
There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed 
on a different thread than your own. 
Handler用於給相關聯的某個線程的MessageQueue發送Message和處理Runnable對象。每個Handler對象實例只和單個線程和它的消息隊列相關。當創建一個新的Handler,它就和創建創建
它的線程/線程的消息隊列綁定,從這一刻起,Handler就會向這個消息隊列發送消息和runnables對象,並且當消息從隊列中出來時執行它們。
boolean android.os.Handler.sendMessage(Message msg) : Pushes a message onto the end of the message queue after all pending messages before the current time. It will be received in handleMessage(Message), in the thread attached to this handler.
boolean android.os.Handler.postDelayed(Runnable r, long delayMillis) : Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached.

--android.os.Looper
Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread 
that is to run the loop, and then loop() to have it process messages until the loop is stopped. 
Most interaction with a message loop is through the Handler class. 
用於爲一個線程運行message loop。線程默認是沒有message loop的,要創建時,可以在將要運行loop的線程中調用prepare(),然後調用loop()讓這個線程處理這些消息直到loop停止。
和message loop的大多數交互是通過Handler類進行的。
MessageQueue android.os.Looper.myQueue() : 返回和當前線程相關的MessageQueue對象。
void android.os.MessageQueue.addIdleHandler(IdleHandler handler) : 添加MessageQueue的IdleHandler。

--android.os.MessageQueue.IdleHandler
Callback interface for discovering when a thread is going to block waiting for more messages.
發現一個線程什麼時候將要阻塞以等待更多的消息的回調接口。

--android.widget.PopupWindow
void android.widget.PopupWindow.showAtLocation(View parent, int gravity, int x, int y) : 在某個具體的位置顯示PopupWindow對象
void android.widget.PopupWindow.update(int x, int y, int width, int height) : 將PopupWindow對象顯示在指定位置
void android.widget.PopupWindow.dismiss() : 拋棄PopupWindow對象。

--SeekBar控件在佈局文件中的主要的屬性有:
android:progressDrawable 進度條的背景圖片和進度條(一般採用@drawable/xxx.xml佈局文件,並使用layer-list一次加載多個層次圖片)
android:thumb 進度條中的小球(一般採用@drawable/xxx.xml佈局文件,並使用selector根據控件屬性變化,加載不同的圖片)
android:progress 進度條當前的進度
android:secondaryProgress 二級進度條

SeekBar主要的偵聽事件:SeekBar.onSeekBarChangeListener,偵聽包括手動滑動滑鈕,程序執行等情況下的progress變化。

<LinearLayout android:layout_width="500px"
   android:layout_height="51px"
   android:layout_above="@id/video_line"                 
   >
   
   <LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="bottom">
    
    <LinearLayout android:layout_width="fill_parent"
     android:layout_height="50px"
     android:gravity="center_horizontal"
     android:layout_alignParentBottom="true">
     
     <LinearLayout android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:background="@drawable/mm_background"
      android:orientation="horizontal">
上面的四個層次的LinearLayout都不是多餘的,layout_above指定相對位置 ,gravity="bottom"指定整體靠底部  gravity="center_horizontal"指定整體水平居中 
orientation="horizontal"指定對平排列

8、調試過程中遇到的小錯誤提示:

1、findViewById()時要注意寫明針對的是哪一個view,如果不指定,默認是在當前view下面的,發生錯誤時會出現NullPointerException,沒有找到對應的控件id。

2、實際應用程序的測試需要到真機上實現,模擬器在測試的時候有些功能是看不到的,或者說在模擬器上正確的在真機上就不一定正確,反之亦然。
3、在Android UI設計中需要注意的是一般都要把圖標放在draw-hdpi的文件夾下,否則在大小顯示的時候會有問題。

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