【问题标题】:What is the different uses of attrs in android?android中attrs的不同用途是什么?
【发布时间】:2021-04-23 02:14:15
【问题描述】:

首先,我知道有很多关于这个问题的帖子,我想我都读过了。如果有人问我会附上他们批准:)

我对 attrs 有疑问。问题是有时我看到程序员在创建 自定义视图 时使用它,但有时我看到它们也在构建主题时使用。 例如:

  <declare-styleable name="Main_Theme">
   
    <attr name="background" format="reference" />
    <attr name="backgroundCard" format="reference" />
    <attr name="secondaryTextColor" format="reference" />
    <attr name="primaryTextColor" format="reference" />
    <attr name="primaryColor" format="reference" />
    <attr name="secondaryColor1" format="reference" />
    <attr name="secondaryColor2" format="reference" />
    <attr name="secondaryColor3" format="reference" />
    <attr name="dividerColor" format="reference" />

然后他们使用它创建一个主题。 例如:

    <item name="background">@color/dark_theme_background</item>
    <item name="backgroundCard">@color/dark_theme_card_background</item>
    <item name="secondaryTextColor">@color/dark_theme_scores_new</item>
    <item name="primaryTextColor">@color/dark_theme_primary_text_color</item>
    <item name="primaryColor">@color/dark_theme_primary_color</item>
    <item name="secondaryColor1">@color/dark_theme_secondary_1_color</item>
    <item name="secondaryColor2">@color/dark_theme_secondary_2_color</item>
    <item name="secondaryColor3">@color/dark_theme_secondary_3_color</item>
    <item name="dividerColor">@color/dark_theme_divider_color</item>

然后我看到他们使用这些项目作为布局活动 xml 文件中的属性值 所以我真的无法理解 attrs 的各种用途。 我希望有人能帮助我理解因为我感到很困惑

【问题讨论】:

    标签: java android layout attr


    【解决方案1】:

    attr 在使用自定义属性制作自定义视图时使用。 如果你定义了attr,你可以在xml文件中使用它。

    普通视图使用默认属性,例如宽度、高度、背景、文本等。 所以你可以在xml文件中使用它。

    <TextView
        android:width="match_parent"
        android:height="wrap_content"
        android:background="@color/white"
        android:text="@string/app_name" />
    

    但是您的自定义视图没有属性。

    如果你的自定义视图需要dividerColor属性,你不能使用它android:dividerColor如下。

    <YourCustomView
      android:width="match_parent"
      android:height="wrap_content"
      android:background="@color/white"
      android:dividerColor="@color/black" /> // it caused compile error.
    

    所以您需要在 xml 文件中使用您的属性。 为此,您需要在 attrs.xml 文件中声明 attrs。 (xml文件名可以更改。)

    <declare-styleable name="Main_Theme">
        <attr name="dividerColor" format="reference" />
    </declare-styleable>
    

    然后你就可以在视图 xml 文件中使用新属性了。

    <YourCustomView
        xmlns:app="http://schemas.android.com/apk/res-auto" // need define app
          android:width="match_parent"
          android:height="wrap_content"
          android:background="@color/white"
          app:dividerColor="@color/black" /> // it's works
    

    PS:您需要额外的代码才能在 YourCustomView 类文件中使用自定义属性。

    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Main_Theme);
    dividerColor = ta.getColor(R.styleable.MainTheme_divider_color, Color.WHITE);
    

    【讨论】:

    • 首先,我认为为了使您的最后一个示例能够正常工作,需要将 Main_Theme 替换为 YourCustomView.. 其次,我不明白 &lt;declare-styleable 可以用于自定义视图,也可以作为 xml 布局中属性的值
    • 您需要在自定义视图类中编写一些代码来获取自定义属性。我在回答中写了评论。
    • 可样式化的名称可以是任何东西。您可以在 R.styleable."Styleable Name" 的代码中使用它。
    • 你可以参考项目名称=的代码块吗?此外,在您的示例中。例如,divide_color 现在可以是 TextView 的属性?
    【解决方案2】:

    Attr 表示元素中的属性。简单地说,attrs 是在与文档关联的架构中定义的允许数量。

    每当我们创建自定义视图时,我们不希望视图接受所有可能的值,因此我们使用 attr 接口定义了一些视图可以接受的值。这些属性是使用 declare-stylable 定义的,使您能够为自定义视图定义属性。例如:

    <resources>
       <declare-styleable name="PieChart">
           <attr name="showText" format="boolean" />
           <attr name="labelPosition" format="enum">
               <enum name="left" value="0"/>
               <enum name="right" value="1"/>
           </attr>
       </declare-styleable>
    </resources>
    

    这个自定义视图接受 showText 和 label 属性。 showText 接受布尔值,labelPosition 接受值:左和右。像下面的例子

    <PieChart
        showText="false"
        labelPosition="left"/>
    

    类似地,在主题中,我们需要定义可以在主题中使用的某些属性,例如,如果我们创建 2 个主题并希望定义应用程序在使用任一主题时将使用的不同原色。我们首先在 attrs.xml 中声明颜色属性:

    <declare-styleable name="Main_Theme">
        <attr name="color_primary" format="color" />
      </declare-stylable>
    

    然后在 styles.xml 中,我们为 2 个主题定义 color_primary 属性的值

    <style name="theme_one">
        <item name="color_primary">#ff0000</item> // red
    </style>
    
    <style name="theme_two">
        <item name="color_primary">#00ff00</item> // green
    </style>
    

    然后可以在布局文件中使用此值,如下所示:?attr/color_primary


    同样,attr 可用于定义范围广泛的事物。一个 attr 元素本身有 2 个属性,您在代码的其他部分中引用它的名称和格式可以是colorbooleandimensionfloat 等。例如枚举可以定义为:

    <attr name="some_enum_attr">
      <enum name="value_one" value="1" />
      <enum name="value_two" value="2" />
    </attr>
    

    【讨论】:

    • 什么“防止” color_primary 成为布局 xml 中视图的属性(与 PieChart 完全相同)? &lt;attr name="showText" format="boolean" /&gt; &lt;attr name="labelPosition" format="enum"&gt; 是否可以用作布局 xml 中任何 View 的属性?
    • 与其说是预防不如说是该观点认为的合法和非法价值。您不能使用 showText 和 labelPosition 作为未在其架构中声明这些属性的视图的属性,如答案所示,如果我没有正确理解您的问题,请原谅我
    • 一切正常。感谢您的帮助.. 但我真的很想了解,所以请不要生我的气... 你说:you cannot use showText and labelPosition as attributes of views that haven't declared these attributes in their schema like shown in the answer但是PieChartCustomView 之间的“关系”是什么,所以CustomView 可以使用这些属性但其他视图不能?你为它做了什么?为什么color_primary 不能是CustomView 中的属性? CustomView和``Main_Theme`有什么区别?
    • 是什么让showText 属性被识别为'color_primary' 不存在?以及为什么 showTextCustomView 中被识别,但在 TextView 中却不被识别
    • 首先对一个小错误表示抱歉,实际上我的意思是自定义视图的名称是 PieChart 并且忘记在上面的 XML 示例中更改它。现在,回到您的问题,所有视图都声明了一个模式,其中声明了它接受的属性。然后它使用这个模式来读取用户提供的值,同时在 layout.xml 文件中定义这个视图。因此,在 layout.xml 文件中,如果您为其提供任何自定义属性(在创建视图时由开发人员定义的架构中不存在),它将无法理解/读取它!
    猜你喜欢
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多