2.在包“no01”内使用相对布局(RelativeLayout)创建Activity,要求: 显示一个按钮和一个静态文本控件,效果如图1所示。
时间: 2024-03-12 07:44:38 浏览: 32
好的,下面是在包“no01”内使用相对布局创建 Activity 的步骤:
1. 在包“no01”内创建一个新的 Activity,命名为 "MainActivity"。
2. 打开 "activity_main.xml" 文件,将布局改为相对布局。
```xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
```
3. 在布局中添加一个按钮和一个静态文本控件。
```xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/my_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="36sp"
android:layout_below="@id/my_button"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
```
4. 在 "MainActivity.java" 文件中,找到 "onCreate" 方法,并添加以下代码来绑定按钮和文本控件,并设置按钮的点击事件。
```java
Button myButton = findViewById(R.id.my_button);
TextView myTextView = findViewById(R.id.my_textview);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myTextView.setText("Button Clicked!");
}
});
```
5. 运行应用程序,查看效果。
现在,你已经成功使用相对布局创建了一个包含按钮和静态文本控件的 Activity。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)