Android使用代碼動態(tài)生成界面
我們最常用使用XML來編寫Android應(yīng)用程序的UI,這樣的好處是方便快捷可視化,而且維護(hù)和修改特別容易,但是它是靜態(tài)的。如果我們要做的程序的界面是固定的,用XML固然是最好的選擇,但是如果我們需要動態(tài)、靈活地控制UI,使用代碼來動態(tài)生成UI無疑使最好的辦法。
在XML中,我們使用的五大布局:LinearLayout(線性布局)、RelativeLayout(相對布局)、TableLayout(表格布局)、AbsoluteLayout(絕對布局)和FrameLayout(幀布局)在Android中也有對應(yīng)的類來表示。
舉個例子,我現(xiàn)在需要顯示一個表格,表格的行數(shù)和列數(shù)及其內(nèi)容都不確定,如果在XML中,這是不可能實現(xiàn)的。
先給大家看一下成品:(下面的代碼只給大家展示如何實現(xiàn),表格里面的內(nèi)容忽略)
首先,新建一個不帶任何控件的XML文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TableLayout android:id="@+id/tableLayout" android:layout_width="match_parent" android:layout_height="wrap_content" > </TableLayout> </LinearLayout>
在代碼中新建一個TableLayout:
// TODO 顯示表格信息 private void displayRegeditedInfo() { Iterator iterator = iterable.iterator(); ICells iCells = GlobalVariable.manager .createPersonDataCells(IInspectionManager.CS_PERSON_LIST_CELLS); boolean flag = true;// 標(biāo)題欄為true,內(nèi)容欄位false int colorChange = 1;// 用來判斷單雙行,以顯示不同的顏色 TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout); tableLayout.setStretchAllColumns(true); tableLayout.setShrinkAllColumns(true); while (iterator.hasNext()) { // 行的樣式 TableRow.LayoutParams params = new TableRow.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); if (flag)// 首先顯示表格的標(biāo)題欄,內(nèi)容自己定義 { TableRow titleRow = new TableRow(this); for (int i = 0; i < colums; i++)// 列數(shù) {// 列名 params.setMargins(1, 1, 1, 1); TextView textView = new TextView(this); textView .setBackgroundColor(getResources().getColor(R.color.top)); textView.setTextColor(Color.WHITE); textView.setTextSize(31); textView.setLayoutParams(params); textView.setText(columsName);// 列名 textView.setTextSize(30); textView.setGravity(Gravity.CENTER_HORIZONTAL); titleRow.addView(textView);// 把控件添加到行TableRow中 } flag = false; tableLayout.addView(titleRow);// 把行添加到TableLayout中 } // 新建一行,顯示每個成員的具體信息 TableRow personRow = new TableRow(this); for (int i = 0; i < lines; i++) { params.setMargins(1, 1, 1, 1); object; // 我在這里用Object代表表格顯示的內(nèi)容, // Object可以是字符串、數(shù)字,也可以是照片,看你具體的定義 if (object instanceof String) {// 字符串居中顯示 TextView textView = new TextView(this); textView.setLayoutParams(params); textView.setTextSize(29); if (colorChange % 2 == 1) textView.setBackgroundColor(getResources().getColor( R.color.second)); else textView.setBackgroundColor(getResources().getColor( R.color.third)); textView.setText(object.toString()); textView.setTextSize(30); textView.setGravity(Gravity.CENTER); personRow.addView(textView); } else if (object instanceof Number) {// 數(shù)字居右顯示 TextView textView = new TextView(this); textView.setPadding(0, 0, 5, 0);// 右內(nèi)邊距 textView.setLayoutParams(params); textView.setText(object.toString()); textView.setTextSize(30); textView.setTextSize(29); if (colorChange % 2 == 1) textView.setBackgroundColor(getResources().getColor( R.color.second)); else textView.setBackgroundColor(getResources().getColor( R.color.third)); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT); personRow.addView(textView); } else if (object instanceof byte[]) {// 顯示頭像 TableRow.LayoutParams params2 = new TableRow.LayoutParams(60, 75); ImageView imageView = new ImageView(this); if (colorChange % 2 == 1) imageView.setBackgroundColor(getResources().getColor( R.color.second)); else imageView.setBackgroundColor(getResources().getColor( R.color.third)); Bitmap bitmap = BitmapFactory.decodeByteArray((byte[]) object, 0, ((byte[]) object).length); imageView.setImageBitmap(bitmap); imageView.setLayoutParams(params2); personRow.addView(imageView); } else {// 空值 TextView textView = new TextView(this); textView.setLayoutParams(params); textView.setTextSize(30); if (colorChange % 2 == 1) textView.setBackgroundColor(getResources().getColor( R.color.second)); else textView.setBackgroundColor(getResources().getColor( R.color.third)); personRow.addView(textView); } } colorChange++; tableLayout.addView(personRow); } }
還可以對整個布局、整行或某個空間添加監(jiān)聽事件,只需setId(int id),然后在設(shè)立監(jiān)聽器即可。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android學(xué)習(xí)教程之動態(tài)GridView控件使用(6)
這篇文章主要為大家詳細(xì)介紹了Android動態(tài)GridView控件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Android入門之使用SQLite內(nèi)嵌式數(shù)據(jù)庫詳解
Android內(nèi)帶SQLite內(nèi)嵌式數(shù)據(jù)庫了。這對于我們存儲一些更復(fù)雜的結(jié)構(gòu)化數(shù)據(jù)帶來了極大的便利。本文就來和大家聊聊具體的使用方法,希望對大家有所幫助2022-12-12Android應(yīng)用開發(fā)中View繪制的一些優(yōu)化點解析
這篇文章主要介紹了Android應(yīng)用開發(fā)中View繪制的一些優(yōu)化點解析,包括Layout布局和硬件加速等方面,需要的朋友可以參考下2016-03-03android中使用Activity實現(xiàn)監(jiān)聽手指上下左右滑動
這篇文章主要介紹了android中使用Activity實現(xiàn)監(jiān)聽手指上下左右滑動,本文使用了Activity的ontouchEvent方法監(jiān)聽手指點擊事件,并給出代碼實例,需要的朋友可以參考下2015-05-05AndroidStudio 3.6 中 R.layout 找不到對應(yīng)的xml文件問題及解決方法
這篇文章主要介紹了AndroidStudio 3.6 中 R.layout 找不到對應(yīng)的xml文件問題,本文給出了解決方法對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Android判斷11位手機(jī)號碼的方法(正則表達(dá)式)
項目里頭需要做一個判斷用戶輸入的號碼是否是正確的手機(jī)號碼,正確的手機(jī)號碼應(yīng)該是11位的,這里我們需要用一個正則表達(dá)式來進(jìn)行判斷,下面我把寫法分享給大家2016-12-12Android開發(fā)實現(xiàn)的導(dǎo)出數(shù)據(jù)庫到Excel表格功能【附源碼下載】
這篇文章主要介紹了Android開發(fā)實現(xiàn)的導(dǎo)出數(shù)據(jù)庫到Excel表格功能,涉及Android數(shù)據(jù)庫及Excel表格相關(guān)操作技巧,并附帶完整源碼供讀者下載參考,需要的朋友可以參考下2018-03-03