参见英文答案 >
Android Styles: Difference between ‘style=“@android:style/XYZ”’ and ‘style=“?android:attr/XYZ”’? 2个
我想使用一种风格的Android库.我想我应该使用@(案例2)作为样式属性,但是我看到了一个来自网站的样本,它使用了什么? (情况1).我不知道为什么.
Android lib是否包含命名的listSeparatorTextViewStyle资源和名为listSeparatorTextViewStyle属性的样式?是的,我确实在系统android lib attrs.xml中找到了名为listSeparatorTextViewStyle属性的样式,但是在哪里可以找到命名的listSeparatorTextViewStyle资源?
这是代码和效果
情况1
<TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/listSeparatorTextViewStyle"
/>
案例2
<TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:attr/listSeparatorTextViewStyle"
/>
最佳答案 引用资源属性:
>可以使用R.java中的整数从代码中引用资源,例如R.drawable.myimage
>可以使用特殊的XML语法从资源引用资源,例如@ drawable / myimage
引用样式属性:
>样式属性资源允许您在当前应用的主题中引用属性的值.引用样式属性允许您通过设置UI元素的样式来自定义UI元素的外观,以匹配当前主题提供的标准变体,而不是提供硬编码值.引用样式属性实质上是说“在当前主题中使用由此属性定义的样式”.
>要引用样式属性,名称语法几乎与普通资源格式相同,但使用问号(?)而不是at符号(?),资源类型部分是可选的.
>?[< package_name>:] [< resource_type> /]< resource_name>
EX ::
<EditText id="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="?android:textColorSecondary"
android:text="@string/hello_world" />
>这里,android:textColor属性指定当前主题中样式属性的名称. Android现在使用应用于android:textColorSecondary样式属性的值作为此窗口小部件中android:textColor的值.因为系统资源工具知道在此上下文中需要属性资源,所以您不需要显式声明类型(可以是?android:attr / textColorSecondary) – 您可以排除attr类型.
参考:: Android Docs – 它就在那里