0

Ich entwickle App, wo Sie Ihre Schule Zeitplan (mit Auth etc.) füllen können In Dialog Klasse ich neue Spinner + TextView erstellen. Wie weisen Sie sie in MainActivity zu und wie legen Sie die Inhaltsansicht am Ende der Liste fest? Ich weiß, dass es LayoutParams gibt, aber ich kann es nicht verstehen.Handle programmatically erstellt Spinner und lokalisieren sie nach unten

Auch wäre es toll, wenn Sie mir helfen können, Elemente aus dem Dialogfenster nach der Auswahl zu entfernen.

Code:

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); 
} 

}

Dialog XML-Fragment:

<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

Verwenden Sie eine xml für Container etc? Ich finde es sehr nützlich, ein Steuerelement aus einer XML-Datei aufzublasen. Dann mäßige danach. Zeigen Sie uns das Fragment XML und Aktivität XML –

+0

hinzugefügt dies. Nicht sicher, ob es hilft –

+0

Was passiert mit hinzugefügtem Inhalt? Fügen Sie jedes Mal Textview und Spinner hinzu? –

Antwort

0

versuchen, dies für Spinner Position:

super.getActivity().addContentView(textView, lp); 

lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
super.getActivity().addContentView(spinner, lp); 
+0

nicht finden, nach dem Klicken auf Schaltfläche Textansicht und Spinner kollidieren miteinander. –

Verwandte Themen