Android使用addView動態添加組件

來源:互聯網
上載者:User

標籤:

          在項目開發中,我們經常需要進行動態添加組件,其中可添加的部分有兩項:布局和組件

 其中,添加的布局主要有RelativeLayout型(相對布局)的和LinearLayout(線性布局)

           添加的組件主要有文本顯示框,編輯框,按鈕等組件。


下面,就讓我們來進行實現:

    首先我們建立一個新的項目,刪除MainActivity.class中沒有的代碼,僅留下protected void onCreate(Bundle savedInstanceState)函數

往布局檔案中添加一個新的組件:

          1. addView方法簡介

在Android中,addView(ViewGroup view, index)在指定的index處添加一個view。可以利用排版View的 addView 函數,將動態產生的View 物件加入到排版View 中。

          2、樣本:

             (1)首先我們往布局檔案中添加一個組件,比如一個文本,兩個按鈕,此時我們需要在布局檔案中添加一個布局項<LinearLayout>,定義其id為linearlay_1,用於在添加組件時識別,布局檔案代碼如下所示:

    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="動態添加組件樣本"         android:id="@+id/textview"/>    <LinearLayout         android:layout_below="@+id/textview"        android:id="@+id/linearlay_1"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:orientation="vertical"        ></LinearLayout>
   然後我們在Activity類裡邊進行添加組件,代碼如下所示:

          /**
      * 代碼中,布局的位置,是垂直順序排列的因為介面代碼Linerlayout的orientation設定的是
      * vertical的,但是為了美觀,需要設定添加的View的位置和樣式。在添加View的時候分
      * 為兩類來介紹,一種是布局(例如:Linearlayout和RelativeLayout等,對於RelativeLayout屬於相對布局)

      *注意,對於LinearLayout布局來說,設定橫向還是縱向是必須的!否則就看不到效果了。 

      */

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//綁定activity_main布局檔案中的布局項,其中R.id.lenearlay_1為布局檔案中設定的idLinearLayout linear=(LinearLayout) findViewById(R.id.linearlay_1);//添加文本,<span style="font-family: Arial, Helvetica, sans-serif;">this代表當前項目</span>TextView tv=new TextView(this);tv.setText("樣本文字框");tv.setId(1);//設定ID,可有可無,也可以在R檔案中添加字串,然後在這裡使用引用的方式使用linear.addView(tv);     // 將Button 加入到LinearLayout 中      Button b1 = new Button(this);      b1.setText("取消");      linear. addView ( b1 );       // 將Button 2 加入到LinearLayout 中      Button b2 = new Button(this);      b2.setText("確定");      linear. addView ( b2 );       // 從LinearLayout 中移除Button 1     // linear. removeView ( b1 );       }}

效果如所示:


圖 1 動態添加組件-LinearLayout


  


       (2) 動態添加布局:

* 下面的例子將介紹如何動態添加布局,基本內容和上面的代碼一致,主要注重如何控制添加的布局的位置
* 在控制布局的位置的時候使用LayoutParam類來實現。
 * 注意:控制位置和樣式的時候,布局和控制項使用的方法是一樣的。
*/這次只是在MainActivity中進行操作,不涉及布局檔案(.xml),其代碼如下所示:

public class MainActivity extends Activity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);      //////////////////////////////////////      //建立一個相對布局relativeRelativeLayout relative = new RelativeLayout(this);relative.setBackgroundColor(Color.YELLOW);// 將Button1 加入到RelativeLayout 中      Button btn_r1 = new Button(this);      btn_r1.setText("取消");//設定顯示的字元      btn_r1.setId(24);      relative.addView(btn_r1);            // 將Button2 加入到RelativeLayout 中      Button btn_r2 = new Button(this);      btn_r2.setText("確定");//設定顯示的字元          btn_r2.setId(25);      relative.addView(btn_r2);                                                                                                                                                                   // 設定RelativeLayout布局的寬高         RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);         lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);         btn_r1.setLayoutParams(lp);   ////設定按鈕的布局屬性         lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);        lp.addRule(RelativeLayout.RIGHT_OF, btn_r1.getId());         btn_r2.setLayoutParams(lp);   ////設定按鈕的布局屬性         setContentView(relative);}}

效果如下所示:


圖 2 動態添加布局-RelativeLayout

學會了上面的介紹,你就可以很輕鬆的布局介面,無論是按鈕還是其他組件,對於布局,你也可以很方便的進行布局使用,以上就是在安卓中如何動態添加組件的方法

       

Android使用addView動態添加組件

聯繫我們

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