我正在使用对话框片段进行全屏对话。我成功地显示了对话框但问题在于它的宽度和高度。它无法显示全屏大小。我想设置宽度和高度与累积全屏大小像活动。请指导我如何设置对话框的宽度和高度,以将其累积到全屏大小。
CustomeDialogFramgnet:
public class CustomDialogFragment extends DialogFragment {
/** The system calls this to get the DialogFragment's layout, regardless
of whether it's being displayed as a dialog or an embedded fragment. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout to use as dialog or embedded fragment
View rootView = inflater.inflate(R.layout.item_fragment, null, false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
toolbar.setTitle("Dialog title");
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(android.R.drawable.ic_menu_close_clear_cancel);
}
return rootView;
}
/** The system calls this only when creating the layout in a dialog. */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.alert_dialog_input, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_save) {
// handle confirmation button click here
return true;
} else if (id == android.R.id.home) {
// handle close button click here
dismiss();
return true;
}
return super.onOptionsItemSelected(item);
}
}
item_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:text="zohaib">
</TextView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
答案 0 :(得分:12)
在theme
中创建一个style.xml
,其范围从Theme.AppCompat.Light.DarkActionBar
延伸,如下所示:
<style name="DialogFragmentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:paddingRight">0dp</item>
<item name="android:paddingLeft">0dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:windowNoTitle">true</item>
</style>
并设置您在打开style
时创建的dialog
,如下所示:
CustomDialogFragment mDialog = new CustomDialogFragment();
...
mDialog.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentTheme);
mDialog.show(getSupportFragmentManager(), "DialogFragment");
答案 1 :(得分:2)
在创建对话框后设置此行。
dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
如果您要创建自定义DialogFragment
,请在onCreate
的{{1}}中写下此内容:
DialogFragment
答案 2 :(得分:2)
像这样创建主题并添加到对话框
<style name="ThemeDialog" parent="android:Theme.Holo.Dialog">
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
</style>
在创建对话框后设置此行
getDialog().getWindow()
.getAttributes().windowAnimations = R.style.ThemeDialog;
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.gravity = Gravity.FILL_HORIZONTAL;
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
答案 3 :(得分:0)
在onCreate中添加此行,以便为布局充气。
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
答案 4 :(得分:0)
在style.xml中创建以下主题:
<style name="DialogTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:paddingRight">0dp</item>
<item name="android:paddingLeft">0dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:windowNoTitle">true</item>
</style>
然后在DialogFragment中设置样式
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogTheme);
}
答案 5 :(得分:0)
找到了一种更安全的方法,这似乎是Android的常规做法。 DialogFragment确实从Fragment继承。因此,只需像其他片段一样显示该片段:
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.contentFragment, dialogFragment)
.addToBackStack(null)
.commitAllowingStateLoss()