Android 检索属性时遇到问题
更新-请看最后的决议,我现在需要的是一个解释。 尽管搜索了一遍,我还是找不到同样的问题 我创建了一个自定义Viewpager,并在attrs.xml中定义了一个自定义属性 在视图寻呼机构造函数中,当我尝试检索属性时,我无法获得有意义的值。我试着用Android 检索属性时遇到问题,android,android-layout,android-viewpager,Android,Android Layout,Android Viewpager,更新-请看最后的决议,我现在需要的是一个解释。 尽管搜索了一遍,我还是找不到同样的问题 我创建了一个自定义Viewpager,并在attrs.xml中定义了一个自定义属性 在视图寻呼机构造函数中,当我尝试检索属性时,我无法获得有意义的值。我试着用 abstract int getAttributeIntValue(String namespace, String attribute, int defaultValue) 返回“属性”的整数值 public MyViewPager(Con
abstract int getAttributeIntValue(String namespace, String attribute, int defaultValue)
返回“属性”的整数值
public MyViewPager(Context context, AttributeSet attrs) {
Log.d(Constant.TAG, "ViewPagerType:" + attrs.getAttributeUnsignedIntValue("http://schemas.android.com/apk/res-auto","view_pager_type",9));
Log.d(Constant.TAG, "ViewPagerType:" + attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto","view_pager_type",9));
Log.d(Constant.TAG, "ViewPagerType:" + attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","view_pager_type"));
Log.d(Constant.TAG, "ViewPagerType:" + attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","garbage"));
如您所见,attrs.getAttributeValue创建了一个字符串,但它对我来说没有意义
这是日志语句的输出-9是默认值如果找不到,我不理解@213数字是什么
ViewPagerType:9
ViewPagerType:9
ViewPagerType:@2131230723
ViewPagerType:null
这是我的attrs.xml
<resources>
<declare-styleable name="AutoResizeTextView">
<attr name="ttf_name" format="string"/>
</declare-styleable>
<declare-styleable name="ViewPagerType">
<attr name="view_pager_type" format="integer"/>
</declare-styleable>
</resources>
下面的作品,但正如我上面所说,请解释给我。传递给构造函数的属性是什么
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.rh.ellierides.MyViewPager xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textPager"
app:view_pager_type="@integer/text_viewpager"
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="24"
tools:context=".Main">
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="text_viewpager">1</integer>
<integer name="image_viewpager">2</integer>
</resources>
TypedArray a = null;
try {
a = getContext().obtainStyledAttributes(attrs, R.styleable.ViewPagerType);
int viewpagerType = a.getInt(R.styleable.ViewPagerType_view_pager_type, 9);
Log.d(Constant.TAG, "viewpagerType:" + viewpagerType);
} finally {
if (a != null) {
a.recycle(); // ensure this is always called
}
}
TypedArray a = null;
try {
a = getContext().obtainStyledAttributes(attrs, R.styleable.ViewPagerType);
int viewpagerType = a.getInt(R.styleable.ViewPagerType_view_pager_type, 9);
Log.d(Constant.TAG, "viewpagerType:" + viewpagerType);
} finally {
if (a != null) {
a.recycle(); // ensure this is always called
}
}