基于Android代碼實(shí)現(xiàn)常用布局
關(guān)于 android 常用布局,利用 XML 文件實(shí)現(xiàn)已經(jīng)有很多的實(shí)例了。但如何利用代碼實(shí)現(xiàn)呢?當(dāng)然利用代碼實(shí)現(xiàn)沒(méi)有太大的必要,也是不提倡的,但我覺(jué)得利用代碼實(shí)現(xiàn)這些布局,可以更好的了解 SDK API ,所以在此也整理一些,和大家分享一下。
首先看一下,布局所對(duì)應(yīng)的類(lèi)的 API 繼承圖:

android常用布局的代碼實(shí)現(xiàn)所有的布局都會(huì)對(duì)應(yīng)相關(guān)的類(lèi),這些類(lèi)都是繼承自 android.view.ViewGroup 類(lèi)的。而 LinearLayout,RelativeLayout 都是在 android.widget 包里的。另外,TableLayout 是繼承自 LinearLayout.
下面直接貼代碼了。
// 利用代碼設(shè)置 線(xiàn)性布局
private void setLinearLayout(){
LinearLayout llayout = new LinearLayout(this);
llayout.setOrientation(LinearLayout.VERTICAL); // 設(shè)置線(xiàn)性布局的排列方式
TextView textView = new TextView(this);
textView.setText("代碼實(shí)現(xiàn)的線(xiàn)性布局");
textView.setTextColor(Color.RED);
textView.setGravity(Gravity.CENTER); // 設(shè)置文本內(nèi)容的對(duì)齊方式
LinearLayout.LayoutParams ll_lpara = new LinearLayout.LayoutParams(MP,WC);
// ll_lpara.gravity = Gravity.CENTER_HORIZONTAL; // 設(shè)置控件在布局中的對(duì)齊方式
llayout.addView(textView,ll_lpara);
Button btn = new Button(this);
btn.setText("按鈕");
llayout.addView(btn,ll_lpara); // 按指定屬性添加控件
setContentView(llayout);
}
實(shí)現(xiàn)效果圖:

=========================================================================
// 利用代碼設(shè)置 相對(duì)布局
private void setRelativeLayout(){
RelativeLayout rlayout = new RelativeLayout(this);
rlayout.setPadding(10, 10, 10, 10); // 單位: pixels
int textViewID = 100;
TextView textView = new TextView(this);
textView.setId(textViewID);
textView.setText("請(qǐng)輸入:");
RelativeLayout.LayoutParams rl_lpara1 = new RelativeLayout.LayoutParams(MP, WC);
rlayout.addView(textView, rl_lpara1);
int editTextID = 200;
EditText editText = new EditText(this);
editText.setId(editTextID);
editText.setBackgroundResource(android.R.drawable.editbox_background); // 設(shè)置背景 , 同android:backgroumd
RelativeLayout.LayoutParams rl_lpara2 = new RelativeLayout.LayoutParams(MP, WC);
rl_lpara2.addRule(RelativeLayout.BELOW,textViewID); // 設(shè)置相對(duì)屬性,需先指定相對(duì)控件的ID
rlayout.addView(editText, rl_lpara2);
int backBtnID = 300;
Button backBtn = new Button(this);
backBtn.setId(backBtnID);
backBtn.setText("返回");
RelativeLayout.LayoutParams rl_lpara3 = new RelativeLayout.LayoutParams(WC, WC);
rl_lpara3.addRule(RelativeLayout.BELOW, editTextID);
rl_lpara3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // 設(shè)置與父控件的相對(duì)屬性
rlayout.addView(backBtn, rl_lpara3);
Button okBtn = new Button(this);
okBtn.setText("確定");
RelativeLayout.LayoutParams rl_lpara4 = new RelativeLayout.LayoutParams(WC, WC);
rl_lpara4.addRule(RelativeLayout.LEFT_OF, backBtnID);
rl_lpara4.addRule(RelativeLayout.ALIGN_TOP,backBtnID);
rlayout.addView(okBtn, rl_lpara4);
setContentView(rlayout);
}
實(shí)現(xiàn)效果圖:

