gpt4 book ai didi

Android自定义属性不显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:45 24 4
gpt4 key购买 nike

我看过很多帖子,人们想知道如何为自定义组件获取自定义属性,但这不是我的问题。我创建了一个自定义组件并尝试添加属性,但是当我在我的 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”添加值,但它们不可用。唯一显示的是与填充相关的内容,如下所示。

enter image description here

有什么想法吗?

最佳答案

找到解决方案。

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

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

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

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

干杯

关于Android自定义属性不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34292345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com