当前位置:编程学习 > XML/UML >>

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

@大家已经司空见惯。比如@+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 i

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,