關於Android的開發經驗總結

來源:互聯網
上載者:User

——第二屆 Google 暑期大學生部落格分享大賽 - 2011 Android 成長篇 

    
最近一直在開發android上的應用程式,自己自學,想實現一個網上商城的用戶端,經過不斷地努力,經過不斷地受挫,不斷地提高,回過頭自己可以去總結一些開發android的經驗。寫這篇的部落格的目的是能夠協助其他想開發android的同道中人。

     首先,如果想要學習開發android應用程式,入門層級的開發人員,可以先去理解android本身提供的21種控制項。這個過程中,自己要反覆地去寫程式,去按著自己的興趣去開發一些小的程式。比如,可以去開發一個登陸介面,一個猜拳遊戲,等等。另外,這個過程中,免不了應用到android的基礎的五種布局方式。通過大量地編寫demo去理解布局方式的意義。

    概括地說,初步入門開發android要做的事情就是:熟練使用21控制項+5種布局方式。

    第二步,隨著開發android的深入,隨著對各種控制項的熟練程度,對於UI的設計,你會越來越不滿意android內建的控制項,你會開始去開發自己定製的控制項。例如一個按鈕的點擊效果,tabwidget的靈活使用等等。這個學習的過程,是充滿困難與糾結的,因為網上的資料雖然多,但是大多數都是簡單控制項使用。其實,你能開始自己定製控制項就走在了許多android的開發人員之前了。所以自己嘗試著定製控制項,是一個充滿困難,但是又充滿樂趣的過程。一會兒我會講解兩種基本的定製。 

    第三步,突破了前個過程後,不要沾沾自喜,因為距離android的大牛還有很長的一段路要走。因為這個時候,你要開始理解android的內部機制,就必須瞭解linux內部原理。這個也得學習一段時間才能逐漸掌握。但是進入android的核心研究的話,你也就可以研究android上的安全問題,等等。因為筆者本人也就是在第二個階段,所以對於以後的學習也不是很瞭解,所以也請大家見諒了。

    接下來,我來介紹兩種常用的定製方式,一個是按鈕點擊事件的處理,另外就是tabwidget的不同模式。

    首先是按鈕點擊事件的處理。先看下面的兩個圖,發現“登陸”按鈕在不點擊的時候是紅色的,但是在在點擊的瞬間是黑色的,這是怎麼做到的呢?

    

其實,我是用了在button上貼圖的方式,一個圖是紅色的,另外一張圖是黑色的來實現的效果。下面是xml代碼:

<Button 
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/home_login_button_bk"
android:layout_gravity="right"
android:layout_marginTop="3px"
/>

其中“home_login_button_bk”是另外的一個xml檔案,用這個檔案來改變按鈕的效果,這個檔案在drawable檔案夾下,代碼:

<?xml version="1.0" encoding="utf-8" ?> 
  <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_window_focused="false" 
        android:drawable="@drawable/home_head_login_normal" /> 
    <item android:state_focused="true" android:state_enabled="false" 
        android:state_pressed="true" 
        android:drawable="@drawable/home_head_login_normal" /> 
    <item android:state_focused="true" android:state_enabled="false" 
        android:drawable="@drawable/home_head_login_normal" /> 
    <item android:state_focused="true" android:state_pressed="true
        android:drawable="@drawable/home_head_login_selected" /> 
    <item android:state_focused="false" android:state_pressed="true" 
        android:drawable="@drawable/home_head_login_selected" /> 
    <item android:state_focused="true" 

        android:drawable="@drawable/home_head_login_selected" /> 

</selector> 

只用改變相應的圖片就能實現效果。


接下來是tabwidget的不同模式,先看效果

                                 

首先是tabwidget在螢幕下方,實現的代碼:

<FrameLayout    
            android:gravity="center"    
            android:id="@android:id/tabcontent"    
            android:layout_width="fill_parent"    
            android:layout_height="wrap_content"    
            android:layout_weight="1.0"
            android:background="@drawable/home_backgroud" > 

            <LinearLayout android:id="@+id/first"  
                android:orientation="vertical"               
                android:layout_width="fill_parent"  
                android:layout_height="fill_parent"  
               >  
            </LinearLayout>  
            </FrameLayout>
   
             <TabWidget    
            android:id="@android:id/tabs"    
           
            android:padding="3.0dip"    
            android:layout_width="fill_parent"    
            android:layout_height="wrap_content"    
            android:layout_weight="0.0" 
            android:tabStripEnabled="false"/>  

在布局上先在<TabWidget></TabWidget>上面去保留一個<FrameLayout></FrameLayout>標籤,來保持位置,這樣tabwidget就被“擠”到了螢幕的下方。

android:tabStripEnabled="false"保證了tabwidget的底線被去掉了。

另外,實現不同的tabwidget的change效果。

myTabHost.setOnTabChangedListener(new OnMyTabChangeListener());來設定監聽器

下面是全部代碼。

