【问题标题】:Get standard layout attributes获取标准布局属性
【发布时间】:2014-03-24 15:14:46
【问题描述】:

我扩展了 ImageView 类并添加了一些自定义参数。我使用Context.getTheme().obtainStyledAttributes()方法成功地从我的代码中获取了这些自定义参数。

我需要的是访问ImageView对象的标准参数,例如android:srcandroid:background。我知道它存在类 android.R.styleable.* ,我可以使用它来获取这些参数,但是该类已被弃用(并且不再可见)。我可以做些什么来访问这些 android 参数?

【问题讨论】:

    标签: android xml layout parameters


    【解决方案1】:

    虽然我不确定如何从 TypedArray 中提取父值,但您可以使用适当的 getter 访问它们,例如:

    public class MyImageView extends ImageView {
    
        public MyImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    
            final TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.whatever);
            try {
                // get custom attributes here
            } finally {
                array.recycle();
            }
    
            // parent attributes
            final Drawable background = getBackground();
            final Drawable src = getDrawable();
    
            // etc.
        }
    
    }
    

    这不是您想要的,但它可能会有所帮助。

    【讨论】:

    • 这是获取自定义属性的代码。我无法检索标准的...
    • 我不太清楚你所说的 standard 是什么意思。 '父属性'注释后的代码不是你想要的吗?
    • 我正在尝试获取android属性,如android:src、android:background、android:layout_width等...
    猜你喜欢
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多