我解決了這個使用一個自定義的Eclipse的生成器,爲每個佈局文件引用生成的類,因爲:
- 它是類型安全和易於使用的
- RoboGuice和所有其他基於反射的API在Android上非常慢。
在我看來,這是解決這個問題的最乾淨和最高性能的方式。
見我要點這裏的建設者:https://gist.github.com/fab1an/10533872
佈局:的test.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text1" />
<ScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="350px"
android:layout_below="@id/text2"
android:layout_marginTop="50px" />
</merge>
用法:TestView.java
public final class TestView extends RelativeLayout {
//~ Constructors ---------------------------------------------------------------------------------------------------
private final ViewRef_test v;
public TestView(final Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.test, this, true);
this.v = ViewRef_test.create(this);
this.v.text1.setText();
this.v.scroll.doSomething();
}
}
生成的文件(以):ViewRef_test.java
package org.somecompany.somepackage;
import android.view.*;
import android.widget.*;
import java.lang.String;
@SuppressWarnings("unused")
public final class ViewRef_test {
public final TextView text1;
public final TextView text2;
public final ScrollView scroll;
private ViewRef_test(View root) {
this.text1 = (TextView) root.findViewById(R.id.text1);
this.text2 = (TextView) root.findViewById(R.id.text2);
this.scroll = (ScrollView) root.findViewById(R.id.scroll);
}
public static ViewRef_test create(View root) {
return new ViewRef_test(root);
}
}
什麼是您的JDK和Android版本? – 2012-07-11 13:40:38
Mine是Android 2.2和JDK 1.6。 – Quv 2012-07-11 13:51:56