在Android上,用戶界面通常應採用XML的文件被創建,而不是Java代碼。你應該對android.com教程讀了起來,尤其是:
http://developer.android.com/guide/topics/ui/declaring-layout.html
一個例子:
在你RES /佈局/ main.xml中,您定義文本的TextView的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/TextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 1"/>
<TextView android:id="@+id/TextView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 2"/>
</LinearLayout>
然後,如果你在活動中顯示此使用的setContentView,應用程序會顯示到TextView中的:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
}
如果你想以編程方式設置活動中的文字,只需要使用findViewById():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
((TextView)this.findViewById(R.id.TextView1)).setText("Setting text of TextView1");
}
那麼如何繪製多個變量? – 2010-09-29 11:41:38
我增加了一個例子,但認真看了一下Android文檔,他們有很多例子和資源。如果你來自其他環境,Android UI界首先有點奇怪。 – TuomasR 2010-09-29 11:46:04