在我的应用程序中,我有一个自定义对话框,该对话框在顶部显示一些多余的边距,而在xml中,我没有在顶部定义任何类型的边距或填充。仍然我的对话框看起来像这样-
相同的代码就像-
<?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>
我希望消除“确认”文本上不必要的空白。
引发对话框的类是-
公共类MyClass扩展了活动{
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类看起来像-
公共类CustomDialogBox扩展了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);
}
}
请帮忙。
最佳答案
添加此this.requestWindowFeature(Window.FEATURE_NO_TITLE);
像
public CustomDialogBox(Context context, Object[] data) {
super(context);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
initializeViews();
fillData(data);
}
关于android - 从CustomDialog减少标题的边距,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9513314/