Android動(dòng)態(tài)布局小結(jié)
android動(dòng)態(tài)布局相比靜態(tài)布局,動(dòng)態(tài)布局不用再將xml轉(zhuǎn)變了布局代碼,提高了一定的效率,當(dāng)然可以忽略不記。動(dòng)態(tài)布局主要是比較靈活,可以很快的在代碼中直接修改布局,并直接使用控件進(jìn)行業(yè)務(wù)邏輯開發(fā)。但代碼量通常比較大,維護(hù)沒有靜態(tài)布局方便。不過,作為一個(gè)android開發(fā)人員,掌握一定的動(dòng)態(tài)布局技巧,有時(shí)在工作中也是可以提高一定的代碼開發(fā)效率。
在動(dòng)態(tài)布局中,要想實(shí)現(xiàn)一個(gè)布局,一般是先創(chuàng)建五大布局的對(duì)象。然后對(duì)這些對(duì)象進(jìn)行屬性設(shè)置,之后再向里面添加子布局或控件。
以RelativeLayout為例。
RelativeLayout mLayout = new RelativeLayout(); //設(shè)置RelativeLayout的子控件屬性對(duì)象,并設(shè)置其尺寸樣式。每個(gè)GroupView中都有一個(gè)LayoutPrams,都是用來給子控件設(shè)置發(fā)生的。 RelativeLayout.LayoutPrams params = new RelativeLayout.LayoutPrams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); //增加 子控件 ImageView iv = new ImageView(getActivity()); iv.setImageResource(R.drawable.tab_icon_conversation_normal); //設(shè)置子控件在RealtiveLayout中的位置屬性。 params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); //給iv 增加屬性 //將iv,增加到mLayout中 mLayout .addView(iv, params);
從最后一句,可以看出來,params對(duì)象引用設(shè)置的屬性都是作用有ImageView這個(gè)子控件上的,然后把iv與params一塊加入到RealtiveLayout中去。
整理android動(dòng)態(tài)布局方法總結(jié)
//絕對(duì)布局
AbsoluteLayout abslayout=new AbsoluteLayout (this); setContentView(abslayout); Button btn1 = new Button(this); btn1.setText(”this is a button”); btn1.setId(1); AbsoluteLayout.LayoutParams lp1 = new AbsoluteLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0,100); abslayout.addView(btn1, lp1);
//相對(duì)布局
RelativeLayout relativeLayout = new RelativeLayout(this); setContentView(relativeLayout); AbsoluteLayout abslayout=new AbsoluteLayout (this); RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP); lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); relativeLayout.addView(abslayout ,lp1);
//線性布局
LinearLayout ll = new LinearLayout(this); EditText et = new EditText(); ll.addView(et); //動(dòng)態(tài)添加布局的方法1. LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null); setContentView(ll); LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,ll); //這樣 main2 作為 main1的子布局 加到了 main1的 根節(jié)點(diǎn)下 //動(dòng)態(tài)添加布局的方法2 addView. LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null); setContentView(ll); LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,null); ll.addView(ll2);
- android動(dòng)態(tài)布局之動(dòng)態(tài)加入TextView和ListView的方法
- 基于Android在布局中動(dòng)態(tài)添加view的兩種方法(總結(jié))
- Android 動(dòng)態(tài)改變布局實(shí)例詳解
- Android編程動(dòng)態(tài)加載布局實(shí)例詳解【附demo源碼】
- android用java動(dòng)態(tài)增添刪除修改布局
- Android動(dòng)態(tài)加載布局
- Android動(dòng)態(tài)添加設(shè)置布局與控件的方法
- android動(dòng)態(tài)加載布局文件示例
- Android動(dòng)態(tài)布局使用詳解
相關(guān)文章
Android中ListView的item點(diǎn)擊沒有反應(yīng)的解決方法
這篇文章主要介紹了Android中ListView的item點(diǎn)擊沒有反應(yīng)的相關(guān)資料,需要的朋友可以參考下2017-10-10
android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸(2)
這篇文章主要為大家詳細(xì)介紹了android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Android自定義View軟鍵盤實(shí)現(xiàn)搜索
本文給大家分享android自定義view軟鍵盤實(shí)現(xiàn)搜索,對(duì)android軟鍵盤相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12
Windows下Flutter+Idea環(huán)境搭建及配置
這篇文章介紹了Windows下Flutter+Idea環(huán)境搭建及配置的方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
基于Android實(shí)現(xiàn)答題倒計(jì)時(shí)功能
這篇文章主要介紹了基于Android實(shí)現(xiàn)答題倒計(jì)時(shí)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
android開發(fā)之蜂鳴提示音和震動(dòng)提示的實(shí)現(xiàn)原理與參考代碼
蜂鳴提示音和震動(dòng)提示此功能在手機(jī)使用中很實(shí)用,最近在讀zxing項(xiàng)目,學(xué)到了不少東西;我們一起來看看他是怎么做的,感興趣的朋友可以了解下哦2013-01-01
Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽
這篇文章主要為大家詳細(xì)介紹了Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android adb.exe程序啟動(dòng)不起來 具體解決方法
這篇文章主要介紹了Android adb.exe程序啟動(dòng)不起來 具體解決方法,有需要的朋友可以參考一下2013-12-12