package EPurch.android.app;import android.app.TabActivity;import android.content.Intent;import android.content.pm.ActivityInfo;import android.content.res.Resources;import android.os.Bundle;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.TabHost;import android.widget.TabWidget;import android.widget.TabHost.TabSpec;import android.widget.TabHost.OnTabChangeListener;public class Home extends TabActivity{private TabHost myTabHost ;private TabWidget myTabWidget ;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,    WindowManager.LayoutParams.FLAG_FULLSCREEN);    //強制設定為豎向    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);setContentView(R.layout.home);myTabHost = getTabHost();myTabWidget = getTabWidget();Resources myResources = getResources();TabSpec myTabSpec;Intent intent;intent = new Intent(this,First.class);myTabSpec = myTabHost.newTabSpec("tab1").setIndicator(null, null).setContent(intent);myTabHost.addTab(myTabSpec);intent = new Intent(this,Classify.class);myTabSpec = myTabHost.newTabSpec("tab2").setIndicator(null, null).setContent(intent);myTabHost.addTab(myTabSpec);intent = new Intent(this,Search.class);myTabSpec = myTabHost.newTabSpec("tab3").setIndicator(null, null).setContent(intent);myTabHost.addTab(myTabSpec);intent = new Intent(this,ShopCar.class);myTabSpec = myTabHost.newTabSpec("tab4").setIndicator(null, null).setContent(intent);myTabHost.addTab(myTabSpec);intent = new Intent(this,More.class);myTabSpec = myTabHost.newTabSpec("tab5").setIndicator(null, null).setContent(intent);myTabHost.addTab(myTabSpec);View myView;myView = myTabWidget.getChildAt(0);myView.setBackgroundResource(R.drawable.home_menu_home_selected);myView = myTabWidget.getChildAt(1);myView.setBackgroundResource(R.drawable.home_menu_class_normal);myView = myTabWidget.getChildAt(2);myView.setBackgroundResource(R.drawable.home_menu_search_normal);myView = myTabWidget.getChildAt(3);myView.setBackgroundResource(R.drawable.home_menu_shopping_normal);myView = myTabWidget.getChildAt(4);myView.setBackgroundResource(R.drawable.home_menu_more_normal);myTabHost.setCurrentTab(0);myTabHost.setOnTabChangedListener(new OnMyTabChangeListener());}class OnMyTabChangeListener implements OnTabChangeListener{public void onTabChanged(String tabTag){View myView;switch(myTabHost.getCurrentTab()){case 0 :myView = myTabWidget.getChildAt(0);myView.setBackgroundResource(R.drawable.home_menu_home_selected);myView = myTabWidget.getChildAt(1);myView.setBackgroundResource(R.drawable.home_menu_class_normal);myView = myTabWidget.getChildAt(2);myView.setBackgroundResource(R.drawable.home_menu_search_normal);myView = myTabWidget.getChildAt(3);myView.setBackgroundResource(R.drawable.home_menu_shopping_normal);myView = myTabWidget.getChildAt(4);myView.setBackgroundResource(R.drawable.home_menu_more_normal);break;case 1 :myView = myTabWidget.getChildAt(0);myView.setBackgroundResource(R.drawable.home_menu_home_normal);myView = myTabWidget.getChildAt(1);myView.setBackgroundResource(R.drawable.home_menu_class_selected);myView = myTabWidget.getChildAt(2);myView.setBackgroundResource(R.drawable.home_menu_search_normal);myView = myTabWidget.getChildAt(3);myView.setBackgroundResource(R.drawable.home_menu_shopping_normal);myView = myTabWidget.getChildAt(4);myView.setBackgroundResource(R.drawable.home_menu_more_normal);break;case 2 :myView = myTabWidget.getChildAt(0);myView.setBackgroundResource(R.drawable.home_menu_home_normal);myView = myTabWidget.getChildAt(1);myView.setBackgroundResource(R.drawable.home_menu_class_normal);myView = myTabWidget.getChildAt(2);myView.setBackgroundResource(R.drawable.home_menu_search_selected);myView = myTabWidget.getChildAt(3);myView.setBackgroundResource(R.drawable.home_menu_shopping_normal);myView = myTabWidget.getChildAt(4);myView.setBackgroundResource(R.drawable.home_menu_more_normal);break;case 3 :myView = myTabWidget.getChildAt(0);myView.setBackgroundResource(R.drawable.home_menu_home_normal);myView = myTabWidget.getChildAt(1);myView.setBackgroundResource(R.drawable.home_menu_class_normal);myView = myTabWidget.getChildAt(2);myView.setBackgroundResource(R.drawable.home_menu_search_normal);myView = myTabWidget.getChildAt(3);myView.setBackgroundResource(R.drawable.home_menu_shopping_selected);myView = myTabWidget.getChildAt(4);myView.setBackgroundResource(R.drawable.home_menu_more_normal);break;case 4 :myView = myTabWidget.getChildAt(0);myView.setBackgroundResource(R.drawable.home_menu_home_normal);myView = myTabWidget.getChildAt(1);myView.setBackgroundResource(R.drawable.home_menu_class_normal);myView = myTabWidget.getChildAt(2);myView.setBackgroundResource(R.drawable.home_menu_search_normal);myView = myTabWidget.getChildAt(3);myView.setBackgroundResource(R.drawable.home_menu_shopping_normal);myView = myTabWidget.getChildAt(4);myView.setBackgroundResource(R.drawable.home_menu_more_selected);break;}}}}


大家如果有更好的建議,可以留言,咱一起分享。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.