如何在自定义视图中获取android默认属性

如何在自定义视图中获取android默认属性,第1张

概述我创建了一个包含RelativeLayout和EditText的简单自定义视图:<RelativeLayoutxmlns:android="http://schemas.android.com/apkes/android"android:layout_width="match_parent"android:layout_height="wrap_content"><EditTex

我创建了一个包含relativeLayout和EditText的简单自定义视图

<relativeLayout    xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content">    <EditText        androID:ID="@+ID/edt_content"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"/></relativeLayout>

我还在res / values / attrs.xml中添加了一些自定义属性,并在自定义视图构造函数中检索这些属性,一切正常.

现在,我想在自定义视图中检索EditText默认属性,例如我想在我的自定义视图中获取androID:text属性.

我的自定义视图类(简化):

public class CustomEditText extends relativeLayout {    private int panCount;    public CustomEditText(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray typedArray = context.gettheme().obtainStyledAttributes(attrs,            R.styleable.CustomEditText, 0, 0);        try {            this.panCount = typedArray.getInt(R.styleable.CustomEditText_panCount, 16);        } finally {            typedArray.recycle();        }    }}

如果不在res / values / attrs.xml中重新声明文本属性,我该怎么做?

解决方法:

您可以将androID:text添加到声明的syleable中.但一定不要重新声明它.

<declare-styleable name="CustomEditText">    <attr name="androID:text" /></declare-styleable>

然后从样式中获取此值,就像使用R.styleable.CustomEditText_androID_text索引的任何其他属性一样.

CharSequence text = typedArray.getText(R.styleable.CustomEditText_androID_text);
总结

以上是内存溢出为你收集整理的如何在自定义视图中获取android默认属性全部内容,希望文章能够帮你解决如何在自定义视图中获取android默认属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115827.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存