认识头部和部件 status_bar.xml

前端之家收集整理的这篇文章主要介绍了认识头部和部件 status_bar.xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
认识头部和部件
XML,就是eXtensible Markup Language的缩写,中文译为可扩展标记语言,它是一种文本语言,可以使得信息“结构化”,整齐、易读取。@H_403_16@ 我们来从第1行看起
  1. <?xml version="1.0" encoding="utf-8"?>
复制代码
这行描述了xml的版本(xml version="1.0")以及xml文件编码格式(encoding="utf-8")@H_403_16@ @H_403_16@ 第二行是一行很长的代码
  1. <com.android.systemUI.statusbar.phone.PhoneStatusBarView android:id="@id/status_bar" android:background="@drawable/status_bar_background" android:focusable="true" android:fitsSystemWindows="true" android:descendantFocusability="afterDescendants" xmlns:android="http://schemas.android.com/apk/res/android">
复制代码
这是一个部件。我们把诸如com.android.systemUI.statusbar.phone.PhoneStatusBarView称作一个部件类名。它反映了这个控件的部件类是什么。@H_403_16@ 例如我们经常看到的按钮,其实它们都是一个基础按钮类再加上各种自定义选项形成的。@H_403_16@ @H_403_16@ 我们将这行代码格式化一下:
  1. <com.android.systemUI.statusbar.phone.PhoneStatusBarView @H_403_16@
  2. android:id="@id/status_bar" @H_403_16@
  3. android:background="@drawable/status_bar_background" @H_403_16@
  4. android:focusable="true" @H_403_16@
  5. android:fitsSystemWindows="true" @H_403_16@
  6. android:descendantFocusability="afterDescendants" @H_403_16@
  7. xmlns:android="http://schemas.android.com/apk/res/android">
复制代码
可读性好多了 :>@H_403_16@ 现在开始分析:@H_403_16@ <、> 控件信息的开始和结束符号,需要配对开始和结束(例如<a>需要配对</a>,中间存放着它的子控件),如果你看到的结束符号是/>不是>,那就不用配对结束了(例如<a/>就不用再写一个</a>了)@H_403_16@ com.android.systemUI.statusbar.phone.PhoneStatusBarView 基础控件@H_403_16@ android:id="@id/status_bar" 控件属性自定义(以此类推,android:background="@drawable/status_bar_background"等也是)@H_403_16@ @H_403_16@ 我们和下列代码(第7行)对比下:
  1. <ImageView @H_403_16@
  2. android:id="@id/notification_lights_out" @H_403_16@
  3. android:paddingLeft="6.0dip" @H_403_16@
  4. android:paddingBottom="2.0dip" @H_403_16@
  5. android:visibility="gone" @H_403_16@
  6. android:layout_width="@dimen/status_bar_icon_size" @H_403_16@
  7. android:layout_height="fill_parent" @H_403_16@
  8. android:src="@drawable/ic_sysbar_lights_out_dot_small" @H_403_16@
  9. android:scaleType="center" />
复制代码
有没有发现上面代码的部件类名好短?因为ImageView这个部件是Android提供的,只要是Android就都会有,这时候就可以直接提供类名。@H_403_16@ 如果你不是使用Android提供的控件,就像com.android.systemUI.statusbar.phone.PhoneStatusBarView那样,就要填写完整的包名。@H_403_16@ @H_403_16@ 一般来说,常见的部件类有以下几种(均为Android提供):@H_403_16@ Button 按钮,不用多说了@H_403_16@ ImageView 显示图片的部件@H_403_16@ TextView 显示文字的部件@H_403_16@ @H_403_16@ 在这个xml里面,你会碰到以下部件:@H_403_16@ com.android.systemUI.statusbar.phone.PhoneStatusBarView 状态栏@H_403_16@ com.android.systemUI.TorchServiceView 具体我也不是很清楚,似乎是手电筒服务@H_403_16@ com.android.systemUI.statusbar.phone.BatteryIndicator 顶部方式显示的电池指示器@H_403_16@ com.android.systemUI.statusbar.phone.BatteryIndicatorCharging 同上,不过这是充电时候的@H_403_16@ com.android.systemUI.statusbar.NetworkSpeedView 网速显示@H_403_16@ com.android.systemUI.statusbar.policy.Clock 时钟,我们以后会经常和他打交道@H_403_16@ com.android.systemUI.statusbar.StatusBarIconView 状态栏的省略号@H_403_16@ com.android.systemUI.statusbar.phone.IconMerger 不明,图标合并的?@H_403_16@ com.android.systemUI.statusbar.phone.BatteryStatusIconView 数字和图标的电池状态指示器@H_403_16@ ImageSwitcher 也是个可以显示图片的@H_403_16@ com.android.systemUI.statusbar.AnimatedImageView 动画图片@H_403_16@ com.android.systemUI.statusbar.phone.TickerView 弹出通知显示就靠他了@H_403_16@ Chronometer 秒表@H_403_16@ @H_403_16@ 嗯,大概就那么多!@H_403_16@ @H_403_16@
容器与布局
很多人肯定都有疑惑,这个文件里面出现了大量的LinearLayout,为什么上面的部件列表丝毫未提。其实,LinearLayout是容器,不是部件。 什么是容器?容器就是可以将一批部件(和子容器)组织成特定的结构。就和我们存放物品差不多,就像将几个圆球从上到下放到一个瓶子容器,将一堆书放进箱子容器里面。程序里面,如果你要让某个部件和另一个部件对齐,那就得使用容器。@H_403_16@ @H_403_16@
线性布局-LinearLayout
LinearLayout就是最简单的布局——线性布局了。这个容器会让里面的子部件呈一列或者一行对齐,然后一个个地按指定顺序摆放。遵循盒模型。@H_403_16@ @H_403_16@ 看一个来自第24行的线性布局起始代码
  1. <LinearLayout @H_403_16@
  2. android:orientation="horizontal" @H_403_16@
  3. android:id="@id/ticker" @H_403_16@
  4. android:paddingLeft="6.0dip" @H_403_16@
  5. android:animationCache="false" @H_403_16@
  6. android:layout_width="fill_parent" @H_403_16@
  7. android:layout_height="fill_parent">
