我正在尝试扩展 LinearLayout 以用作复合组件.我已按照本教程进行操作:链接
I'm trying to extend a LinearLayout to use as a composite component. I have followed this tutorial: link
这看起来相当简单.但是,当我尝试为我的组件充气时,它总是失败.我从 logCat 得到的错误是找不到我的班级.对我来说似乎很奇怪,据我所知,android 在它自己的组件中搜索我的组件(我不确定这里的正确措辞,请参见下文).
Which seems fairly straight forward. However when I try to inflate my component it fails all the time. The error I get from logCat is that my class can't be found. It seems strange to me that as far as I can see android searches for my component among it's own components (I'm unsure about the correct wording here, please se below).
我写的代码如下:
main(当前活动):
main (the current activity):
//just the ordinary autogenerated eclipse code...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
我的扩展线性布局:
public class DialPadView extends LinearLayout {
public DialPadView(Context ctx, AttributeSet attr) {
super(ctx, attr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
((Activity)getContext()).getLayoutInflater().inflate(R.layout.dialpadview, this);
}
}
我的 main.xml 布局文件
My main.xml layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<whalenut.testinflate.DialPadView
android:id="@+id/mydialpad"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/></LinearLayout>
最后是与我的复合组件一起使用的 dialpadview.xml 文件:
and finally my dialpadview.xml file to use with my composite component:
<?xml version="1.0" encoding="utf-8"?>
<DialPadView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test"/>
</DialPadView>
当然,我所有的类文件都驻留在包 whalenut.testinflate
ofcourse all my class files resides in the package whalenut.testinflate
logCat 是这样说的:
logCat says this:
java.lang.RuntimeException:无法启动活动 ComponentInfo{whalenut.testinflate/whalenut.testinflate.TestInflateActivity}:android.view.InflateException:二进制 XML 文件第 2 行:膨胀类 DialPadView 时出错
java.lang.RuntimeException: Unable to start activity ComponentInfo{whalenut.testinflate/whalenut.testinflate.TestInflateActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class DialPadView
...
原因:android.view.InflateException: Binary XML file line #2: Error inflating class DialPadView
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class DialPadView
...
原因:java.lang.ClassNotFoundException: android.view.DialPadView in loader dalvik.system.PathClassLoader[/data/app/whalenut.testinflate-2.apk]
Caused by: java.lang.ClassNotFoundException: android.view.DialPadView in loader dalvik.system.PathClassLoader[/data/app/whalenut.testinflate-2.apk]
当我在 xml-文件,是不是因为一开始就没有找到它?
Why is Dalvik(?) searching for my class in the package android.view instead of whalenut.testinflate when I have given the fully qualified classname in the xml-file, is it because it doesn't find it there to begin with?
简而言之,为什么我不能,或者更确切地说,我如何在 android 中扩展扩展布局?
In short why can't I, or rather how do I inflate a extended Layout in android?
你需要改变你的 DialPadView 如下:
You need to change your DialPadView as follows:
<merge android:class="com.packageName.UIClassName"
android:id="@+id/DialView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test"/>
</merge>
然后在你的构造函数中调用它:
Then call this in your constructor:
super(context, attrs, defStyle);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.DialView, this, true);
然后您可以在主要活动中使用以下内容:
You can then use the following in your main activity:
DialView myDialView = new DialView(context);
mainView.addView(myDialView);
我希望这会有所帮助!
这篇关于扩展扩展 Layoutclass 的复合组件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!