1 xmlns:android="http://schemas.android.com/apk/res/android"什么意思
在那个LinearLayout,也就是线性布局中,第一行就是它。
这行代码是XML命名空间声明的一部分,它用于Android开发中定义XML资源文件时指定Android特定的属性和元素。
xmlns 是“XML Namespace”的缩写,用于声明XML文档中使用的命名空间。
android 是这个命名空间的别名,当你在XML文件中使用Android框架提供的属性或元素时,会通过这个别名来引用。
“http://schemas.android.com/apk/res/android” 是命名空间的URI(统一资源标识符),它是一个唯一标识符,用于定位Android框架中定义的XML元素和属性的规范。
2
<?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:gravity = "center"
android:orientation="vertical">
<ImageView
android:layout_width = "100dp"
android:layout_height = "100dp"
android:background = "#F00"
/>
<ImageView
android:layout_width = "100dp"
android:layout_height = "100dp"
android:background = "#0F0"
/>
<ImageView
android:layout_width = "100dp"
android:layout_height = "100dp"
android:background = "#00F"
/>
</LinearLayout>
LinearLayout是一种线性布局
android:orientation = "vertical"代表垂直方向
android:orientation = "horizontal"代表水平方向
android:gravity = “center”,重心在中间,就是全部居中的意思。
android:gravity = "top"重心在顶部,就是那三个小方块左上角
android:gravity = "top|centerhorizontal"重心在顶部,也就是在天花板的中间
android:gravity = "top|right"重心在顶部的右面,也就是在天花板的右面
android:gravity = “center”
android:gravity = “center|left”
android:gravity = “center|right”
android:gravity = “bottom”
android:gravity = “bottom|center_horizontal”
android:gravirt = “bottom|right”
3
如果要按比例分配这几个小方块所占的地方,那就需要weight
<ImageView
android:layout_width = "0dp"
android:layout_weight = "1"
android:layout_height = "100dp"
android:background = "#F00"
/>
这个图片就会把除掉那200dp以外的地方全部占满
<ImageView
android:layout_width = "0dp"
android:layout_weight = "1"
android:layout_height = "100dp"
android:background = "#F00"
/>
<ImageView
android:layout_width = "0dp"
androd:layout_weight = "2"
android:layout_height = "100dp"
android:background = "#0F0"
/>
<ImageView
android:layout_width = "0dp"
android:layout_weight = "3"
android:layout_height = "100dp"
android:background = "#00F"
/>
这三个小方块按照1:2:3的比例占据所有的空间。