java以动态和编程方式添加定义的TextInputLayout
我试图添加一个TextInputLayout,其中包含一个基于用户从微调器中选择的数字的EditText。我已经在XML中定义了TextInputLayout属性,并希望简单地根据用户从微调器中选择的数字以编程方式添加它们:
XML:
<FrameLayout
安卓:background="@drawable/image_border"
安卓:layout_width="match_parent"
安卓:layout_height="0dp"
安卓:layout_weight=".525">
<Button
安卓:id="@+id/add_image_button"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="center"
安卓:text="Click to Add Image" />
</FrameLayout>
<LinearLayout
安卓:orientation="vertical"
安卓:padding="4dp"
安卓:layout_width="match_parent"
安卓:layout_height="0dp"
安卓:layout_weight=".475">
<安卓.support.design.widget.TextInputLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
>
<EditText
安卓:id="@+id/create_poll_question_editText"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:imeOptions="actionDone"
安卓:singleLine="true"
安卓:hint="@string/create_poll_question" />
</安卓.support.design.widget.TextInputLayout>
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal">
<TextView
安卓:id="@+id/how_many_answers_textView"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/how_many_answers_text"
安卓:textColor="@color/black"
安卓:textSize="16sp" />
<Spinner
安卓:id="@+id/number_of_answers_spinner"
安卓:layout_width="wrap_content"
安卓:layout_gravity="bottom"
安卓:layout_height="24dp"
安卓:background="@安卓:drawable/btn_dropdown" />
</LinearLayout>
<!--Want to Add Programatically -->
<安卓.support.design.widget.TextInputLayout
安卓:id="@+id/create_poll_answer"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
>
<EditText
安卓:id="@+id/create_poll_answer_editText"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:imeOptions="actionDone"
安卓:singleLine="true"
/>
</安卓.support.design.widget.TextInputLayout>
</LinearLayout>
以下是我当前使用的代码,但它不会根据我已经创建的视图动态添加:
public class YourItemSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
Toast.makeText(getActivity().getApplicationContext(), selected, Toast.LENGTH_SHORT).show();
for (int i = 0; i < Integer.parseInt(selected); i++) {
ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_linearlayout);
EditText editText = new EditText(getActivity());
editText.setHint("Poll Answer");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(layoutParams);
TextInputLayout newAnswer = new TextInputLayout(getActivity());
newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newAnswer.addView(editText, layoutParams);
layout.addView(newAnswer);
}
}
# 1 楼答案
加上
你的根线布局