我想展示一个几乎全屏的对话片段.但不知怎么的我做不到.

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

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,以填充_parent和一些minWidth和minHeight.

<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会占据大部分屏幕.但我似乎只在垂直方向调整大小,但只在水平方向上调整到某个固定宽度.

我还try 在代码中设置窗口属性,正如这里建议的:http://groups.google.com/group/android-developers/browse_thread/thread/f0bb813f643604ec.但这也没用.

我可能对Android如何处理对话框有些误解,因为我对它是全新的.我怎么能做这样的事?有没有其他方法可以达到我的目标?


Android设备:
华硕EeePad变压器
Android 3.0.1


Update:个 现在我设法使其全屏显示,并在片段中包含以下代码

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);
}

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

有没有办法做到这一点?

推荐答案

试着换成LinearLayout,而不是RelativeLayout.在测试时,我的目标是3.0蜂巢API.

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;
    }

}
}

layouts人:

<?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

<?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>

Android相关问答推荐

ava.lang. ClassNotFound异常:没有找到类com.example.myapp.MyApp"

图像在Android Studio中显示,但不在设备中显示

Jetapck Compose:将Compose BOM更新为最新版本&2024.01.00&Quot;CircularProgressIndicator后出现错误

StateFlow集合AsState没有更新他的值Jetpack Compose导航

登录方法显示为空列表,即使它显示的是Firebase身份验证控制台

无法将Kotlin序列化添加到Android项目

如何制作安卓';s FileProvider在Android 11上使用外部存储+

Kotlin - 在继续之前如何等待这个协程完成?

使用 List 和 LazyColumn 重新组合所有项目

在模块 jetified-kotlin-stdlib-1.8.10 中发现重复的类 kotlin.random.jdk8,带有启动基准

安卓模拟器打不开

Jetpack compose :使用 rememberSaveable 时未应用待处理的合成

找不到(包名称).在以下位置搜索:

如何关闭可组合对话框?

是什么让 Android Studio 中的按钮变成紫色?加上新手的其他奇怪行为

如何在 Compose 中更改高程 colored颜色 ?

Android - 水平(从右到左)圆形背景 colored颜色 过渡

Android Material Date Range Picker - 如何仅更改所选范围日期的文本 colored颜色 ?

如何使用 Kotlin 在 Android Wear(Galaxy watch 4)中继续在后台运行应用程序

在jetpack compose中看不到圆角