对话框中按钮周围不需要的填充

时间:2014-06-19 03:24:03

标签: android button layout dialog padding

问题:

Unwanted Padding

您可以清楚地看到按钮周围的填充

守则:

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. 使用构建器.create()方法构建到AlertDialog并设置.setView(dialogLayout, 0, 0, 0, 0)

  2. 通过尝试ViewGroup parent = (ViewGroup) dialogLayout.getParent()然后尝试parent.setPadding(0, 0, 0, 0);来删除父级的填充(这会返回NullPointerException错误,因为您无法为{{1}设置填充并且Dialog

  3. 还有其他想法吗??

    提前致谢!

    JRad the Bad

3 个答案:

答案 0 :(得分:1)

android中的默认按钮有一个自然填充。您可以通过更改布局XML上的背景或样式来删除它。

答案 1 :(得分:0)

设定最低高度&最小宽度为" 0"然后填充将删除前

submit.setMinHeight(0);

答案 2 :(得分:0)

所以我想出了另一个解决方案: 请记住,如果SDK中的9个补丁图像将来发生变化,这可能会破坏,但除此之外,它可以毫无效地删除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);