2016-08-30 118 views
4

我正在使用對話框片段進行全屏對話框。我成功顯示對話框,但問題在於它的寬度和高度。它無法顯示全屏幕大小。我想設置寬度和高度,並像活動一樣累積全屏大小。請引導我如何設置對話框的寬度和高度以將其累積爲全屏尺寸。如何使用對話框片段設置全屏對話框

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

您可以在對話片段使用bottombar,畢竟使用的是協調佈局..你可以拖動作爲全屏 –

+0

請寫一些示例代碼 –

+0

引用這一個..它可能會幫助你... http://www.androidhive.info/2016/04/android-glide-image-library-建築-I mage-gallery-app/ –

回答

12

創建您style.xml一個themeTheme.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"); 
+0

不錯。其工作感謝之人 –

+0

太棒了,祝你有美好的一天。 – himanshu1496

+0

還有一個問題,dimiss()函數也不起作用。你有什麼想法嗎? –

1

組此行創建對話框後。

dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 

如果要創建自定義DialogFragmentDialogFragmentonCreate這樣寫:

setStyle(DialogFragment.STYLE_NORMAL,android.R.style.Theme_Black_NoTitleBar_Fullscreen); 

reference

+0

我在這行之後添加了對話框dialog = super.onCreateDialog(savedInstanceState);但沒有工作 –

+0

它也不能正常工作 –

+0

你有其他方法嗎? –

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); 
+0

設置重力FILL_HORIZONTAL是我一直在尋找的東西。謝謝。 – 0neel

0

添加此行在onCreate你膨脹你的佈局。

getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 
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); 
}