天天看點

android控件動态使用

<a href="http://blog.51cto.com/attachment/201105/214440309.jpg" target="_blank"></a>

view plaincopy to clipboardprint?  

package com.fetion.android;     

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&lt;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");     

        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 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,并設定其背景色    

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

        LinearLayout myLayout = new LinearLayout(context);      

        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     

    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:     

            case KeyEvent.KEYCODE_DPAD_LEFT:     

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

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

            case KeyEvent.KEYCODE_DPAD_RIGHT:     

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

            case KeyEvent.KEYCODE_DPAD_CENTER:     

            case KeyEvent.KEYCODE_0:     

        return super.onKeyDown(keyCode, event);     

}   

通常android裡的界面布局都是在XML裡設定好的

也就是說

在程式中,不能更改界面上的元素數量等,

比如上圖所示的一個 聊天會話界面

當有人發言就要增加一個TextView,

這就是動态增加控件,

這就不能在XML裡事先布局了!

不過還好,ANDROID使用控件也不是隻有XML這一種方式

以下代碼就是動态生産控件的JAVA程式

實作效果如上圖

代碼僅為預研使用

本文轉自 kome2000 51CTO部落格,原文連結:http://blog.51cto.com/kome2000/578692

繼續閱讀