簡體   English   中英

對話框中按鈕周圍的多余填充

[英]Unwanted padding around buttons in dialog

問題:

不需要的填充

您可以清楚地看到按鈕周圍的填充

編碼:

public void startGameDialog(){

    Context context = GameBoardActivity.this;

    ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.AppBaseTheme);

    AlertDialog.Builder startGameDialog = new AlertDialog.Builder(ctw);
    startGameDialog.setTitle(getResources().getString(R.string.whats_your_name));

    LinearLayout dialogLayout = new LinearLayout(context);

        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        dialogLayout.setOrientation(LinearLayout.VERTICAL);

        final EditText newName = new EditText(context);
        newName.setLayoutParams(params);

        newName.setText("");

        final Button submit = new Button(context);
        submit.setText(getString(R.string.done));
        submit.setLayoutParams(params);

    dialogLayout.addView(newName);
    dialogLayout.addView(submit);

    startGameDialog.setView(dialogLayout);
    final Dialog dialog = startGameDialog.create();

    OnClickListener onClick = new OnClickListener() {

        @Override
        public void onClick(View v) {
            GameBoardActivity.NAME = newName.getText().toString();
            dialog.dismiss();
            setUpPlayers();

        }
    };
    submit.setOnClickListener(onClick);     

    dialog.show();
    dialog.setCancelable(false);        

}

嘗試的解決方案 (均失敗):

  1. 使用builder .create()方法構建到AlertDialog並設置.setView(dialogLayout, 0, 0, 0, 0)

  2. 通過嘗試使用ViewGroup parent = (ViewGroup) dialogLayout.getParent()然后嘗試使用parent.setPadding(0, 0, 0, 0); ViewGroup parent = (ViewGroup) dialogLayout.getParent() parent.setPadding(0, 0, 0, 0);刪除父級的填充parent.setPadding(0, 0, 0, 0); (這返回NullPointerException錯誤,因為您無法為DialogAlertDialog設置填充。

還有其他想法嗎?

提前致謝!

壞家伙

Android中的默認按鈕具有自然填充。 您可以通過更改背景或布局XML上的樣式將其刪除。

將最小高度和最小寬度設置為“ 0”,然后刪除填充

submit.setMinHeight(0);

因此,我想出了另一種解決方案:請記住,如果將來更改SDK中的9個Patch圖像,這可能會破壞事情,但否則,它似乎無法刪除9Patch圖像的填充並與所有API兼容。 這是我更改的代碼:

    //set to compensate for 9 patch padding on button
    dialogLayout.setPadding(-5, 5, -5, -8);
    //set to compensate for dialogLayout padding affecting the EditText view
    FrameLayout editTextWrapper = new FrameLayout(context);
    editTextWrapper.addView(newName);
    editTextWrapper.setPadding(5, 0, 5, 0);

    dialogLayout.addView(editTextWrapper);
    dialogLayout.addView(submit);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM