在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

            EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Android 利用addView 動(dòng)態(tài)給Activity添加View組件

            Android 利用addView 動(dòng)態(tài)給Activity添加View組件

            作者: 時(shí)間:2016-09-12 來源:網(wǎng)絡(luò) 收藏

            本文主要講述如何動(dòng)態(tài)給UI界面添加布局和控件,在編程的時(shí)候很多時(shí)候需要?jiǎng)討B(tài)顯示一些內(nèi)容,在動(dòng)態(tài)添加View的時(shí)候,主要使用方法。

            本文引用地址:http://www.biyoush.com/article/201609/303736.htm

            1. 方法簡介

            中,可以利用排版View的 函數(shù),將動(dòng)態(tài)產(chǎn)生的View 物件加入到排版View 中。

            例子如下:

            Activity代碼:

            public class helloWorld extends Activity {

            public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView( R.layout.main );

            // 取得LinearLayout 物件

            LinearLayout ll = (LinearLayout)findViewById(R.id.viewObj);

            // 將TextView 加入到LinearLayout 中

            TextView tv = new TextView(this);

            tv.setText(Hello World);

            ll. addView ( tv );

            // 將Button 1 加入到LinearLayout 中

            Button b1 = new Button(this);

            b1.setText(取消);

            ll. addView ( b1 );

            // 將Button 2 加入到LinearLayout 中

            Button b2 = new Button(this);

            b2.setText(確定);

            ll. addView ( b2 );

            // 從LinearLayout 中移除Button 1

            ll. removeView ( b1 );

            }

            }

            上述代碼的位置,是垂直順序排列的因?yàn)榻缑娲aLinerlayout的orientation設(shè)置的是vertical的,但是為了美觀,需要設(shè)置添加的View的位置和樣式。在添加View的時(shí)候分為兩類來介紹,一種是布局(例如:Linearlayout等),一種是控件(例如:Button,TextView等等。)

            2. 動(dòng)態(tài)添加布局(包括樣式和位置)

            下面的例子將介紹如何動(dòng)態(tài)添加布局,基本內(nèi)容和上面的代碼一致,主要注重如何控制添加的布局的位置。在控制布局的位置的時(shí)候使用LayoutParam類來實(shí)現(xiàn)。

            例子:

            界面代碼和上面的界面代碼類似,就不在重復(fù)介紹。

            Activity類部分代碼:

            RelativeLayout rl = new RelativeLayout(this);

            //設(shè)置RelativeLayout布局的寬高

            RelativeLayout.LayoutParams relLayoutParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

            this.addView(rl, relLayoutParams);

            3. 動(dòng)態(tài)添加控件

            動(dòng)態(tài)添加控件和添加布局很相似,下述代碼主要注重看控制控件的位置,下面的代碼和第二項(xiàng)添加布局的補(bǔ)充,在新添加的布局里面再添加控件。

            界面代碼同樣不在重復(fù)。

            Activity類部分代碼:

            RelativeLayout rl = new RelativeLayout(this);

            //設(shè)置RelativeLayout布局的寬高

            RelativeLayout.LayoutParams relLayoutParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

            TextView temp = new TextView(this);

            temp .setId(1);

            temp.setText(“圖片”);

            rl.addView(temp);

            TextView tv = new TextView(this);

            tv.setText(“文字”);

            tv.setId(2);

            LayoutParams param1 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

            param1.addRule(RelativeLayout.BELOW, 1);//此控件在id為1的控件的下邊

            rl.addView(tv,param1);

            Button update = new Button(this);

            update.setText(Button);

            LayoutParams param2 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

            param2.addRule(RelativeLayout.RIGHT_OF, 1);//此控件在id為1的控件的右邊

            rl.addView(update,param2);

            this.addView(rl, relLayoutParams);

            注意:控制位置和樣式的時(shí)候,布局和控件使用的方法是一樣的。



            關(guān)鍵詞: Android addView

            評論


            相關(guān)推薦

            技術(shù)專區(qū)

            關(guān)閉