自学内容网 自学内容网

第4天:用户界面和布局补充材料——`activity_login.xml`解读

下面是对“第4天:用户界面和布局补充材料”该文学习的更深层次的补充材料,对 activity_login.xml 文件的理解。
下面对`activity_login.xml’ 文件中每一行进行详细解释:

<?xml version="1.0" encoding="utf-8"?>
  • 声明XML版本和编码:这行代码声明了XML文档的版本(1.0)和字符编码(UTF-8)。
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">
  • LinearLayout:这是一个线性布局容器,所有子视图将按顺序排列。
  • xmlns:android:定义了Android命名空间,允许使用Android特有的属性。
  • android:orientation:设置布局的方向为垂直(vertical),即子视图从上到下排列。
  • android:layout_width:设置布局的宽度为“match_parent”,表示占满父容器的宽度。
  • android:layout_height:设置布局的高度为“match_parent”,表示占满父容器的高度。
  • android:padding:设置布局内边距为16dp,确保内容与边缘有一定的距离。
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="登录"
    android:textSize="24sp"
    android:layout_gravity="center"/>
  • TextView:用于显示文本的视图。
  • android:layout_width:设置宽度为“wrap_content”,表示宽度根据内容自适应。
  • android:layout_height:设置高度为“wrap_content”,表示高度根据内容自适应。
  • android:text:显示的文本内容为“登录”。
  • android:textSize:设置文本大小为24sp(可缩放像素),适应不同屏幕密度。
  • android:layout_gravity:将该TextView在父布局中居中显示。
<EditText
    android:id="@+id/et_username"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="用户名"/>
  • EditText:用于用户输入文本的视图。
  • android:id:为该视图分配唯一标识符,便于在代码中引用。
  • android:layout_width:设置宽度为“match_parent”,占满父容器的宽度。
  • android:layout_height:设置高度为“wrap_content”,根据内容自适应。
  • android:hint:提示文本为“用户名”,在没有输入内容时显示。
<EditText
    android:id="@+id/et_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="密码"
    android:inputType="textPassword"/>
  • EditText:同样用于用户输入文本。
  • android:id:唯一标识符为“et_password”。
  • android:layout_width:占满父容器的宽度。
  • android:layout_height:根据内容自适应高度。
  • android:hint:提示文本为“密码”。
  • android:inputType:设置输入类型为“textPassword”,表示输入的内容将以密码形式隐藏(用点或星号显示)。
<Button
    android:id="@+id/btn_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="登录"/>
  • Button:一个可点击的按钮视图。
  • android:id:唯一标识符为“btn_login”。
  • android:layout_width:占满父容器的宽度。
  • android:layout_height:根据内容自适应高度。
  • android:text:按钮上显示的文本为“登录”。
</LinearLayout>
  • 结束标签:关闭LinearLayout容器,表示布局的结束。

整体上,这段XML代码定义了一个简单的登录界面,包含一个标题、两个输入框(用户名和密码)以及一个登录按钮。


原文地址:https://blog.csdn.net/weixin_41644568/article/details/142753472

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!