复制代码
从LinearLayout我们可以知道它是一个线性布局容器,下面的属性我们只挑选几个来说明,其它留待后面讲述。@H_403_16@ @H_403_16@
方向
@H_403_16@ android:orientation="horizontal" 这个属性设置了线性布局的方向,前面说到线性布局可以是一列或者一行,如果在这个属性设置为vertical就是一列,horizontal就是一行。@H_403_16@ @H_403_16@ @H_403_16@
填充模型
  1. android:layout_width="fill_parent"
复制代码
@H_403_16@ 这段代码是指定这个部件和父部件/容器的关系,取值“fill_parent”时,宽度=整个父部件/容器的宽度;取值“wrap_content”时,宽度=显示内容所需的宽度;取值“match_parent”时,和fill_parent一样;取值具体数值(例如200px)时,宽度=所给的具体数值。@H_403_16@ @H_403_16@
权重
  1. android:layout_height="fill_parent"
复制代码
这个属性可以表示部件的权重,默认为0(显示多少东西就占用多少空间),数值越低(起作用的话最小为1)的话,在分配剩余空间的时候分得的空间越大。@H_403_16@ @H_403_16@ @H_403_16@ 再来看34行:
  1. <LinearLayout @H_403_16@
  2. android:gravity="center" @H_403_16@
  3. android:id="@id/returnToDriveModeScreen" @H_403_16@
  4. android:background="@drawable/status_bar_orange_bar_bg" @H_403_16@
  5. android:visibility="gone" @H_403_16@
  6. android:layout_width="fill_parent" @H_403_16@
  7. android:layout_height="fill_parent">
复制代码
重力
  1. android:gravity="center"
复制代码
很多时候我们要要求一个容器里面的部件按一定规律对齐,重力就是容器内部件对齐的一种手段,可以将全部部件居左、居中、居右。@H_403_16@ @H_403_16@ @H_403_16@
环中环——include
@H_403_16@ 肯定有人还发现有这么一行(19行):
  1. <include @H_403_16@
  2. android:id="@id/signal_cluster" @H_403_16@
  3. android:layout_width="wrap_content" @H_403_16@
  4. android:layout_height="wrap_content" @H_403_16@
  5. layout="@layout/signal_cluster_view" />
复制代码
include严格来说是一种特殊部件,它是一种引用部件,你可以看成,这里有一个部件,里面还有别的布局文件控制。@H_403_16@ 例如上面的代码就引用了layout/signal_cluster_view.xml文件内容了。@H_403_16@ @H_403_16@ @H_403_16@
@H_403_16@

from: http://www.miui.com/thread-1348881-1-1.html

一个月米见,大家是不是有点想我呢?学生党就是坑,手机时常要受到成绩的影响,苦逼…… 上一节课,我们讲了MIUISystem.apk反编译后的结构,并且找到了掌管状态栏布局的status_bar.xml。这节课,我们来看看status_bar.xml这个文件怎样构成了看到的状态栏。

原文链接:https://www.f2er.com/xml/299343.html

猜你在找的XML相关文章