=========================================================================
// 利用代碼設(shè)置 表格布局
private void setTableLayout(){
TableLayout tlayout = new TableLayout(this);
tlayout.setColumnStretchable(2, true); // 拉長(zhǎng)索引從0開(kāi)始的第2列
TableLayout.LayoutParams tl_lpara = new TableLayout.LayoutParams(MP,WC);
// 1. TableRow 不需要設(shè)置 layout_width, layout_height
// 2. TableRow 中的控件不能設(shè)置 layout_span 屬性
TableRow tr1 = new TableRow(this);
TextView textView0 = new TextView(this);
textView0.setText("第0列");
tr1.addView(textView0);
TextView textView1 = new TextView(this);
textView1.setText("第1列");
tr1.addView(textView1);
TextView textView2 = new TextView(this);
textView2.setText("第2列");
textView2.setBackgroundColor(Color.CYAN);
tr1.addView(textView2);
tlayout.addView(tr1, tl_lpara);
TableRow tr2 = new TableRow(this);
Button btn0 = new Button(this);
btn0.setText("按鈕0");
tr2.addView(btn0);
Button btn1 = new Button(this);
btn1.setText("按鈕1");
tr2.addView(btn1);
Button btn2 = new Button(this);
btn2.setText("按鈕2");
tr2.addView(btn2);
Button btn3 = new Button(this);
btn3.setText("按鈕3");
tr2.addView(btn3);
tlayout.addView(tr2, tl_lpara);
setContentView(tlayout);
}
實(shí)現(xiàn)效果圖:

- android LinearLayout和RelativeLayout組合實(shí)現(xiàn)精確布局方法介紹
- Android RelativeLayout相對(duì)布局屬性簡(jiǎn)析
- android layout 按比例布局的代碼
- Android布局——Preference自定義layout的方法
- android自定義RadioGroup可以添加多種布局的實(shí)現(xiàn)方法
- android Activity線(xiàn)性布局和表格布局實(shí)例講解
- android Activity相對(duì)布局的使用方法
- android動(dòng)態(tài)加載布局文件示例
- 分享五種Android常用布局方式
相關(guān)文章
Android拼圖游戲 玩轉(zhuǎn)從基礎(chǔ)到應(yīng)用手勢(shì)變化
這篇文章主要介紹了Android拼圖游戲的實(shí)現(xiàn)方法,教大家玩轉(zhuǎn)從基礎(chǔ)到應(yīng)用手勢(shì)變化,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android 勇闖高階性能優(yōu)化之啟動(dòng)優(yōu)化篇
在移動(dòng)端程序中,用戶(hù)希望的是應(yīng)用能夠快速打開(kāi)。啟動(dòng)時(shí)間過(guò)長(zhǎng)的應(yīng)用不能滿(mǎn)足這個(gè)期望,并且可能會(huì)令用戶(hù)失望。輕則鄙視你,重則直接卸載你的應(yīng)用2021-10-10
Android動(dòng)態(tài)繪制餅狀圖的示例代碼
這篇文章主要介紹了Android動(dòng)態(tài)繪制餅狀圖的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Android仿微信Viewpager-Fragment惰性加載(lazy-loading)
這篇文章主要為大家詳細(xì)介紹了Android仿微信Viewpager-Fragment惰性加載lazy-loading,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android四大組件之Service(服務(wù))實(shí)例詳解
這篇文章主要介紹了Android四大組件之Service(服務(wù))的用法,結(jié)合實(shí)例形式詳細(xì)分析了Service的基本概念,類(lèi)型,用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01
Android Studio / IDEA kotlin 顯示 var 真實(shí)類(lèi)型操作
這篇文章主要介紹了Android Studio / IDEA kotlin 顯示 var 真實(shí)類(lèi)型操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Android 應(yīng)用的全屏和非全屏實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 應(yīng)用的全屏和非全屏實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
android開(kāi)發(fā)去除標(biāo)題欄的方法
這篇文章主要介紹了android開(kāi)發(fā)去除標(biāo)題欄的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04

