@和?的区别

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

在设置style的时候既可以使用@也可以使用?,他们有什么区别呢??

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

例如上面的progressBarStyleHorizontal,查看\platforms\android-23\data\res\values\themes.xml文件,可以看到在不同的theme中,progressBarStyleHorizontal引用的style是不同的。看下面的示例:

在Theme中

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

Theme.Holo中为

 "progressBarStyleHorizontal">@style/Widget.Holo.ProgressBar.Horizontal

style和attr使用区别

style

@style/Widget.AppCompat.ProgressBar.Horizontal@android:style/Widget.ProgressBar.Horizontal

attr

"?attr/属性""?属性""?android:属性""?android:attr/属性"

另外:

当引用系统自带的style和attr时

"@android:style/主题""@style/android:主题"等同"?android:attr/属性""?attr/android:属性"等同

自定义随主题改变的属性

1、 如果是自定义控件,请在style.xml中或attrs.xml中声明属性:

<declare-styleable name="SunnyAttr">    <attr name="sunnyTextColor" format="reference"/>    <attr name="sunnyBgColor" format="reference"/>    <attr name="sunnyTextColorWhite" format="color"/>    <attr name="sunnyTextColorRed" format="reference"/>    <attr name="textColor" format="reference">attr>declare-styleable>

如代码第五行所示,必须指明format为reference。这样自定义控件的属性就可以在xml使用,如果不明白,查看这里

2、 在Theme中使用自定义的属性,可以再多个主题中定义不同的属性值

-- Base application theme. -->

3、 在对应的属性color,drawable等里面加入相应的资源

<color name="sunnyTextColorRed">#FFFF0000

4、这样就可以在xml中使用自定义控件的自定义属性,这个属性会随着主题而改变:

见下面第五行代码

<com.smartbracelet.sunny.sunnydemo3.SunnyTextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_centerInParent="true"    android:text="设置界面"    app:sunnyTextColor="?attr/sunnyTextColorRed"    />

Android xml中 @和?区别,style和attr小结
android中?attr/** 与@drawable/ ** 或@color/**等的区别

关注我的公众号,轻松了解和学习更多技术
这里写图片描述

更多相关文章

  1. Android Studio 之 AndroidManifest.xml文件中的android:label属
  2. 【ImageView】图片自适应及android:scaleType属性
  3. Android 属性总结
  4. android onTouchEvent和setOnTouchListener中onTouch的区别
  5. android中gravity和layoutgravity的区别
  6. Android中visibility的3个属性说明
  7. Textview基本属性及功能

随机推荐

  1. android 实用sax 读取xml文件内容
  2. Android(安卓)中如何得到字符的像素宽度
  3. android 开机动画修改以及默认壁纸
  4. Android问题集锦
  5. Android状态栏适配源码解析。
  6. Android调用getSimSerialNumber获取iccid
  7. 2013.03.19(7)———android scrollview和l
  8. Android仿QQ消息列表ListView滑动删除效
  9. Android(安卓)7.0 使用FileProvider 在应
  10. Android中Intent介绍