问题描述
当您要访问某些layout.xml
文件中的自定义视图时,有两种选择:
When you want to access a custom view in some layout.xml
file, you have two options:
- 该视图位于其自己的类中.然后,您执行
<package.name.MyView android:layout_width= ... />
- 该视图是一个内部类:
<view class="package.name.OuterClass$MyView" android:layout_width= ... />
- The view is in it's own class. Then you do
<package.name.MyView android:layout_width= ... />
- The view is an inner class:
<view class="package.name.OuterClass$MyView" android:layout_width= ... />
现在,我想在<PreferenceScreen>
中做同样的事情.第一种方法很好用,但是我想将所有自定义的Preference
类放到我的PreferenceActivity中.我尝试了<Preference class="package.name.OuterClass$MyPreference" ... />
(也使用'.'而不是'$')以及<package.name.OuterClass.MyPreference ... />
,但是都失败了.
Now I want to do the same thing inside a <PreferenceScreen>
. The first way works well, but I would like to put all the custom Preference
classes together in my PreferenceActivity. I tried <Preference class="package.name.OuterClass$MyPreference" ... />
(also with '.' instead of '$') as well as <package.name.OuterClass.MyPreference ... />
, but both failed.
有人有主意吗?
推荐答案
在处理Views
膨胀时,LayoutInflater
查找视图"->类"情况:
When dealing with Views
inflating, LayoutInflater
looks for a "view" -> "class" case:
View createViewFromTag(View parent, String name, AttributeSet attrs) {
if (name.equals("view")) {
name = attrs.getAttributeValue(null, "class");
} ...
首选项的PreferenceInflater
没有,因此适用于类"的情况.
Preference's PreferenceInflater
doesn't, so that is for the "class" case.
它在其createItem()
方法中使用了反射,这可能就是第一种情况对您有用的原因.
It uses reflection in its createItem()
method and that's probably why the first case works for you.
这篇关于实例化xml文件中的内部类(首选项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!