简体   繁体   中英

Dynamic linearlayout in DialogFragment for Android (Java)

I set the view to a layout with scrollview and linearlayout, and set a button added in the title to add new row in the linearlayout, but new row does not appear, any tips? I plan to use this DialogFragment as a BLE search, is this a good way to solve it?

public class BLE_Devices_Fragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout to use as dialog or embedded fragment
        return inflater.inflate(R.layout.fragment_ble_devices, container, false);
    }

    @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();


        LinearLayout layout = inflater.inflate(R.layout.fragment_ble_devices, null).findViewById(R.id.deviceList);

        View titleView = inflater.inflate(R.layout.bluetoothscan_title, null, false);
        ImageButton syncBLEButton = titleView.findViewById(R.id.syncBLEButton);
        syncBLEButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                layout.addView(new Button(layout.getContext()));
            }
        });

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        builder.setCustomTitle(inflater.inflate(R.layout.bluetoothscan_title, null)).create();
        builder.setView(inflater.inflate(R.layout.fragment_ble_devices, null));
        // Create the AlertDialog object and return it
        return builder.create();
    }


}

Replace

builder.setView(inflater.inflate(R.layout.fragment_ble_devices, null));

with

builder.setView(layout);

解决:必须设置视图,而不是创建新视图。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM