Android使用addView動態(tài)添加組件的方法
在項目開發(fā)中,我們經(jīng)常需要進行動態(tài)添加組件,其中可添加的部分有兩項:布局和組件
其中,添加的布局主要有RelativeLayout型(相對布局)的和LinearLayout(線性布局)
添加的組件主要有文本顯示框,編輯框,按鈕等組件。
下面,就讓我們來進行實現(xiàn):
首先我們創(chuàng)建一個新的項目,刪除MainActivity.class中沒有的代碼,僅留下protected void onCreate(Bundle savedInstanceState)函數(shù)往布局文件中添加一個新的組件:
1. addView方法簡介
在Android中,addView(ViewGroup view, index)在指定的index處添加一個view??梢岳门虐鎂iew的 addView 函數(shù),將動態(tài)產(chǎn)生的View 物件加入到排版View 中。
2、示例:
(1)首先我們往布局文件中添加一個組件,比如一個文本,兩個按鈕,此時我們需要在布局文件中添加一個布局項<LinearLayout>,定義其id為linearlay_1,用于在添加組件時識別,布局文件代碼如下所示:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="動態(tài)添加組件示例" 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 { @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 動態(tài)添加組件-LinearLayout
(2) 動態(tài)添加布局:
* 下面的例子將介紹如何動態(tài)添加布局,基本內(nèi)容和上面的代碼一致,主要注重如何控制添加的布局的位置
* 在控制布局的位置的時候使用LayoutParam類來實現(xiàn)。
* 注意:控制位置和樣式的時候,布局和控件使用的方法是一樣的。
*/這次只是在MainActivity中進行操作,不涉及布局文件(.xml),其代碼如下所示:
public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ////////////////////////////////////// //創(chuàng)建一個相對布局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 動態(tài)添加布局-RelativeLayout
學會了上面的介紹,你就可以很輕松的布局界面,無論是按鈕還是其他組件,對于布局,你也可以很方便的進行布局使用,以上就是在安卓中如何動態(tài)添加組件的方法。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- android ListView內(nèi)數(shù)據(jù)的動態(tài)添加與刪除實例代碼
- Android動態(tài)添加menu菜單的簡單方法
- 在Android中動態(tài)添加Panel框架的實現(xiàn)代碼
- Android 動態(tài)添加Fragment的實例代碼
- Android用RecyclerView實現(xiàn)動態(tài)添加本地圖片
- Android動態(tài)添加view的方法示例
- Android編程實現(xiàn)長按Button按鈕連續(xù)響應功能示例
- Android實現(xiàn)圓角Button按鈕
- Android開發(fā)中button按鈕的使用及動態(tài)添加組件方法示例
相關文章
Android 優(yōu)雅的實現(xiàn)通用格式化編輯
這篇文章主要介紹了Android 優(yōu)雅的實現(xiàn)通用格式化編輯,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下2021-03-03