您的位置:首页 > 其它

认识头部和部件 status_bar.xml

2013-12-31 11:58 330 查看
认识头部和部件
XML,就是eXtensible Markup Language的缩写,中文译为可扩展标记语言,它是一种文本语言,可以使得信息“结构化”,整齐、易读取。

我们来从第1行看起

<?xml version="1.0" encoding="utf-8"?>

复制代码
这行描述了xml的版本(xml version="1.0")以及xml文件编码格式(encoding="utf-8")

第二行是一行很长的代码:

<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称作一个部件类名。它反映了这个控件的部件类是什么。

例如我们经常看到的按钮,其实它们都是一个基础按钮类再加上各种自定义选项形成的。

我们将这行代码格式化一下:

<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">

复制代码
可读性好多了 :>

现在开始分析:

<、> 控件信息的开始和结束符号,需要配对开始和结束(例如<a>需要配对</a>,中间存放着它的子控件),如果你看到的结束符号是/>不是>,那就不用配对结束了(例如<a/>就不用再写一个</a>了)

com.android.systemui.statusbar.phone.PhoneStatusBarView 基础控件

android:id="@id/status_bar" 控件属性的自定义(以此类推,android:background="@drawable/status_bar_background"等也是)

我们和下列代码(第7行)对比下:

<ImageView

android:id="@id/notification_lights_out"

android:paddingLeft="6.0dip"

android:paddingBottom="2.0dip"

android:visibility="gone"

android:layout_width="@dimen/status_bar_icon_size"

android:layout_height="fill_parent"

android:src="@drawable/ic_sysbar_lights_out_dot_small"

android:scaleType="center" />

复制代码
有没有发现上面代码的部件类名好短?因为ImageView这个部件是Android提供的,只要是Android就都会有,这时候就可以直接提供类名。

如果你不是使用Android提供的控件,就像com.android.systemui.statusbar.phone.PhoneStatusBarView那样,就要填写完整的包名。

一般来说,常见的部件类有以下几种(均为Android提供):

Button 按钮,不用多说了

ImageView 显示图片的部件

TextView 显示文字的部件

在这个xml里面,你会碰到以下部件:

com.android.systemui.statusbar.phone.PhoneStatusBarView 状态栏

com.android.systemui.TorchServiceView 具体我也不是很清楚,似乎是手电筒服务

com.android.systemui.statusbar.phone.BatteryIndicator 顶部方式显示的电池指示器

com.android.systemui.statusbar.phone.BatteryIndicatorCharging 同上,不过这是充电时候的

com.android.systemui.statusbar.NetworkSpeedView 网速显示

com.android.systemui.statusbar.policy.Clock 时钟,我们以后会经常和他打交道

com.android.systemui.statusbar.StatusBarIconView 状态栏的省略号

com.android.systemui.statusbar.phone.IconMerger 不明,图标合并的?

com.android.systemui.statusbar.phone.BatteryStatusIconView 数字和图标的电池状态指示器

ImageSwitcher 也是个可以显示图片的

com.android.systemui.statusbar.AnimatedImageView 动画图片

com.android.systemui.statusbar.phone.TickerView 弹出通知的显示就靠他了

Chronometer 秒表

嗯,大概就那么多!

容器与布局
很多人肯定都有疑惑,这个文件里面出现了大量的LinearLayout,为什么上面的部件列表丝毫未提。其实,LinearLayout是容器,不是部件。 什么是容器?容器就是可以将一批部件(和子容器)组织成特定的结构。就和我们存放物品差不多,就像将几个圆球从上到下放到一个瓶子容器,将一堆书放进箱子容器里面。程序里面,如果你要让某个部件和另一个部件对齐,那就得使用容器。

线性布局-LinearLayout
LinearLayout就是最简单的布局——线性布局了。这个容器会让里面的子部件呈一列或者一行对齐,然后一个个地按指定顺序摆放。遵循盒模型。

看一个来自第24行的线性布局起始代码:

<LinearLayout

android:orientation="horizontal"

android:id="@id/ticker"

android:paddingLeft="6.0dip"

android:animationCache="false"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

复制代码
从LinearLayout我们可以知道它是一个线性布局容器,下面的属性我们只挑选几个来说明,其它留待后面讲述。

方向
android:orientation="horizontal" 这个属性设置了线性布局的方向,前面说到线性布局可以是一列或者一行,如果在这个属性设置为vertical就是一列,horizontal就是一行。



无标题.png (9.41 KB, 下载次数: 0)

下载附件 保存到相册发送到手机

填充模型
android:layout_width="fill_parent"

复制代码



无标题3.png (10.84 KB, 下载次数: 0)

下载附件 保存到相册发送到手机

这段代码是指定这个部件和父部件/容器的关系,取值“fill_parent”时,宽度=整个父部件/容器的宽度;取值“wrap_content”时,宽度=显示内容所需的宽度;取值“match_parent”时,和fill_parent一样;取值具体数值(例如200px)时,宽度=所给的具体数值。

权重
android:layout_height="fill_parent"

复制代码
这个属性可以表示部件的权重,默认为0(显示多少东西就占用多少空间),数值越低(起作用的话最小为1)的话,在分配剩余空间的时候分得的空间越大。



无标题2.png (5.43 KB, 下载次数: 0)

下载附件 保存到相册发送到手机

再来看34行:

<LinearLayout

android:gravity="center"

android:id="@id/returnToDriveModeScreen"

android:background="@drawable/status_bar_orange_bar_bg"

android:visibility="gone"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

复制代码
重力
android:gravity="center"

复制代码
很多时候我们要要求一个容器里面的部件按一定规律对齐,重力就是容器内部件对齐的一种手段,可以将全部部件居左、居中、居右。



无标题1.png (6.8 KB, 下载次数: 0)

下载附件 保存到相册发送到手机

环中环——include
肯定有人还发现有这么一行(19行):

<include

android:id="@id/signal_cluster"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

layout="@layout/signal_cluster_view" />

复制代码
include严格来说是一种特殊部件,它是一种引用部件,你可以看成,这里有一个部件,里面还有别的布局文件控制。

例如上面的代码就引用了layout/signal_cluster_view.xml文件的内容了。

from: http://www.miui.com/thread-1348881-1-1.html
一个月米见,大家是不是有点想我呢?学生党就是坑,手机时常要受到成绩的影响,苦逼…… 上一节课,我们讲了MIUISystem.apk反编译后的结构,并且找到了掌管状态栏布局的status_bar.xml。这节课,我们来看看status_bar.xml这个文件怎样构成了看到的状态栏。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: