1
我有一个控件布局:Android。添加controlls programmaticaly
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/contact_phones_layout_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/contact_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:inputType="phone" />
<Spinner
android:id="@+id/contact_phone_type"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
而且我想它膨胀到的片段另一个布局。这些控件的数量取决于数组中的值。数组包含控件对象。所以每次onCreateView上升我从阵列填补了布局:
private void addLine(PhoneLine line) {
LinearLayout layout = (LinearLayout) mLayout.findViewById(R.id.contact_phones_layout);
View view = line.getParent();
((ViewGroup) view.getParent()).removeView(view);
layout.addView(view, layout.getChildCount() - 1);
setButtonVisible(false);
}
如果线是空控件创建这样:
private void addLine() {
LinearLayout layout = (LinearLayout) mLayout.findViewById(R.id.contact_phones_layout);
View view = inflater.inflate(R.layout.contact_phone_line, layout, false);
EditText phoneEditText = (EditText) view.findViewById(R.id.contact_phone);
Spinner phoneType = (Spinner) view.findViewById(R.id.contact_phone_type);
phoneLines.add(new PhoneLine(phoneEditText, phoneType, view));
layout.addView(view, layout.getChildCount() - 1);
}
但在那之后我在所有的EditText /微调控件得到相同的值,值等于数组中的最后一个元素。什么可能是错误的?可能有更纯粹的方式来动态添加控件吗?
尝试'ListView'也许? – aragaer 2013-04-08 05:56:11
布局上的所有控件都在ScrollView中,所以我无法使用ListView – Shallow 2013-04-08 06:08:20
将您的LinearLayout替换为ListView。并将其保存在ScrollView中。 – aragaer 2013-04-08 06:43:46