关于android:Android的XML常用标签整理

1次阅读

共计 2395 个字符,预计需要花费 6 分钟才能阅读完成。

XML 和 HTML 的异同

惯例标签和自闭合标签

XML 和 HTML 有许多相似之处,XML 的标签分为两类。
一类是成对的标签,例如 <manifest> 和 </manifest>
另一类是自闭合标签,因为这一类标签的外面没有内容,所以它只须要一个标签就能实现所有性能,例如

<activity
    android:name=".MainActivity2"
    android:exported="false" />

自闭合标签能够转换成惯例的成对的标签,只须要去掉最初的 /,再加上 </name> 即可。

标签内的属性

相熟 HTML 的同学必定晓得 HTML 标签外部能够加 CSS 款式、定义 class、定义 ID 以及应用标签所领有的属性,而一对标签的两头显示的是内容。例如:

  <p style="color: white" class="mt-4"> 致力加载中... </p>

但 Android XML 的用法有轻微的区别,没有“标签两头的字符示意内容”这个性能。而是写在某个属性里。例如:

    <Button android:text="Hello"/>

常见 Android 页面元素的标签及属性

Android 主配置文件 AndroidManifest.xml

manifest 有显示的意思,这个文件定义了包名、流动、启动的流动等等。例如:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication" > ①

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" ②
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication" > ③
        <activity ④
            android:name=".MainActivity" ⑤
            android:exported="true" >
            <intent-filter>⑥
                <action android:name="android.intent.action.MAIN" /> ⑦
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>⑥
        </activity> ④
    </application>

</manifest>

常见属性:
① manifest 中的 package 定义了包名,包名就像其余语言中的命名空间,通知计算机这个类的住址,JAVA 依据包名 + 类名来确定惟一的类
② android:label 属性申明软件的软件名,这个题目运行时会写在页面的顶部(如果页面没有定义题目的状况下)
③ android:theme 属性申明的软件的主题,引入了另一个主题文件来设定款式,相似全局 CSS 文件
④ <activity> 标签是定义流动的标签,所有的流动都要写在这里,因为标签外部有其余内容,决裂成惯例的成对标签
⑤ android:name 属性,指出这个流动的类名,这个类名和 JAVA 文件绝对应。之所以有一个. 是因为 JAVA 查找类的时候应用“包名. 类名”
⑥ <intent-filter> 是用意选择器,用意用来实现流动之间的通信。用意选择器能够通知 Android 零碎,这个流动能够解决什么类型的用意
⑦ <action> 是一个动作,android.intent.action.MAIN 表名这个流动是主流动,也就是整个 Android 的入口

layout 布局

一个空的,没有任何元素的流动,代码是这个样子的:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout  ①
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" ②
    android:layout_height="match_parent"
    tools:context=".MainActivity"> ③
</androidx.constraintlayout.widget.ConstraintLayout>

① <androidx.constraintlayout.widget.ConstraintLayout> 这个标签指的是流动的布局(Layout)
② layout_width 属性用来定义此元素的宽度,layout_height 用来定义高度。
如果按住 Conmand 键点进去就能看到这个属性容许设定的值:

遇事不决就翻译:
fill_parent:指定视图的根本宽度。这是蕴含布局管理器的任何视图所必须的属性。其值能够是恒定宽度的尺寸(如“12dip”)或非凡常数之一。
视图应该和其父视图一样大(减去填充)。从 API 级别 8 开始,这个常量被弃用,并被 {@code match_parent} 替换
match_parent:视图应该和其父视图一样大(减去填充)。在 API 级别 8 中引入
wrap_content:视图的大小应仅足以蕴含其内容(加上填充)

③tools:context 属性表名这个流动的类名。

正文完
 0