好的,我遵循developer.android.com上的Android教程來構建我的第一個應用程序。因此,爲了創建一個簡單的用戶界面,我在教程中添加了一個按鈕和文本字段。但是當我在手機上運行它時,我看不到按鈕或文本字段。無法將按鈕和文本字段添加到我的Android應用程序
package com.example.lookforbuttons;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv= new TextView(this);
tv.setText("Buttons");
setContentView(tv);
}
}
.xml文件,我描述的佈局是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
和strings.xml中看起來是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Buttons</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
目標Android版本是4.03,因爲我我正在4.03手機上進行測試。當我運行這個我只打印「按鈕」,沒有按鈕或文本字段。謝謝。