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> 
+0

你使用的容器等任何XML?我发现将一个控件从一个xml文件中膨胀是非常有用的。然后在此之后缓和。向我们展示片段XML和活动XML –

+0

添加了此内容。不确定它是否有帮助 –

+0

添加内容时发生了什么?每次都要添加textview和spinner? –

回答

0

尝试一下本作微调的位置:

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

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

no找到这个对象,点击按钮后文本视图和微调器相互碰撞。 –