Android-3-创建activity

发布时间 2023-09-15 08:53:02作者: Coder-Wang

activity是什么?

本质上就是一个一个的页面,可以在页面中放button,label,input等等

view是什么?

本质上就是activity中用于布局的一个个组件,是作为这些组件的

R是什么?

本来只是表示Android,但在这里表示整个布局框架的各种细节,比如要布局中的某个组件,页面的大小,横屏还是竖屏等等都存放在R中

创建一个activity有哪几步?

  • 在layout目录下创建xml文件
  • 创建xml文件对应的java代码(java代码中需要继承AppCompatActivity,然后再一个参数的onCreate方法中绑定java代码对应的xml布局文件,并且为其中的一些组件绑定一些事件)
  • 在AndroidManifest.xml中注册页面配置

布局代码(activity_demo_2.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:orientation="vertical"
    android:gravity="center">

<!--    为了便于做国际化,需要将其中的文字都作为变量放到values中的变量寄存中,然后在布局代码中尝试引用-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/activity2"/>

</LinearLayout>