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

How to get android default attributes in a custom view

我创建了一个简单的自定义视图,其中包含一个 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 属性。

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

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 中为您的 edittext 添加一个 id,如果您想检索 editText 的值,只需使用:

edittext.getText().toString()

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

您可以使用像

这样的布局来构建它
<?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>

您的自定义视图 class 将像这样

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");
    }
}

您可以将 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);

您可以像这样从自定义视图访问属性:

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" />