动态添加按钮到对话框

时间:2015-06-28 09:47:27

标签: android button

我想在对话框中动态添加可变数量的按钮。因为按钮的数量没有修复,我无法将它们添加到布局文件中。

这是我尝试的方式:

 private void oeffne_dialog ( String[] prediction_array ) {
    //GestureAnyWhere gestureAnyWhere = null;
    // Activity activity = gestureAnyWhere.get_activity ();

    // TODO: bessere Lösung finden, als das Flag setzen zu müssen. Falscher Context
    Dialog dialog = new Dialog ( getApplicationContext () );
    dialog.getWindow ().setType ( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT );
    Log.d ( "dialog", "dialog " + dialog + "|" + HintergrundService.this /*+ "|" + activity*/ );
    dialog.setContentView ( R.layout.multiple_predictions_layout );

    dialog.setTitle ( "Bitte die zu startende Anwendung auswählen" );

    // Button button_id = (Button)dialog.findViewById ( R.id.button_ID );

    Button button;

    for ( int i = 0 ; i < prediction_array.length ; i++ ) {

        /*
        button_id = new Button ( getApplicationContext () );
        button_id.setText ( prediction_array [i] );
        */

        Log.d ( "aufrufe", "aufrufe " + i + prediction_array[ i ] );
        button = new Button ( getApplicationContext () );
        button.setText ( prediction_array[ i ] );
        button.setId ( i );

    }


    dialog.show ();
}

但是使用此代码时,对话框中不会显示任何按钮。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您只需要首先对视图进行充气,然后从该视图中获取要添加子项的ViewGroup。

Dialog dialog = new Dialog ( MainActivity.this );
dialog.getWindow ().setType ( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT );
View view = getLayoutInflater().inflate(R.layout.empty_basket, null);

dialog.setTitle ( "Bitte die zu startende Anwendung auswählen" );

Button button;
LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll);

for ( int i = 0 ; i < 10 ; i++ ) {
    Log.d ( "aufrufe", "aufrufe " + i);
    button = new Button ( MainActivity.this );
    button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    button.setText (String.valueOf(i));
    button.setId (i);
    ll.addView(button);
}

dialog.setContentView (view);
dialog.show ();

不要忘记添加权限TYPE_SYSTEM_ALERT

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

答案 1 :(得分:0)

这是一个自定义对话框,具有自定义布局和一个取消和保存按钮。

您还可以在设备屏幕(底部)上设置重力并定义对话框的宽度和高度。

private void showDialog(final String scanContent, final String currentTime, final String currentDate) { LayoutInflater linf = LayoutInflater.from(this); final View inflator = linf.inflate(R.layout.dialog_barcode_result_dialog, null);

final Dialog dialog = new Dialog(this, android.R.style.Theme_DeviceDefault_Light_Dialog);

// Setting dialogview
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);

dialog.getWindow().setLayout(375, 350);

window.setLayout(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);
dialog.setTitle(null);
dialog.setContentView(R.layout.dialog_barcode_result_dialog);

dialog.setTitle(getResources().getString(R.string.dialog_title));
dialog.setContentView(inflator);

final Button save = inflator.findViewById(R.id.saveBtn);
final Button cancel = inflator.findViewById(R.id.cancelBtn);
final TextView message = inflator.findViewById(R.id.dialog_message_text);
message.setText(scanContent);
save.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        dialog.cancel();

    }
});
cancel.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        dialog.cancel();

    }
});

dialog.show();

}

对话框布局xml文件为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:minWidth="350dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:textSize="16sp"
                android:layout_marginBottom="10dp"
                android:id="@+id/dialog_message_text"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="right"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/cancelBtn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/cancel" />

                <Button
                    android:id="@+id/saveBtn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/button_save" />
            </LinearLayout>

        </LinearLayout>
    </ScrollView>
</LinearLayout>
相关问题