Java 获取StyledAttributes的工作方式是错误的
我正在尝试创建自己的键盘。使用Android键盘不足以完成我的任务,所以我决定直接从View.class创建继承器类。作为基础,我决定使用Keyboard.class的代码,然后开始一个接一个地进行更改 我甚至在编译那个类时遇到了很多问题,使用了一些反射和黑客来获取私有字段。我创建了文件属性,以便能够使用键盘的属性和标记。最后,类被编译并知道它崩溃了,因为参数(应该是膨胀的)是空的 我发现的是方法context.getainstyledattributes(attrs,R.styleable.MyKeyboardView);返回错误的结果和a.getIndexCount();之后返回0 这是我的attr.xml文件:Java 获取StyledAttributes的工作方式是错误的,java,android,typedarray,Java,Android,Typedarray,我正在尝试创建自己的键盘。使用Android键盘不足以完成我的任务,所以我决定直接从View.class创建继承器类。作为基础,我决定使用Keyboard.class的代码,然后开始一个接一个地进行更改 我甚至在编译那个类时遇到了很多问题,使用了一些反射和黑客来获取私有字段。我创建了文件属性,以便能够使用键盘的属性和标记。最后,类被编译并知道它崩溃了,因为参数(应该是膨胀的)是空的 我发现的是方法context.getainstyledattributes(attrs,R.styleable.M
<resources>
<declare-styleable name="MyKeyboardView">
<attr name="keyBackground" format="reference" />
<attr name="keyTextSize" format="dimension" />
<attr name="labelTextSize" format="dimension" />
<attr name="state_long_pressable" format="boolean" />
<attr name="keyTextColor" format="color" />
<attr name="keyPreviewLayout" format="reference" />
<attr name="keyPreviewOffset" format="dimension" />
<attr name="keyPreviewHeight" format="dimension" />
<attr name="verticalCorrection" format="dimension" />
<attr name="shadowColor" format="color" />
<attr name="shadowRadius" format="float" />
<attr name="backgroundDimAmount" format="float" />
<attr name="popupLayout" format="reference" />
</declare-styleable>
以下是我在布局中使用键盘的方式:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="240dp"
android:layout_gravity="bottom">
<com.test.features.keyboard.MyKeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="2.5dip"
android:keyPreviewHeight="@dimen/key_height"
android:shadowRadius="0"
android:keyPreviewLayout="@layout/keyboard_key_preview"
android:keyPreviewOffset="0dp"
android:keyBackground="@drawable/keyboard_key_background"
android:keyTextColor="@color/keyboard_key_color"
android:background="@color/primary"
android:popupLayout="@layout/keyboard_popup_layout"/>
尝试这样使用,length()返回11,但switch case几乎找不到匹配项。如果找到,则使用默认值:
int n = a.length();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.MyKeyboardView_keyBackground:
mKeyBackground = a.getDrawable(attr);
break;
case R.styleable.MyKeyboardView_verticalCorrection:
mVerticalCorrection = a.getDimensionPixelOffset(attr, 0);
break;
case R.styleable.MyKeyboardView_keyPreviewLayout:
previewLayout = a.getResourceId(attr, 0);
break;
case R.styleable.MyKeyboardView_keyPreviewOffset:
mPreviewOffset = a.getDimensionPixelOffset(attr, 0);
break;
case R.styleable.MyKeyboardView_keyPreviewHeight:
mPreviewHeight = a.getDimensionPixelSize(attr, 80);
break;
case R.styleable.MyKeyboardView_keyTextSize:
mKeyTextSize = a.getDimensionPixelSize(attr, 18);
break;
case R.styleable.MyKeyboardView_keyTextColor:
mKeyTextColor = a.getColor(attr, 0xFF000000);
break;
case R.styleable.MyKeyboardView_labelTextSize:
mLabelTextSize = a.getDimensionPixelSize(attr, 14);
break;
case R.styleable.MyKeyboardView_popupLayout:
mPopupLayout = a.getResourceId(attr, 0);
break;
case R.styleable.MyKeyboardView_shadowColor:
mShadowColor = a.getColor(attr, 0);
break;
case R.styleable.MyKeyboardView_shadowRadius:
mShadowRadius = a.getFloat(attr, 0f);
break;
}
}
int n=a.length();
对于(int i=0;i
您的自定义视图
属性将位于应用程序的名称空间中,而不是系统名称空间中,因此当它们带有系统名称空间前缀时,在给定的属性集
中找不到它们
添加xmlns:app=”http://schemas.android.com/apk/res-auto“
命名空间声明可以直接在
标记中,也可以在祖先标记中,并将所有属性上的前缀更改为app
请注意,前缀可以是您喜欢的任何有效名称,尽管
app
可能是最常见的。您如何实例化MyKeyboardView
?你是在夸大布局吗?如果是这样的话,您是否真的在正在膨胀的布局中的
上设置了任何自定义属性?@MikeM。为这些属性添加了代码,这些属性将位于应用程序的命名空间中,而不是系统命名空间中。添加xmlns:app=”http://schemas.android.com/apk/res-auto“
,并将所有属性上的android
前缀更改为app
。(让我知道安卓工作室是否有抱怨。)@MikeM。非常感谢你!现在我还有很多其他问题,但至少我的键盘充气了,不再崩溃了。创建一个答案,以便我将其检查为正确答案)
int n = a.length();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.MyKeyboardView_keyBackground:
mKeyBackground = a.getDrawable(attr);
break;
case R.styleable.MyKeyboardView_verticalCorrection:
mVerticalCorrection = a.getDimensionPixelOffset(attr, 0);
break;
case R.styleable.MyKeyboardView_keyPreviewLayout:
previewLayout = a.getResourceId(attr, 0);
break;
case R.styleable.MyKeyboardView_keyPreviewOffset:
mPreviewOffset = a.getDimensionPixelOffset(attr, 0);
break;
case R.styleable.MyKeyboardView_keyPreviewHeight:
mPreviewHeight = a.getDimensionPixelSize(attr, 80);
break;
case R.styleable.MyKeyboardView_keyTextSize:
mKeyTextSize = a.getDimensionPixelSize(attr, 18);
break;
case R.styleable.MyKeyboardView_keyTextColor:
mKeyTextColor = a.getColor(attr, 0xFF000000);
break;
case R.styleable.MyKeyboardView_labelTextSize:
mLabelTextSize = a.getDimensionPixelSize(attr, 14);
break;
case R.styleable.MyKeyboardView_popupLayout:
mPopupLayout = a.getResourceId(attr, 0);
break;
case R.styleable.MyKeyboardView_shadowColor:
mShadowColor = a.getColor(attr, 0);
break;
case R.styleable.MyKeyboardView_shadowRadius:
mShadowRadius = a.getFloat(attr, 0f);
break;
}
}