@大家已經司空見慣。比如@+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/,可以直接在?後面加上屬性名稱。