首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓中的全屏DialogFragment

安卓中的全屏DialogFragment
EN

Stack Overflow用户
提问于 2011-08-25 19:59:43
回答 31查看 183.6K关注 0票数 187

我正在试着显示一个几乎全屏的DialogFragment。但不知何故我不能这么做。

我显示片段的方式直接来自android开发人员文档

代码语言:javascript
复制
FragmentManager f = ((Activity)getContext()).getFragmentManager();
FragmentTransaction ft = f.beginTransaction();
Fragment prev = f.findFragmentByTag("dialog");
if (prev != null) {
    ft.remove(prev);
}
ft.addToBackStack(null);

// Create and show the dialog.
DialogFragment newFragment = new DetailsDialogFragment();
newFragment.show(ft, "dialog");

我知道天真地试图将片段中的RelativeLayout设置为fill_parent以及一些minWidth和minHeight。

代码语言:javascript
复制
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  
    android:minWidth="1000px" 
    android:minHeight="600px"
    android:background="#ff0000">

我知道,预计DialogFragment将占据屏幕的大部分。但我似乎只在垂直方向上调整大小,但只有在水平方向上有固定的宽度时才会调整。

我还尝试在代码中设置窗口属性,如下所示:http://groups.google.com/group/android-developers/browse_thread/thread/f0bb813f643604ec。但这也无济于事。

我可能对Android处理对话框的方式产生了误解,因为我对它是全新的。我怎么能做这样的事情呢?有没有其他方法可以达到我的目标?

Android设备:

华硕EeePad转换器

Android 3.0.1

更新:我现在设法让它全屏显示,片段中有以下代码

代码语言:javascript
复制
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);
}

不幸的是,这不是我想要的。我绝对需要在对话框周围添加一个小的“填充”来显示背景。

有没有办法做到这一点?

EN

回答 31

Stack Overflow用户

回答已采纳

发布于 2011-08-27 01:43:40

尝试切换到LinearLayout而不是RelativeLayout。在测试时,我的目标是3.0 Honeycomb api。

代码语言:javascript
复制
public class FragmentDialog extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button) findViewById(R.id.show);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            showDialog();
        }
    });
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

void showDialog() {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    DialogFragment newFragment = MyDialogFragment.newInstance();
    newFragment.show(ft, "dialog");
}

public static class MyDialogFragment extends DialogFragment {

    static MyDialogFragment newInstance() {
        MyDialogFragment f = new MyDialogFragment();
        return f;
    }

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

}
}

fragment_dialog.xml布局

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:minWidth="1000dp"  
    android:minHeight="1000dp"> 
 </LinearLayout> 

main.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:background="#ffffff">
    <Button android:id="@+id/show"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="show">
    </Button>
</LinearLayout>
票数 73
EN

Stack Overflow用户

发布于 2014-10-02 22:42:08

要让DialogFragment全屏显示

覆盖DialogFragment的onStart,如下所示:

代码语言:javascript
复制
@Override
public void onStart()
{
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null)
    {
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(width, height);
    }
}

非常感谢这篇文章:The-mystery-of-androids-full-screen-dialog-fragments

票数 215
EN

Stack Overflow用户

发布于 2013-02-21 22:03:41

代码语言:javascript
复制
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setStyle(DialogFragment.STYLE_NORMAL,
             android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
票数 163
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7189948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档