我正在设计一个自定义对话框(使用DialogFragment),提示日历视图,允许用户选择日期。显示对话框但“OK”和“取消”仅部分可见。 Here is the screenshot for your reference.
这是我的自定义dialogfragment类
public class GetDateDialogFragment extends DialogFragment implements android.content.DialogInterface.OnClickListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
LayoutInflater inflater = getActivity().getLayoutInflater();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View calendarView = inflater.inflate(R.layout.activity_datepicker, null);
calendarView.findViewById(R.id.btnOk).setVisibility(View.INVISIBLE);
calendarView.findViewById(R.id.btnCancel).setVisibility(View.INVISIBLE);
builder.setView(calendarView);
AlertDialog dialog = builder.setTitle(R.string.datetitle).setMessage("Please enter Date")
.setPositiveButton("OK", this).setNegativeButton("Cancel", null).create();
dialog.show();
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, 600);
//position the buttons
//Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
//positiveButton.setY(positiveButton.getY()-100);
//Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
//negativeButton.setY(negativeButton.getY()-100);
return dialog;
}
请查看以下布局activity_datepicker.xml的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="250dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical" />
<Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="false"
android:layout_alignRight="@id/calendarView"
android:text="@string/btnOk" />
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/calendarView"
android:layout_alignParentBottom="true"
android:text="@string/btnCancel" />
答案 0 :(得分:2)
尝试设置ok取消按钮的可见性,而不是隐藏
LayoutInflater inflater = this.getLayoutInflater();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View calendarView = inflater
.inflate(R.layout.activity_datepicker, null);
calendarView.findViewById(R.id.btnOk).setVisibility(View.GONE);
calendarView.findViewById(R.id.btnCancel).setVisibility(View.GONE);
builder.setView(calendarView);
AlertDialog dialog = builder.setTitle("dfd")
.setMessage("Please enter Date").setPositiveButton("OK", null)
.setNegativeButton("Cancel", null).create();
dialog.show();
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);