0
我正在開發應用程序,您可以在其中填充您的學校shedule(使用身份驗證等) 在對話框類中創建新的Spinner + TextView。如何在MainActivity中分配它們以及如何將內容視圖設置到列表的底部?我知道有一些LayoutParams的東西,但我無法理解它。處理programmaticaly創建的spinners並將其定位到底部
如果您選擇後可以幫助我從對話框窗口中刪除項目,那也是非常棒的。
代碼:
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>
你使用的容器等任何XML?我發現將一個控件從一個xml文件中膨脹是非常有用的。然後在此之後緩和。向我們展示片段XML和活動XML –
添加了此內容。不確定它是否有幫助 –
添加內容時發生了什麼?每次都要添加textview和spinner? –