在 xml 文件中实例化内部类(Preference)
当您想要访问某个 layout.xml
文件中的自定义视图时,您有两个选择:
- 该视图位于它自己的类中。然后你做
- 该视图是一个内部类:
现在我想在
中做同样的事情。 第一种方法效果很好,但我想把所有将自定义 Preference
类放在我的 PreferenceActivity 中。我尝试了
(也用 '.' 而不是 '$')以及
有人有想法吗?
When you want to access a custom view in some layout.xml
file, you have two options:
- 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= ... />
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.
Does anyone have an idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在处理
Views
膨胀时,LayoutInflater
会查找“视图” -> “class”情况:Preference 的
PreferenceInflater
没有,所以这适用于“class”情况。它在其
createItem()
方法中使用反射,这可能就是第一种情况适合您的原因。When dealing with
Views
inflating,LayoutInflater
looks for a "view" -> "class" case:Preference's
PreferenceInflater
doesn't, so that is for the "class" case.It uses reflection in its
createItem()
method and that's probably why the first case works for you.