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属性表名这个流动的类名。