从CustomDialog减少标题的边距

时间:2012-03-01 09:03:20

标签: android customdialog

在我的应用程序中,我有一个自定义对话框,在顶部显示一些不需要的边距,而在xml中,我没有定义任何类型的边距或填充到顶部。我的对话框仍然是这样的 - enter image description here

相同的代码就像 -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/customDialogRelativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dip"
            android:layout_marginRight="15dip"
            android:textStyle="bold"
            android:layout_alignParentTop="true"
            android:textColor="@color/white"
            android:textSize="22sp"/>

        <View
            android:id="@+id/view_separator_in_dialog"
            style="@style/dialog_separator"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_message"
            style="@style/dialog_message"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="gone" />

        <EditText
            android:id="@+id/et_input_text"
            style="@style/dialog_edittext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_message"
            android:paddingLeft="10dip"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_caption"
            style="@style/dialog_caption"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/et_input_text"
            android:visibility="gone" />

        <LinearLayout
            android:id="@+id/rl_dialog_buttons_horizontal"
            style="@style/dialog_rl_buttons"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/btn_left_dialog"
                style="@style/dialog_button_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:visibility="gone" />

            <Button
                android:id="@+id/btn_right_dialog"
                style="@style/dialog_button_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:visibility="gone" />

        </LinearLayout>
</RelativeLayout>

我希望删除“确认”文本上的不必要的差距。

引发对话框的类就像 -

public class MyClass扩展了Activity {

private View _separator;
private CustomDialogBox _dialog;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_dialog);

    Object[] data = new Object[]{"Confirmation!", "Contact with this number already exits. Do you want to merge?", "Merge", "Create New"};
    raiseDailog(this, data);
}

private void raiseDailog(Context context, Object[] data) {
    _dialog = new CustomDialogBox(this, data);
    _dialog.show();
}

}

我的CustomDialog类看起来像 -

public class CustomDialogBox extends Dialog {

private TextView _title, _message;
private Button _leftButton, _rightButton;
private View _separator;

private String textviewTop;
private String textviewMiddle;
private String buttonText1;
private String buttonText2;

public CustomDialogBox(Context context, Object[] data) {
    super(context);

    setContentView(R.layout.custom_dialog);
    initializeViews();
    fillData(data);
}

private void fillData(Object[] data) {
    try {
        int i = 0;
        textviewTop = (String) data[i++];

        textviewMiddle = (String) data[i++];

        buttonText1 = (String) data[i++];
        buttonText2 = (String) data[i++];
    } catch (Exception e) {
        e.printStackTrace();
    }

    generateDialog();
}

private void generateDialog() {
    //Title goes here
    _title.setVisibility(View.VISIBLE);
    _title.setText(textviewTop);

    //Separator goes here
    _separator.setVisibility(View.VISIBLE);

    //Message goes here
    _message.setVisibility(View.VISIBLE);
    _message.setText(textviewMiddle);

    //Ok button goes here
    _leftButton.setVisibility(View.VISIBLE);
    _leftButton.setText(buttonText1);

    //Cancel button goes here
    _rightButton.setVisibility(View.VISIBLE);
    _rightButton.setText(buttonText2);
}

private void initializeViews() {
    _title = (TextView) findViewById(R.id.tv_title);
    _message = (TextView) findViewById(R.id.tv_message);

    _leftButton = (Button) findViewById(R.id.btn_left_dialog);
    _rightButton = (Button) findViewById(R.id.btn_right_dialog);

    _separator = findViewById(R.id.view_separator_in_dialog);
}

}

请帮忙。

3 个答案:

答案 0 :(得分:7)

添加此this.requestWindowFeature(Window.FEATURE_NO_TITLE);

喜欢As​​ ,,

 public CustomDialogBox(Context context, Object[] data) {
        super(context);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog);
        initializeViews();
        fillData(data);

    }

答案 1 :(得分:3)

<item name="android:windowNoTitle">true</item>添加到Dialog主题。

<style name="DialogTheme" parent="android:Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
</style>

然后使用此主题创建Dialog。

public class ThemeDialog extends Dialog {
    protected ThemeDialog(Context context) {
       super(context, R.style.DialogTheme);
    }
}

答案 2 :(得分:0)

对我来说,我使用了以下启动作为我的custom_dialog.xml布局,它没有奇怪的间距(并且占用了整个屏幕):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical">
</LinearLayout>

也许也可以使用它或将layout_height设置为fill_parent?