Android開發入門(九)使用者介面 9.1 純編碼實現UI

來源:互聯網
上載者:User

到目前為止,在所有的章節中,我們所看見的UI都是通過XML來實現的。之前也提及過,除了使用XML,也 可以使用代碼來實現UI介面。這個方式是很有用的,比如你的UI需要在運行時被產生。舉個例子,假設你在 編寫一個“電影票的預定系統”,你的程式使用按鈕Buttons去顯示每個電影院的座位。在這種情況下,你就 會需要依據實際情況去動態地產生UI。

下面的例子將會展示如何通過編碼,動態地在Activity中產生 UI。

1. 建立一個工程:UICode。

2. UICodeActivity.java中的代碼。

public class UICodeActivity extends Activity {          /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {              super.onCreate(savedInstanceState);              //setContentView(R.layout.main);                       LayoutParams params =                   new LinearLayout.LayoutParams(                      LayoutParams.FILL_PARENT,                      LayoutParams.WRAP_CONTENT);                        LinearLayout layout = new LinearLayout(this);              layout.setOrientation(LinearLayout.VERTICAL);                         TextView tv = new TextView(this);              tv.setText("This is a TextView");              tv.setLayoutParams(params);                       Button btn = new Button(this);              btn.setText("This is a Button");              btn.setLayoutParams(params);                                                 layout.addView(tv);                         layout.addView(btn);                         LinearLayout.LayoutParams layoutParam =                   new LinearLayout.LayoutParams(                          LayoutParams.FILL_PARENT,                          LayoutParams.WRAP_CONTENT );                         this.addContentView(layout, layoutParam);          }      }

3. 按F11調試,效果圖如下。

在這個例子中,首先要把setContentView()方法注釋掉,目的是不讓Activity去載入main.xml中的UI視圖。

然後,建立一個LayoutParams對象,這個對象指定了布局的屬性。

LayoutParams params =           new LinearLayout.LayoutParams(              LayoutParams.FILL_PARENT,              LayoutParams.WRAP_CONTENT);

也要建立一個LinearLayout對象,這個對象包含了 activity中的所有視圖。

LinearLayout layout = new LinearLayout(this);   layout.setOrientation(LinearLayout.VERTICAL);

然後,建立一個TextView和一個Button。

TextView tv = new TextView(this);   tv.setText("This is a TextView");   tv.setLayoutParams(params);        Button btn = new Button(this);   btn.setText("This is a Button");   btn.setLayoutParams(params);

然後,把它們加到LinearLayout對象中去。

layout.addView(tv);        layout.addView(btn);

同時,也要建立一個LayoutParams對象,供LinearLayout對象使用。

LinearLayout.LayoutParams layoutParam =        new LinearLayout.LayoutParams(               LayoutParams.FILL_PARENT,               LayoutParams.WRAP_CONTENT );

最後,把這個LinearLayout對象添加到Activity中去 。

this.addContentView(layout, layoutParam);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.