【发布时间】: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