Android使用addView動(dòng)態(tài)添加組件的方法
在項(xiàng)目開發(fā)中,我們經(jīng)常需要進(jìn)行動(dòng)態(tài)添加組件,其中可添加的部分有兩項(xiàng):布局和組件
其中,添加的布局主要有RelativeLayout型(相對(duì)布局)的和LinearLayout(線性布局)
添加的組件主要有文本顯示框,編輯框,按鈕等組件。
下面,就讓我們來進(jìn)行實(shí)現(xiàn):
首先我們創(chuàng)建一個(gè)新的項(xiàng)目,刪除MainActivity.class中沒有的代碼,僅留下protected void onCreate(Bundle savedInstanceState)函數(shù)往布局文件中添加一個(gè)新的組件:
1. addView方法簡(jiǎn)介
在Android中,addView(ViewGroup view, index)在指定的index處添加一個(gè)view??梢岳门虐鎂iew的 addView 函數(shù),將動(dòng)態(tài)產(chǎn)生的View 物件加入到排版View 中。
2、示例:
(1)首先我們往布局文件中添加一個(gè)組件,比如一個(gè)文本,兩個(gè)按鈕,此時(shí)我們需要在布局文件中添加一個(gè)布局項(xiàng)<LinearLayout>,定義其id為linearlay_1,用于在添加組件時(shí)識(shí)別,布局文件代碼如下所示:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="動(dòng)態(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>
然后我們?cè)贏ctivity類里邊進(jìn)行添加組件,代碼如下所示:
/** * 代碼中,布局的位置,是垂直順序排列的因?yàn)榻缑娲aLinerlayout的orientation設(shè)置的是 * vertical的,但是為了美觀,需要設(shè)置添加的View的位置和樣式。在添加View的時(shí)候分 * 為兩類來介紹,一種是布局(例如:Linearlayout和RelativeLayout等,對(duì)于RelativeLayout屬于相對(duì)布局) *注意,對(duì)于LinearLayout布局來說,設(shè)置橫向還是縱向是必須的!否則就看不到效果了。 */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //綁定activity_main布局文件中的布局項(xiàng),其中R.id.lenearlay_1為布局文件中設(shè)置的id LinearLayout linear=(LinearLayout) findViewById(R.id.linearlay_1); //添加文本,this代表當(dāng)前項(xiàng)目 TextView tv=new TextView(this); tv.setText("示例文本框"); tv.setId(1);//設(shè)置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 動(dòng)態(tài)添加組件-LinearLayout
(2) 動(dòng)態(tài)添加布局:
* 下面的例子將介紹如何動(dòng)態(tài)添加布局,基本內(nèi)容和上面的代碼一致,主要注重如何控制添加的布局的位置
* 在控制布局的位置的時(shí)候使用LayoutParam類來實(shí)現(xiàn)。
* 注意:控制位置和樣式的時(shí)候,布局和控件使用的方法是一樣的。
*/這次只是在MainActivity中進(jìn)行操作,不涉及布局文件(.xml),其代碼如下所示:
public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ////////////////////////////////////// //創(chuàng)建一個(gè)相對(duì)布局relative RelativeLayout relative = new RelativeLayout(this); relative.setBackgroundColor(Color.YELLOW); // 將Button1 加入到RelativeLayout 中 Button btn_r1 = new Button(this); btn_r1.setText("取消");//設(shè)置顯示的字符 btn_r1.setId(24); relative.addView(btn_r1); // 將Button2 加入到RelativeLayout 中 Button btn_r2 = new Button(this); btn_r2.setText("確定");//設(shè)置顯示的字符 btn_r2.setId(25); relative.addView(btn_r2); // 設(shè)置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); ////設(shè)置按鈕的布局屬性 lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.RIGHT_OF, btn_r1.getId()); btn_r2.setLayoutParams(lp); ////設(shè)置按鈕的布局屬性 setContentView(relative); } }
效果如下所示:
圖 2 動(dòng)態(tài)添加布局-RelativeLayout
學(xué)會(huì)了上面的介紹,你就可以很輕松的布局界面,無論是按鈕還是其他組件,對(duì)于布局,你也可以很方便的進(jìn)行布局使用,以上就是在安卓中如何動(dòng)態(tài)添加組件的方法。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android ListView內(nèi)數(shù)據(jù)的動(dòng)態(tài)添加與刪除實(shí)例代碼
- Android動(dòng)態(tài)添加menu菜單的簡(jiǎn)單方法
- 在Android中動(dòng)態(tài)添加Panel框架的實(shí)現(xiàn)代碼
- Android 動(dòng)態(tài)添加Fragment的實(shí)例代碼
- Android用RecyclerView實(shí)現(xiàn)動(dòng)態(tài)添加本地圖片
- Android動(dòng)態(tài)添加view的方法示例
- Android編程實(shí)現(xiàn)長(zhǎng)按Button按鈕連續(xù)響應(yīng)功能示例
- Android實(shí)現(xiàn)圓角Button按鈕
- Android開發(fā)中button按鈕的使用及動(dòng)態(tài)添加組件方法示例
相關(guān)文章
Android 優(yōu)雅的實(shí)現(xiàn)通用格式化編輯
這篇文章主要介紹了Android 優(yōu)雅的實(shí)現(xiàn)通用格式化編輯,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03