2016-09-20 100 views
1

我一直在尝试删除DialogFragment的标题,但多次尝试失败。我试着用这些:删除DialogFragment的标题

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

这:

int style, theme; 

    style = DialogFragment.STYLE_NO_TITLE; 
    theme = R.style.dialog;//Tried using Theme.Holo.Dialog here too 


    setStyle(style, theme);//Setting theme to 0 renders it invisible. Content only 

这(没有效果):

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    Dialog dialog = super.onCreateDialog(savedInstanceState); 

    // request a window without the title 
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
    return dialog; 
} 

都没有去除标题的效果。

还有其他想法吗?

编辑:

重要的代码在DIalogFragment:

public class MenuDialog extends DialogFragment implements View.OnClickListener { 
    Context c; 
    Clicker cl; 
    Game game; 

    public static MenuDialog newInstance() { 
     MenuDialog f = new MenuDialog(); 

     // Supply num input as an argument. 
     Bundle args = new Bundle(); 

     f.setArguments(args); 

     return f; 
    } 

    public void params(Context c, Clicker cl, Game game){ 
     this.c = c; 
     this.cl = cl; 
     this.game = game; 
    } 




    @Override 
    public void onCreate(Bundle sis){ 
     super.onCreate(sis); 

     int style, theme; 

     style = DialogFragment.STYLE_NO_TITLE; 
     theme = R.style.dialog;//This theme is as defined above 


     setStyle(style, theme); 


    } 

    private View v; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.menu, container, false); 
     this.v = v; 
     setButtons(); 
     return v; 
    } 

} 

显示对话框:

FragmentTransaction ft = getFragmentManager().beginTransaction(); 
    Fragment prev = getFragmentManager().findFragmentByTag("dialog"); 
    if (prev != null) { 
     ft.remove(prev); 
    } 
    ft.addToBackStack(null); 

    // Create and show the dialog. 
    MenuDialog newFragment = MenuDialog.newInstance(); 
    newFragment.params(getBaseContext(), clicker, this); 
    newFragment.show(ft, "dialog"); 
+0

我有同样的问题。解决'getDialog()。getWindow()。requestFeature(Window.FEATURE_NO_TITLE);'在'onViewCreated()' – tpk

+0

添加onViewCreated:没有效果,没有崩溃 – Zoe

+0

我使用onCreateDialog作为一个不同的SO问题。我添加了一些更多的代码片段。我在onCreateView中创建片段的内容 – Zoe

回答

3

创建对话框正常,并显示之前添加一行:

myDialog.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

编辑: 加入.getDialog()呼叫

编辑2:测试与

现在我已经测试此代码对你的榜样的链接,它的工作原理:

public class HelpDialog extends DialogFragment { 

    public HelpDialog() { 
     // Empty constructor required for DialogFragment 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     //Inflate the XML view for the help dialog fragment 
     View view = inflater.inflate(R.my_layout, container); 

     HelpDialog.this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); 


     return view; 
    } 

} 

和我打电话给那样的对话框:

HelpDialog dialog = new HelpDialog(); dialog.show(getFragmentManager(),"test");

+0

“无法解析符号”。您是否想过使用Dialog或DialogFragment?这是可能的对话框,但不是DialogFragment – Zoe

+0

噢,你是对的,我会编辑我的答案 –

+0

java.lang.NullPointerException:试图调用虚拟方法'布尔android.app.Dialog.requestWindowFeature(int)'对空引用 - 这个对话框在调用 – Zoe