写个安卓画面helloworld
时间: 2023-07-25 17:11:03 浏览: 41
好的,以下是一个简单的 Android 画面 HelloWorld 程序示例:
1. 在 Android Studio 中创建一个新项目,选择 "Empty Activity" 模板,并为您的项目命名。
2. 打开 activity_main.xml 文件并添加以下代码:
```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:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/hello_world_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="24sp" />
</LinearLayout>
```
3. 打开 MainActivity.java 文件并添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
private TextView helloWorldText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helloWorldText = findViewById(R.id.hello_world_text);
}
}
```
4. 运行应用程序并查看您的 HelloWorld 画面。
这个示例程序创建了一个简单的 LinearLayout,其中包含一个 TextView,用于显示 "Hello, World!" 的文本。在 MainActivity 中,我们在 onCreate() 方法中设置了 ContentView,并在此方法中获取了 TextView 的引用,以便我们可以在以后的代码中操作它。