我正在使用Eclipse开发一个Android应用程序,我注意到GetView()方法的创建非常耗时,我觉得应该自动化,特别是当一个典型的应用程序包含许多要膨胀的布局时。 我并不是指实现监听器,而是简单地从新膨胀的布局中获取带有id的每个元素的引用。
例如,当覆盖适配器的getView()方法时,前几行中的一行总是读取类似于以下内容:
View rootView = inflater.inflate(R.layout.fragmenta, container, false);
EditText edittext = (EditText) rootView.findViewById(R.id.input_user_name);
Button button = (Button) rootView.findViewById(R.id.show_user_name);
TextView textview = (TextView) rootView.findViewById(R.id.display_user_name);
对于每次布局膨胀都会重复这种做法并让我懒惰,但我正在寻找一种通过Eclipse插件自动执行此过程的方法。插件获取布局文件,使用android:id属性提取所有元素然后生成上面的java代码会很好。
该插件将采用文件'fragmenta.xml':
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/input_user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/show_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show user name" />
<TextView
android:id="@+id/display_user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
为getView()方法生成一些启动代码:
View rootView = inflater.inflate(R.layout.fragmenta, container, false);
EditText edittext = (EditText) rootView.findViewById(R.id.input_user_name);
Button button = (Button) rootView.findViewById(R.id.show_user_name);
TextView textview = (TextView) rootView.findViewById(R.id.display_user_name);
我确信此插件对其他视图创建非常有用,例如:活动 - &gt; onCreate(),Fragment - &gt; onCreateView(),Adapter - &gt; getView()。
这样的插件是否存在?
感谢您提供任何信息
答案 0 :(得分:2)
最常用的库是:
Butterknife:https://github.com/JakeWharton/butterknife
Android注释:http://androidannotations.org/