我正在开发app,你可以在那里填补你的学校(使用auth等) 在对话框类中,我创建了新的Spinner + TextView。如何在MainActivity中分配它们以及如何将内容视图设置到列表的底部?我知道有一些LayoutParams的东西,但我无法理解。
如果您可以帮助我在选择后从Dialog窗口中删除项目,那就太棒了。
代码:
public class OtherSubjectsDialog extends DialogFragment implements View.OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().setTitle("Title!");
View v = inflater.inflate(R.layout.dialog_layout, null);
v.findViewById(R.id.btn_inf).setOnClickListener(this);
v.findViewById(R.id.btn_phys).setOnClickListener(this);
v.findViewById(R.id.btn_soc).setOnClickListener(this);
v.findViewById(R.id.btn_chem).setOnClickListener(this);
v.findViewById(R.id.btn_biol).setOnClickListener(this);
return v;
}
@TargetApi(Build.VERSION_CODES.M)
public void onClick(View v) {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.alignWithParent = true;
Spinner spinner = new Spinner(super.getContext());
ArrayAdapter<?> adapter = null;
TextView textView = new TextView(super.getContext());
switch (v.getId()) {
case R.id.btn_inf:
adapter = ArrayAdapter.createFromResource(super.getContext(), R.array.inf_groups_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
textView.setText("Группа по информатике:");
break;
case R.id.btn_phys:
adapter = ArrayAdapter.createFromResource(super.getContext(), R.array.phys_groups_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
textView.setText("Группа по физике:");
break;
case R.id.btn_soc:
adapter = ArrayAdapter.createFromResource(super.getContext(), R.array.soc_groups_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
textView.setText("Группа по обществознанию:");
break;
case R.id.btn_chem:
adapter = ArrayAdapter.createFromResource(super.getContext(), R.array.chem_groups_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
textView.setText("Группа по химии:");
break;
case R.id.btn_biol:
adapter = ArrayAdapter.createFromResource(super.getContext(), R.array.biol_groups_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
textView.setText("Группа по биологии:");
break;
}
spinner.setAdapter(adapter);
super.getActivity().addContentView(textView, lp);
super.getActivity().addContentView(spinner, lp);
dismiss();
}
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
}
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
}
}
对话框片段xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:text="Выберите дополнительный предмет:"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/inf"
android:id="@+id/btn_inf" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phys"
android:id="@+id/btn_phys" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/soc"
android:id="@+id/btn_soc" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chem"
android:id="@+id/btn_chem" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/biol"
android:id="@+id/btn_biol" />
</TableLayout>
</LinearLayout>
答案 0 :(得分:0)
尝试使用旋转器位置:
super.getActivity().addContentView(textView, lp);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
super.getActivity().addContentView(spinner, lp);