android控制項的動態增加

來源:互聯網
上載者:User

通常android裡的介面布局都是在XML裡設定好的 也就是說 在程式中,不能更改介面上的元素數量等,

但是也可以在程式中動態增加控制項。

 import android.app.Activity;   

 import android.content.Context;   

 import android.graphics.Color;   

 import android.os.Bundle;   

 import android.text.Layout;   

 import android.text.format.DateFormat;   

 import android.util.Log;   

 import android.view.KeyEvent;   

 import android.view.ViewGroup.LayoutParams;   

 import android.widget.*;   

   

 import java.util.Calendar;   

   

 /**  

  * 測試動態使用android控制項  

  * @author gaolei by 20090827  

  */  

 public class fetion2009 extends Activity   

 {   

     /** Called when the activity is first created. */  

     ProgressBar pb;                 //進度條控制項,但拿出來是為了可控,動態改變其進度   

     //聊天對話的底色是間隔的   

     private static final int[] bg = { Color.WHITE, Color.GRAY };   

     private static int bgIndex=0;   //聊天對話的底色 當前色應該是bg中的索引值   

        

     //以下 布局參數 標識當前控制項的寬高情況FILL_PARENT=佔據全部父控制項,WRAP_CONTENT=僅包裹控制項中的內容//還有其他作用比如左右邊距,這裡我們使用預設的   

     private LinearLayout.LayoutParams LP_FF = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);   

     private LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);   

     private LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);   

        

     @Override  

     public void onCreate( Bundle savedInstanceState )   

     {   

         super.onCreate( savedInstanceState );   

            

         //聊天對白視窗需要滾動   

         ScrollView sv   = new ScrollView(this);   

         sv.setLayoutParams( LP_FF );   

            

         LinearLayout layout = new LinearLayout(this);   //線性布局方式   

         layout.setOrientation( LinearLayout.VERTICAL ); //控制項對其方式為垂直排列   

         layout.setBackgroundColor( 0xff00ffff );        //設定布局板的一個特殊顏色,這可以檢驗我們會話時候是否有地方顏色不正確!   

   

         //豐富聊天頁面,也順帶測試頁面滾動效果,增加了10個重複的對話內容   

         for( int i=0; i<10; i++ )   

         {   

             setSendMsg( layout, this, getCurrColor(), i+"聊天內容在這裡。。" );   

         }   

            

         //傳送檔案效果1,圓環進度條,也是ProgressBar預設的效果   

         setSendFile( layout, this, getCurrColor(),"我的照片.jpg");   

            

         //傳送檔案效果 2,矩行進度條,也是ProgressBar的風格設定成 style="?android:attr/progressBarStyleHorizontal"的效果   

         setSendFile2( layout, this, getCurrColor(),"我的照片.jpg");   

            

         for( int i=0; i<10; i++ )   

         {   

             setSendMsg( layout, this, getCurrColor(), i+"聊天內容在這裡。。" );   

         }   

         sv.addView( layout );   //把線性布局加入到ScrollView中   

         setContentView(sv);     //設定當前的頁面為ScrollView   

     }   

        

     /**  

      * 擷取當前聊天對白的底色值  

      * @return 當前聊天對白的底色值  

      */  

     private int getCurrColor()   

     {   

         return bg[ (++bgIndex)% bg.length ];   

     }   

        

     /**  

      * 動態增加一個聊天內容  

      * 這裡為了簡化編程把 某人說 和 內容放到一個TextView中,可以根據設計文檔拆成2個TextView分別顯示,設定字型等  

      * @param layout    TextView 控制項欲添加到的目標layout  

      * @param context   構建View控制項的必須參數 既View控制項的環境  

      * @param bgColur   TextView 控制項的背景色  

      * @param MSG       TextView 控制項要現實的常值內容  

      */  

     private void setSendMsg(LinearLayout layout, Context context, int bgColur, String MSG)   

     {   

         TextView tv = new TextView(context);    //普通聊天對話   

         //擷取一個全域的日曆執行個體,用於擷取當前系統時間並格式化成小時:分鐘形式,僅用於測試,這裡的時間應該是由其他程式提供   

         tv.setText( "某人  說: ["+DateFormat.format( "kk:mm" , Calendar.getInstance())+"]\n"+MSG );   

         tv.setBackgroundColor( bgColur );   

         layout.addView( tv );   

     }   

        

     /**  

      * 動態增加一個傳送檔案的會話條目  

      * 這裡因為是發送進度條與取消按鈕的水平對其方式,所以需要增加一個LinearLayout  

      * @param layout    欲添加到的目標layout  

      * @param context   構建 View控制項的必須參數 既View控制項的環境  

      * @param bgColur   控制項的背景色  

      * @param MSG       控制項要現實的常值內容  

      */  

     private void setSendFile(LinearLayout layout, Context context, int bgColur, String fileName)   

     {   

         //把 某人說 [時間]   

         //要發送的檔案資訊 全都交給 setSendMsg 繪製吧!   

         setSendMsg( layout, context, bgColur, "正在發送"+fileName );   

         //水平排列2個控制項需要一個LinearLayout,相片順序預設的就是水平排列   

         LinearLayout myLayout = new LinearLayout(context);   

         //這個 LinearLayout控制項的背景色需要設定,要不就會顯示出主LinearLayout的顏色了,即0xff00ffff   

         myLayout.setBackgroundColor( bgColur );   

   

         //動態建立一個 ProgressBar,以預設屬性加入到myLayout中   

         ProgressBar pb = new ProgressBar(context);   

         pb.setLayoutParams( LP_WW );   

         myLayout.addView( pb );   

   

         //動態建立一個 Button,以預設屬性加入到myLayout中   

         Button bt = new Button(context);   

         bt.setLayoutParams( LP_WW );   

         bt.setText( " 取消" );   

         myLayout.addView( bt );   

         //將水平布局的 LinearLayout及其內如所有控制項添加到主layout中   

         layout.addView( myLayout );   

     }   

        

     /**  

      * 動態增加一個傳送檔案的會話條目  

      * 但為了保障ProgressBar和 Button的底色符合設計要求,增加了一個LinearLayout,並設定其背景色  

      * @param layout    欲添加到的目標layout  

      * @param context   構建 View控制項的必須參數 既View控制項的環境  

      * @param bgColur   控制項的背景色  

      * @param MSG       控制項要現實的常值內容  

      */  

     private void setSendFile2(LinearLayout layout, Context context, int bgColur, String fileName)   

     {   

         setSendMsg( layout, context, bgColur, "正在發送"+fileName );   

   

         LinearLayout myLayout = new LinearLayout(context);    

         myLayout.setBackgroundColor( bgColur );   

         myLayout.setOrientation( LinearLayout.VERTICAL );//控制項對其方式為垂直,預設為水平   

            

         //ProgressBar 的預設風格是圓環型,這裡需要設定她的風格為Horizontal(水平線)   

         pb = new ProgressBar(context,null,android.R.attr.progressBarStyleHorizontal);   

         pb.setLayoutParams( LP_FW );   

         pb.setProgress( 45 );           // 設定第1進度為45   

         pb.setSecondaryProgress( 0 );   //這裡我們不需要第2進度,所以為0   

         myLayout.addView( pb );   

            

         Button bt = new Button(context);   

         bt.setLayoutParams( LP_WW );   

         bt.setText( "取消" );   

         myLayout.addView( bt );   

            

         layout.addView( myLayout );   

     }   

        

     @Override  

     public boolean onKeyDown(int keyCode, KeyEvent event)   

     {   

         Log.d("onKeyDown:", " keyCode=" + keyCode + " KeyEvent=" + event);   

         switch (keyCode)   

         {   

             case KeyEvent.KEYCODE_DPAD_UP:   

   

             break;   

             case KeyEvent.KEYCODE_DPAD_DOWN:   

   

             break;   

             case KeyEvent.KEYCODE_DPAD_LEFT:   

                 //右左按鍵可以控制第一進度的增減   

                 pb.setProgress( pb.getProgress()-5 );   

             break;   

             case KeyEvent.KEYCODE_DPAD_RIGHT:   

                 pb.setProgress( pb.getProgress()+5 );   

             break;   

             case KeyEvent.KEYCODE_DPAD_CENTER:   

   

             break;   

             case KeyEvent.KEYCODE_0:   

             break;   

         }   

         return super.onKeyDown(keyCode, event);   

     }   

 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.