4

我想在Dialog中动态添加我的自定义项目,例如Spinner,但它没有显示出来。我想微调器项目没有被. 我没有收到任何错误,但项目没有动态显示。findviewbyid()

请提出一种方法来做到这一点。

这是我的课程和 xml 文件:

public class AlertDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    View view = inflater.inflate(R.layout.dialog, null);

    Spinner spin;
    spin = (Spinner)view.findViewById(R.id.spinner1);

    List<String> list = new ArrayList<String>();
    list.add("Material 1");
    list.add("Material 2");
    list.add("Material 3");
    list.add("Material 4");
    list.add("Material 5");
    list.add("Material 6");

    //Second List
    List<String> list2 = new ArrayList<String>();
    list2.add("Mat 7");
    list2.add("Mat 8");
    list2.add("Mat 9");

    //Combined List
    List<String> listCombine = new ArrayList<String>();
    listCombine.addAll(list);
    listCombine.addAll(list2);

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, listCombine); 

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spin.setAdapter(dataAdapter);

    builder
    .setTitle("Title")
    .setView(inflater.inflate(R.layout.dialog, null))
           .setPositiveButton("Add", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // FIRE ZE MISSILES!
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });
    // Create the AlertDialog object and return it
    return builder.create();
}

}

我的xml文件是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<LinearLayout android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="20dp">


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Materials"
    android:layout_marginRight="10dp"
    android:textSize="20sp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="0dip"
    android:layout_height="35dp"
    android:layout_weight="1" 
    android:layout_marginRight="10dp"
    android:background="#FFB2B2"      
     />

</LinearLayout>


<LinearLayout android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="20dp">


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Weight"
    android:layout_marginRight="35dp"
    android:textSize="20sp"
    android:textAppearance="?android:attr/textAppearanceMedium" />


 <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="55"
    android:textSize="20sp"
    android:textAppearance="?android:attr/textAppearanceMedium" />


</LinearLayout>

4

1 回答 1

4

而不是在这里膨胀第二个布局:

builder
    .setTitle("Title")
    .setView(inflater.inflate(R.layout.dialog, null)) // Second layout!

只需使用您已经膨胀和设置的布局:

builder
    .setTitle("Title")
    .setView(view) // Existing layout
于 2013-02-03T20:32:03.150 回答