Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 使用 addView 動態添加組件的方法

編輯:關於 Android 程式設計

在項目開發中,我們經常需要在運行時動態添加界面元素。addView 方法就是實現這一需求的利器,它既可以添加布局,也可以添加各類控件。本文就來詳細講解它的用法。

可動態添加的內容主要分兩類:一是布局(如 RelativeLayout 相對布局、LinearLayout 線性布局),二是組件(如文本框、編輯框、按鈕等)。

一、addView 方法簡介

在 Android 中,addView(ViewGroup view, index) 會在指定的 index 位置添加一個 View。借助容器布局的 addView 函數,我們就能把動態生成的 View 對象加入到界面中。

二、示例一:動態添加組件

首先在布局文件中預留一個容器。這裡添加一個文本和兩個按鈕,並用一個 id 為 linearlay_1 的 LinearLayout 作為後續添加組件的容器:

 <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 中通過代碼添加組件。注意,由於布局的 orientation 設置為 vertical,添加的 View 會按垂直順序排列:

 /** * 代碼中,布局的位置,是垂直順序排列的因為界面代碼Linerlayout的orientation設置的是 * vertical的,但是為了美觀,需要設置添加的View的位置和樣式。在添加View的時候分 * 為兩類來介紹,一種是布局(例如:Linearlayout和RelativeLayout等,對於RelativeLayout屬於相對布局) *注意,對於LinearLayout布局來說,設置橫向還是縱向是必須的!否則就看不到效果了。 */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //綁定activity_main布局文件中的布局項,其中R.id.lenearlay_1為布局文件中設置的id LinearLayout linear=(LinearLayout) findViewById(R.id.linearlay_1); //添加文本,this代表當前項目 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

三、示例二:動態添加布局

下面的例子演示如何動態添加布局,重點在於如何控制添加元素的位置。這裡會用到 LayoutParams 類來設置位置與樣式。需要注意的是,無論是布局還是控件,控制位置和樣式的方法都是一致的。

這次直接在 MainActivity 中操作,不涉及 xml 布局文件:

 public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ////////////////////////////////////// //創建一個相對布局relative RelativeLayout 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 中動態添加組件的完整方法,希望對大家的學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved