Android 自定义属性未显示
Android custom attributes not showing
我看过很多帖子,有人想知道如何获取自定义组件的自定义属性,但这不是我的问题。我创建了一个自定义组件并尝试添加属性,但是当我在我的 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
}
}
这是我自定义的构造函数class。
<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" 添加值,但它们不可用。唯一显示的是与填充相关的内容,如下所示。
大家有什么想法吗?
找到解决方案。
碰巧您的样式名称需要与您的 class 名称相同。
<resources>
<declare-styleable name="MenuListItem">
<attr name="my_custom_attribute" format="integer" />
</declare-styleable>
</resources>
关于这个主题的很多帖子在那种情况下都是错误的,因为我可以看到很多样式名称完全不同。
希望这可以阻止其他人落入这个陷阱。
干杯
我的解决方案是:
在 attr.xml 文件中,您可能已按如下方式声明自定义属性
<declare-styleable name="IDEditText">
<attr name="customFont" format="string"/>
</declare-styleable>
这里declare-styleable
的name字段必须是自定义视图的名称。然后会显示在建议里。
在我的例子中,我的自定义视图名称是 IDEditText
,因此 declare-styleable
name="IDEditText"
虽然这个 post 有点旧,但为了提供信息,我将分享我的案例。
我定义了一个名为 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>
最后,我将自定义 class 命名为 DeviceMaterialCardView
。现在自定义属性与自定义 class 同名。但是我在编译 him_stroke_color
xml 文件时遇到编译错误:
/home/Projeler/PK-BLE-RF/Android/Uygulama/PK0101BLE/app/src/main/res/color/him_stroke_color.xml:6: AAPT: error: attribute com.himtec.himtec_kontrol:state_dragging not found.
我仍然卡住了,不知道如何解决这个问题。
我看过很多帖子,有人想知道如何获取自定义组件的自定义属性,但这不是我的问题。我创建了一个自定义组件并尝试添加属性,但是当我在我的 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
}
}
这是我自定义的构造函数class。
<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" 添加值,但它们不可用。唯一显示的是与填充相关的内容,如下所示。
大家有什么想法吗?
找到解决方案。
碰巧您的样式名称需要与您的 class 名称相同。
<resources>
<declare-styleable name="MenuListItem">
<attr name="my_custom_attribute" format="integer" />
</declare-styleable>
</resources>
关于这个主题的很多帖子在那种情况下都是错误的,因为我可以看到很多样式名称完全不同。
希望这可以阻止其他人落入这个陷阱。
干杯
我的解决方案是: 在 attr.xml 文件中,您可能已按如下方式声明自定义属性
<declare-styleable name="IDEditText">
<attr name="customFont" format="string"/>
</declare-styleable>
这里declare-styleable
的name字段必须是自定义视图的名称。然后会显示在建议里。
在我的例子中,我的自定义视图名称是 IDEditText
,因此 declare-styleable
name="IDEditText"
虽然这个 post 有点旧,但为了提供信息,我将分享我的案例。
我定义了一个名为 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>
最后,我将自定义 class 命名为 DeviceMaterialCardView
。现在自定义属性与自定义 class 同名。但是我在编译 him_stroke_color
xml 文件时遇到编译错误:
/home/Projeler/PK-BLE-RF/Android/Uygulama/PK0101BLE/app/src/main/res/color/him_stroke_color.xml:6: AAPT: error: attribute com.himtec.himtec_kontrol:state_dragging not found.
我仍然卡住了,不知道如何解决这个问题。