引用资源时?和@的区别

参考博文:

https://blog.csdn.net/RichieZhu/article/details/52490521

https://blog.csdn.net/xx326664162/article/details/64125654

引用资源时,可能遇到下列情况,例如在设置style的时候可以使用“@”引用也可以使用“?”引用

 style="?android:attr/progressBarStyleHorizontal"
 style="@android:style/Widget.ProgressBar.Horizontal"

 我理解的这两种引用方式的根本区别在于:@是全局引用,而?是相对引用。

  • 使用@表示使用固定的style,而不会跟随Theme改变,这style可以在对应的style.xml中找到。
  • 使用?表示从Theme中查找引用的资源名,这个google叫预定义样式,用在多主题时的场景,属性值会随着主题而改变。

例如上面的progressBarStyleHorizontal,查看sdk\platforms\android-27\data\res\values\themes.xml文件和themes_holo.xml文件,可以看出在不同的 theme xml 文件中,progressBarStyleHorizontal引用的style是不同的。

在Theme中为

<item name="progressBarStyleHorizontal">@style/Widget.ProgressBar.Horizontal</item>

在Theme.Holo中为

 <item name="progressBarStyleHorizontal">@style/Widget.Holo.ProgressBar.Horizontal</item>

而上述引用的这两种style也是可以在对应style.xml中找到的,在styles.xml文件中的Widget.ProgressBar.Horizontal样式:

<style name="Widget.ProgressBar.Horizontal">
    <item name="indeterminateOnly">false</item>
    <item name="progressDrawable">@drawable/progress_horizontal</item>
    <item name="indeterminateDrawable">@drawable/progress_indeterminate_horizontal</item>
    <item name="minHeight">20dip</item>
    <item name="maxHeight">20dip</item>
    <item name="mirrorForRtl">true</item>
</style>

以及在styles_holo.xml文件中的Widget.Holo.ProgressBar.Horizontal样式:

<style name="Widget.Holo.ProgressBar.Horizontal" parent="Widget.ProgressBar.Horizontal">
    <item name="progressDrawable">@drawable/progress_horizontal_holo_dark</item>
    <item name="indeterminateDrawable">@drawable/progress_indeterminate_horizontal_holo</item>
    <item name="minHeight">16dip</item>
    <item name="maxHeight">16dip</item>
</style>

猜你喜欢

转载自blog.csdn.net/Ein3614/article/details/82024934