灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:3056回复:0

Android的xml文件中的@和?的区别

楼主#
更多 发布于:2012-12-08 13:39

@大家已经司空见惯。比如@+id/...  @string/...


看下面代码:




[html]
<ProgressBar
   android:id="@+id/firstProgressBar"
   style="?android:progressBarStyleHorizontal"
   android:layout_width="200dp"
   android:layout_height="wrap_content"
   android:visibility="gone" />


   <ProgressBar
       android:id="@+id/firstProgressBar"
       style="?android:progressBarStyleHorizontal"
       android:layout_width="200dp"
       android:layout_height="wrap_content"
       android:visibility="gone" />
上面代码中用到了“?”,那么“?”是什么意思呢?


“?”引用主题属性,当您使用这个标记,你所提供的资源名必须能够在主题属性中找到,因为资源工具认为这个资源属性是被期望得到的,您不需要明确的指出它的类型(也就是不需要写全在哪个文件中?android:attr/android:textDisabledColor)


那么什么又叫主题属性呢?


你可以在Android的SDK的以下目录中找到attrs.xml文件


D:\android-sdk\platforms\android-16\data\res\values


打开这个文件可以看到,Android系统的属性都定义在这个文件中。


举个例子(ImageView):


[html]
<declare-styleable name="ImageView">
   <!-- Sets a drawable as the content of this ImageView. -->
   <attr name="src" format="reference|color" />
   <!-- Controls how the image should be resized or moved to match the size
        of this ImageView. -->
   <attr name="scaleType">
       <enum name="matrix" value="0" />
       <enum name="fitXY" value="1" />
       <enum name="fitStart" value="2" />
       <enum name="fitCenter" value="3" />
       <enum name="fitEnd" value="4" />
       <enum name="center" value="5" />
       <enum name="centerCrop" value="6" />
       <enum name="centerInside" value="7" />
   </attr>
   <!-- Set this to true if you want the ImageView to adjust its bounds
        to preserve the aspect ratio of its drawable. -->
   <attr name="adjustViewBounds" format="boolean" />
   <!-- An optional argument to supply a maximum width for this view.
        See {see android.widget.ImageView#setMaxWidth} for details. -->
   <attr name="maxWidth" format="dimension" />
   <!-- An optional argument to supply a maximum height for this view.
        See {see android.widget.ImageView#setMaxHeight} for details. -->
   <attr name="maxHeight" format="dimension" />
   <!-- Set a tinting color for the image -->
   <attr name="tint" format="color" />
   <!-- If true, the image view will be baseline aligned with based on its
        bottom edge -->
   <attr name="baselineAlignBottom" format="boolean" />
    <!-- If true, the image will be cropped to fit within its padding -->
   <attr name="cropToPadding" format="boolean" />
</declare-styleable>


   <declare-styleable name="ImageView">
       <!-- Sets a drawable as the content of this ImageView. -->
       <attr name="src" format="reference|color" />
       <!-- Controls how the image should be resized or moved to match the size
            of this ImageView. -->
       <attr name="scaleType">
           <enum name="matrix" value="0" />
           <enum name="fitXY" value="1" />
           <enum name="fitStart" value="2" />
           <enum name="fitCenter" value="3" />
           <enum name="fitEnd" value="4" />
           <enum name="center" value="5" />
           <enum name="centerCrop" value="6" />
           <enum name="centerInside" value="7" />
       </attr>
       <!-- Set this to true if you want the ImageView to adjust its bounds
            to preserve the aspect ratio of its drawable. -->
       <attr name="adjustViewBounds" format="boolean" />
       <!-- An optional argument to supply a maximum width for this view.
            See {see android.widget.ImageView#setMaxWidth} for details. -->
       <attr name="maxWidth" format="dimension" />
       <!-- An optional argument to supply a maximum height for this view.
            See {see android.widget.ImageView#setMaxHeight} for details. -->
       <attr name="maxHeight" format="dimension" />
       <!-- Set a tinting color for the image -->
       <attr name="tint" format="color" />
       <!-- If true, the image view will be baseline aligned with based on its
            bottom edge -->
       <attr name="baselineAlignBottom" format="boolean" />
        <!-- If true, the image will be cropped to fit within its padding -->
       <attr name="cropToPadding" format="boolean" />
   </declare-styleable>
以上是从attrs.xml文件中提取的,可以看到ImageView的一些特有属性(由于ImageView继承自View,所以View的属性自然就继承了)。
同样,Android系统也为Theme定义了很多属性(可以看看attrs.xml文件),其中每个主题属性的名称都一一对应D:\android-sdk\platforms\android-16\data\res\values目录下的themes.xml文件。当要用到主题属性的时候,就不需要特别指定android:attr/,可以直接在?后面加上属性名。

喜欢0 评分0
游客

返回顶部