【问题标题】:Android custom attributes not showingAndroid自定义属性未显示
【发布时间】:2016-03-21 10:16:07
【问题描述】:

我看到很多人想知道如何为自定义组件获取自定义属性,但这不是我的问题。我创建了一个自定义组件,我正在尝试添加属性,但是当我在我的 xml 文件顶部添加命名空间时,它只会找到两个随机自定义属性“paddingEnd”和“paddingStart”。

<resources>
    <declare-styleable name="menu_item_attrs">
        <attr name="imageId" format="integer" />
        <attr name="menuText" format="string" />
    </declare-styleable>
</resources>

这是 attrs.xml 文件。

public MenuListItem(Context context, AttributeSet set) {
    super(context, set);

    TypedArray a = context.obtainStyledAttributes(set, R.styleable.menu_item_attrs);
    if (a == null) {
        return;
    }
    CharSequence s = a.getString(R.styleable.menu_item_attrs_menuText);
    if (s != null) {
        // do something
    }
}

这是我的自定义类中的构造函数。

        <LinearLayout
            xmlns:custom="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/expanding_layout"
            android:background="#029eed">

            <aaron.testappanim.MenuListItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
        </LinearLayout>

这是我正在使用的组件。我想向“imageId”和“menuText”添加值,但它们不可用。唯一显示的是与填充相关的内容,如下所示。

各位有什么想法吗?

【问题讨论】:

    标签: android attributes components


    【解决方案1】:

    虽然这篇文章有点老了,但为了提供信息,我将分享我的案例。

    我定义了一个名为 attrs.xml 的自定义属性文件以与 MaterialCardView 一起使用:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="DeviceMaterialCardView">
            <attr name="state_none" format="boolean"/>
            <attr name="state_favorite" format="boolean"/>
            <attr name="state_dragging" format="boolean"/>
        </declare-styleable>
    </resources>
    

    我用这个选择器通过拖动attr来改变cardview的笔画颜色:

    <?xml version="1.0" encoding="utf-8"?>
    <selector
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:himtec="http://schemas.android.com/apk/res-auto">
    
        <item android:color="?attr/colorSecondary" himtec:durum_surukle="true"/>
        <item android:color="?attr/colorSecondary" android:state_activated="true"/>
        <item android:alpha="0.2" android:color="?attr/colorOnBackground"/>
    </selector>
    

    最后我有了名为DeviceMaterialCardView 的自定义类。现在自定义属性与自定义类具有相同的名称。但是我在编译him_stroke_color xml 文件时遇到了编译错误:

    /home/Projeler/PK-BLE-RF/Android/Uygulama/PK0101BLE/app/src/main/res/color/him_stroke_color.xml:6: AAPT: 错误: 属性 com.himtec.himtec_kontrol:state_dragging not found .

    我仍然陷入困境,无法弄清楚如何解决这个问题。

    【讨论】:

      【解决方案2】:

      我的解决方案是: 在 attr.xml 文件中,您可能已将自定义属性声明如下

      <declare-styleable name="IDEditText"> <attr name="customFont" format="string"/> </declare-styleable>

      这里declare-styleable的name字段必须是自定义视图的名称。然后它将显示在建议中。

      在我的情况下,我的自定义视图名称是 IDEditText,因此是 declare-styleable name="IDEditText"

      【讨论】:

      • 这是正确的..但是已经回答了!还是谢谢!
      • 不客气。我只是遇到了这个问题,所以认为它可能对其他人有帮助
      【解决方案3】:

      找到了解决办法。

      碰巧你需要让可样式化的名称与你的类名相同。

      <resources>
          <declare-styleable name="MenuListItem">
              <attr name="my_custom_attribute" format="integer" />
          </declare-styleable>
      </resources>
      

      在这种情况下,很多关于这个主题的帖子都是错误的,因为我可以看到很多样式名称完全不同。

      希望这可以阻止其他人落入这个陷阱。

      干杯

      【讨论】:

      • 在网上搜索了这个简单问题的答案后,这对我帮助很大。谢谢!
      猜你喜欢
      • 2014-06-16
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多