【问题标题】:Get resource id of attribute of view获取视图属性的资源id
【发布时间】:2012-10-09 20:28:08
【问题描述】:

我正在创建一个自定义列表视图实现。 这是我在 xml 中声明的视图的一个实例:

<CustomListView android:id="@+id/list" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:listSelector="@drawable/listview_background">
</CustomListView>

是否可以从我的构造函数中获取 'android:listSelector' 资源或任何其他属性的 int 值?

public CustomListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.listSelectorId = ???
}

我已经尝试过了,但它没有返回正确的资源 ID:

public CustomListView(Context context, AttributeSet attrs) {
    TypedArray a = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.selectableItemBackground});
    this.selectorResourceId = a.getResourceId(0, 0);
}

【问题讨论】:

  • 为什么是 android.R.attr.selectableItemBackground 而不是 android.R.attr.listSelector ?
  • 改成android.R.attr.listSelector,没区别
  • 你试过 attr.getAttributeResourceValue("android", "listSelector", -1) 吗?

标签: android android-widget android-xml


【解决方案1】:

attr.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "listSelector", -1) 成功了 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多