基于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)的類的 API 繼承圖:
android常用布局的代碼實(shí)現(xiàn)所有的布局都會(huì)對(duì)應(yīng)相關(guān)的類,這些類都是繼承自 android.view.ViewGroup 類的。而 LinearLayout,RelativeLayout 都是在 android.widget 包里的。另外,TableLayout 是繼承自 LinearLayout.
下面直接貼代碼了。
// 利用代碼設(shè)置 線性布局 private void setLinearLayout(){ LinearLayout llayout = new LinearLayout(this); llayout.setOrientation(LinearLayout.VERTICAL); // 設(shè)置線性布局的排列方式 TextView textView = new TextView(this); textView.setText("代碼實(shí)現(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開始的第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線性布局和表格布局實(shí)例講解
- android Activity相對(duì)布局的使用方法
- android動(dòng)態(tài)加載布局文件示例
- 分享五種Android常用布局方式
相關(guān)文章
Android自定義View實(shí)現(xiàn)加載進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)加載進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android開發(fā)之項(xiàng)目模塊化實(shí)踐教程
這篇文章主要給大家介紹了關(guān)于Android開發(fā)之項(xiàng)目模塊化的相關(guān)資料,文中通過(guò)示例代碼給各位Android開發(fā)者們介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09Android仿網(wǎng)易客戶端頂部導(dǎo)航欄效果
這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)易客戶端頂部導(dǎo)航欄效果,幫助大家制作網(wǎng)易客戶端導(dǎo)航欄特效,感興趣的小伙伴們可以參考一下2016-06-06Android使用http實(shí)現(xiàn)注冊(cè)登錄功能
這篇文章主要為大家詳細(xì)介紹了Android使用http實(shí)現(xiàn)注冊(cè)登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android自定義ToolBar并實(shí)現(xiàn)沉浸式的方法
這篇文章主要給大家介紹了關(guān)于Android自定義ToolBar并實(shí)現(xiàn)沉浸式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Android 啟動(dòng)activity的4種方式及打開其他應(yīng)用的activity的坑
這篇文章主要介紹了Android 啟動(dòng)activity的4種方式及打開其他應(yīng)用的activity的坑的相關(guān)資料,需要的朋友可以參考下2016-05-05Android中ViewPager1和ViewPager2的使用教程
這篇文章主要介紹了Android中ViewPager1和ViewPager2的使用,效果圖是結(jié)合BottomNavigationView+ViewPager一起使用的,具體實(shí)例代碼跟隨小編一起看看吧2021-10-10