Android自訂動態布局 — 多圖片上傳,

來源:互聯網
上載者:User

Android自訂動態布局 — 多圖片上傳,

Android自訂動態布局 — 多圖片上傳


本文介紹Android中動態布局添加圖片,多圖片上傳。


項目中:

  


技術點:

1.動態添加格局中的線條和添加圖片的+號

2.多張圖片非同步上傳


首先來看一下布局檔案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#f2f2f2" >    <LinearLayout        android:id="@+id/layout_CONTENT"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#f2f2f2"        android:orientation="vertical"        android:padding="5dp" >        <!-- 布局由程式動態產生 -->        <LinearLayout            android:id="@+id/layout_container"            android:layout_width="300dp"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"            android:layout_margin="5dp"            android:background="#cbcbcb"            android:orientation="vertical"            android:padding="0.2px" />        <TextView            android:id="@+id/text_no_data"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="20dp"            android:text="@string/text_picture_upload"            android:textSize="16dp" />    </LinearLayout></LinearLayout>

布局很簡單,主要是id為layout_container的一個LinearLayout作為父布局。



橫向的線條和縱向的線條布局也很簡單:

<View xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="1px"    android:background="#cbcbcb" />

<View xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="1px"    android:layout_height="match_parent"    android:background="#cbcbcb" />



下面是動態產生布局的實現方式:

private void initUI() {        setContentView(R.layout.activity_main);        //setTitle(R.string.button_service_upload_picture);        //showBackwardView(R.string.button_backward, true);        //showForwardView(R.string.button_upload,true);        //最頂層父布局        mLayout = (ViewGroup) findViewById(R.id.layout_container);        final int count = 9;    //9格        final int rowCount = (count + 2) / 3;        for (int i = 0; i < rowCount; i++) {            if (i != 0) {                //載入橫向布局線條                View.inflate(this, R.layout.layout_line_horizonal, mLayout);            }            //建立布局對象,設定按下顏色            final LinearLayout linearLayout = new LinearLayout(this);            linearLayout.setBackgroundResource(R.drawable.row_selector);            for (int j = 0; j < 3; j++) {                if (j != 0) {                    //載入內層縱向布局線條                    View.inflate(this, R.layout.layout_line_vertical, linearLayout);                }                ImageButton imageButton = new ImageButton(this);                imageButton.setBackgroundResource(R.drawable.row_selector);                imageButton.setTag(TAG);                imageButton.setOnClickListener(this);                imageButton.setEnabled(false);                LinearLayout.LayoutParams layoutParams =                        new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);                //添加到linearLayout布局中                linearLayout.addView(imageButton, layoutParams);                //將imageButton對象添加到列表                mImageButtonList.add(imageButton);            }            DisplayManager manager = DisplayManager.getInstance();            LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, manager.dipToPixel(100));            //將View添加到總父布局            mLayout.addView(linearLayout, layoutParams);        }        //外層設定ImageButton屬性        final ImageButton currentImageButton = mImageButtonList.get(mCurrent);        currentImageButton.setImageResource(R.drawable.ic_add_picture);        currentImageButton.setScaleType(ScaleType.CENTER);        currentImageButton.setEnabled(true);    }


圖片上傳功能:

 private class UploadPictureTask extends AsyncTask<List<String>, Integer, String> {        /* (non-Javadoc)         * @see android.os.AsyncTask#doInBackground(Params[])         */        @Override        protected String doInBackground(List<String>... params) {            final List<String> pictureList = params[0];            for (int i = 0, len = pictureList.size(); i < len; i++) {                final File file = new File(pictureList.get(i));                //final String response = ApacheHttpUtils.post(mUrlPrefix + "/upload", new File[] {file});                // 解析,儲存                //final UploadInfo upload = new UploadParser().parse(response).getData();                /*if (upload != null) {                    final String url = upload.getUrl();                    if (url != null) {                        mPictureUrlList.add(url);                    }                }*/                publishProgress(i);            }            return null;        }        /* (non-Javadoc)         * @see android.os.AsyncTask#onProgressUpdate(Progress[])         */        @Override        protected void onProgressUpdate(Integer... values) {        }        /* (non-Javadoc)         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)         */        @Override        protected void onPostExecute(String result) {            //addPictures();            super.onPostExecute(result);        }    }

註:類中聲明了三個列表去儲存之前所操作的記錄

mImageButtonList = new ArrayList<ImageButton>();        mPicturePathList = new ArrayList<String>();        mPictureUrlList = new ArrayList<String>();



關於細節大家感興趣的下載源碼學習吧。


歡迎下載源碼:http://download.csdn.net/download/gao_chun/8776533


轉載請註明.

聯繫我們

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