android - 为什么样本使用?引用样式 listSeparatorTextViewStyle 时?

标签 android android-xml android-styles

<分区>

我想使用一种风格的 Android 库。我想我应该为 style 属性使用 @ (案例 2),但我从网站上看到了一个示例,它使用 ? (案例 1) .我不知道为什么。

Android 库是否同时包含命名的 listSeparatorTextViewStyle 资源和名为 listSeparatorTextViewStyle 的样式属性?是的,我确实在系统 android lib attrs.xml 中找到了名为 listSeparatorTextViewStyle 属性的样式,但是我在哪里可以找到名为 listSeparatorTextViewStyle 的资源?

下面是代码和效果

案例一

<TextView
    android:id="@+id/dialog_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    style="?android:attr/listSeparatorTextViewStyle"  
/>

Case1.GIF

案例二

<TextView
    android:id="@+id/dialog_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    style="@android:attr/listSeparatorTextViewStyle"  
/>

Case2.GIF

最佳答案

引用资源属性:

  • 可以使用 R.java 中的整数从代码中引用资源,例如 R.drawable.myimage
  • 可以使用特殊的 XML 语法从资源中引用资源,例如 @drawable/myimage

引用样式属性:

  • A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."

  • To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@), use a question-mark (?), and the resource type portion is optional.

  • ?[<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" />
  • Here, the android:textColor attribute specifies the name of a style attribute in the current theme. Android now uses the value applied to the android:textColorSecondary style attribute as the value for android:textColor in this widget. Because the system resource tool knows that an attribute resource is expected in this context, you do not need to explicitly state the type (which would be ?android:attr/textColorSecondary)—you can exclude the attr type.

引用::Android Docs - 都在那里

关于android - 为什么样本使用?引用样式 listSeparatorTextViewStyle 时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25322402/

相关文章:

java - 构造函数 Intent 未定义

java - 如何确保我的微调器(菜单内)更新我的菜单标题并确保标题完整显示?

android - 在 Android 中更改 ActionMode 背景颜色时遇到问题

android - 内存不足错误 : bitmap size exceeds VM budget (Android)

java - 使用 LibGDX 顺时针旋转图像

java - 如何获取网站内容并将其以我们想要的方式放入 android 应用程序布局中?

android - 在 R.attr 中找到正确的样式

Android BitmapDrawable setTileModeX 不适用于 TextView

android - 自定义 View 构造函数中的重写样式

android - 如何使用 styles.xml 自定义 SpinnerItemStyle