强制执行DialogFragment的大小/使其超越屏幕

时间:2012-10-23 15:14:13

标签: android android-dialogfragment

我有以下情况:我需要在对话框中放置一个大图像(超出屏幕大小),但是我仍然需要对话框框架顶部有大约150dp的边距(这样它后面的标题就会可见)。我可以通过让对话框超出屏幕,或者通过强制执行对话框的大小并将其y位置设置为某个值来实现这一点,让我们说150.但是现在发生的是,当屏幕中没有位置时,DialogFragment仍然可以扩展到屏幕的边界(它在屏幕尺寸大到足以保持图像全尺寸的设备上工作),因此忽略垂直起始位置y。有什么办法可以解决这个问题吗?这是我到目前为止的代码:

public static class MyDialogFragment extends DialogFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, 0);
    }

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

        Window window = getDialog().getWindow();
        // window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
        // WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

        Display display = window.getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
        WindowManager.LayoutParams p = window.getAttributes();
        p.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
        p.x = 240;
        p.y = 40;
        p.height = 100;
        // p.verticalMargin = 50.0f;
        window.setAttributes(p);

        // ScrollView scv = (ScrollView) v.findViewById(R.id.findingsPopup);
        // ViewGroup.LayoutParams svcp = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, height - 200);
        // scv.setLayoutParams(svcp);

        getDialog().setCanceledOnTouchOutside(true);
        ImageView image = (ImageView) v.findViewById(R.id.findingsImage);
        image.setImageResource(R.drawable.dummy_findings_picture); // TODO dummy image

        Button closeButton = (Button) v.findViewById(R.id.closeButton);
        closeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MyDialogFragment.this.dismiss();
            }
        });

        // this.setStyle(DialogFragment.STYLE_NO_TITLE, 0);

        return v;
    }
}

这是xml布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/findingsPopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100dp"
android:minHeight="50dp" >

<Button
    android:id="@+id/closeButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onClose"
    android:text="@string/close" 
    android:layout_marginLeft="200dp"/>

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/closeButton" >

    <ImageView
        android:id="@+id/findingsImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="false"
        android:contentDescription="@string/logo"
        android:src="@drawable/acute_myocarditis_cover" />
</ScrollView>
</RelativeLayout>

0 个答案:

没有答案