自定义控件属性获取不了的问题
初心勿忘 2016-06-24 03:50:57 如题所示,自定义控件的属性代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="test">
<attr name="text" format="string" />
<attr name="testAttr" format="string" />
</declare-styleable>
</resources>
使用过程中,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xky="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.administrator.atterbutesettest.MyTextView
android:id="@+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xky:text="hello"
/>
</RelativeLayout>
然后,
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test);
String text = ta.getString(R.styleable.test_testAttr);
Log.e(TAG, "text = " + text );
打印的信息却为空,请问为什么呢