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

15

我创建了一个包含RelativeLayoutEditText的简单自定义视图:

<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中的文本属性的情况下实现这一点?

这很令人困惑,你没有在 XML 代码中使用视图...!你确定你已经发布了最新的代码吗?请查看此答案:https://dev59.com/dnI-5IYBdhLWcg3wEEBV#8090772 它可能会解决你在 XML 中设置值并将其传递到 Java 代码的问题。 - Ajeet Choudhary
你能否为CustomEditText添加你的styleable文件? - tynn
5个回答

40

您可以将android:text添加到您声明的样式中。但请确保不要重新声明它。

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

然后,您可以像处理其他属性一样,使用R.styleable.CustomEditText_android_text的索引从样式中获取该值。

CharSequence text = typedArray.getText(R.styleable.CustomEditText_android_text);

3
似乎不能处理像“android:textColor”这样的混合大小写? - coyer
1
这应该没有影响。我从来没有遇到过问题。 - tynn

1
您可以像这样从自定义视图中访问attrs:

您可以像这样从自定义视图中访问attrs:

class CustomView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0,
) : ConstraintLayout(context, attrs, defStyleAttr) {

    private val text = attrs?.getAttributeValue(
        "http://schemas.android.com/apk/res/android",
        "text"
    )

    private val scaleType = ImageView.ScaleType.values().getOrNull(
        attrs?.getAttributeIntValue(
            "http://schemas.android.com/apk/res/android",
            "scaleType",
            -1
        ) ?: -1
    )

    private val adjustViewBounds = attrs?.getAttributeBooleanValue(
        "http://schemas.android.com/apk/res/android",
        "adjustViewBounds",
        false
    )

    private val verticalGapRes = attrs?.getAttributeResourceValue(
        "http://schemas.android.com/apk/res-auto",
        "flow_verticalGap",
        R.dimen.default_res
    )
}

在XML中:
<com.example.testcustomview.CustomView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:text="Hello World!"
    app:flow_verticalGap="@dimen/default_res"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

0

我认为你无法这样做。 在xml中为你的edittext添加一个ID,如果你想检索editText的值,只需使用:

edittext.getText().toString()

我想在XML中使用android:text="some text",并在我的自定义视图类中检索它,就像自定义属性一样。 - hosseinAmini
请发布您的自定义视图类。 - Dany Pop
我添加了我的自定义视图类。 - hosseinAmini

0

我认为这是不可能的,因为你的自定义视图扩展了 RelativeLayout,而它没有这个属性。如果你直接从 EditText 或 TextView 扩展,你可以访问 android:text 或其他属性。


-1

您可以使用类似的布局来构建此内容

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:id="@+id/edittext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="My Custom View" />
</RelativeLayout>

你的自定义视图类会像这样

public class CustomEditText extends RelativeLayout {
    LayoutInflater mInflater;

    public CustomView(Context context) {
        super(context);
        mInflater = LayoutInflater.from(context);
        init();

    }

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mInflater = LayoutInflater.from(context);
        init();
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mInflater = LayoutInflater.from(context);
        init();
    }

    // Here you can access your edittext text
    public void init() {
        View v = mInflater.inflate(R.layout.custom_view, this, true);
        EditText et = (TextView) v.findViewById(R.id.edittext);
        et.setText(" Custom RelativeLayout");
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接