DialogFragment与透明背景

时间:2016-08-03 06:32:02

标签: android android-dialogfragment android-dialog

我试图创建一个全屏(但状态栏必须仍然可见)对话框具有自定义透明(与此答案相同https://stackoverflow.com/a/29482234),但我的背景不透明。

我浪费了2天尝试所有解决方案,但它只是没有工作。我的目标是显示一个自定义暗淡颜色的对话框(而不是默认的黑色)。上面的答案看起来像我需要但我不能让它工作。有什么建议吗?

我的代码:

<style name="CustomDialogTheme2" parent="@android:style/Theme.Dialog">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#AAFFFFFF">

    <RelativeLayout
        android:id="@+id/dialog_root"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="#CCFF0000"
        android:padding="16dp">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />


    </RelativeLayout>

</FrameLayout>

DialogFragment:

public class TestDialogFrag extends DialogFragment {

    public static TestDialogFrag newInstance() {

        Bundle args = new Bundle();

        TestDialogFrag fragment = new TestDialogFrag();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialogTheme2);
    }

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


}

1 个答案:

答案 0 :(得分:0)

请尝试以下操作,它会对您有所帮助。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center">

    <LinearLayout
        android:layout_width="260dp"
        android:layout_height="wrap_content"
        android:background="@color/colorWhite"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="72dp"
            android:layout_gravity="center"
            android:gravity="center">

            <ImageView
                android:id="@+id/dialog_universal_info_image"
                android:layout_width="match_parent"
                android:layout_height="72dp"
                android:background="#ff0000"
                android:contentDescription="Imagg"
                android:scaleType="centerCrop" />

            <View
                android:layout_width="match_parent"
                android:layout_height="72dp"
                android:background="#4cbdbcbc" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="@string/app_name"
                android:textColor="@android:color/white"
                android:textAppearance="?android:textAppearanceLarge" />
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="22dp"
            android:paddingLeft="26dp"
            android:paddingRight="26dp"
            android:paddingTop="22dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/dialog_info_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:layout_marginTop="10dp"
                    android:text="heare"
                    android:textColor="@android:color/holo_green_dark"
                    android:textAppearance="?android:textAppearanceLarge" />

                <TextView
                    android:id="@+id/dialog_info_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="4"
                    android:text="Content is here"
                    android:textColor="@android:color/holo_green_dark" />
            </LinearLayout>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="22dp"
            android:layout_marginRight="22dp"
            android:background="@android:color/holo_blue_dark" />


        <TextView
            android:id="@+id/dialog_info_ok"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="OK"
            android:textColor="@android:color/holo_green_dark"
            android:textSize="14sp" />
    </LinearLayout>

</RelativeLayout>

Java文件

 public class DialogUtilsInfo {

        public Activity mDialogUniversalInfoActivity;
        private Dialog mDialog;

        private TextView mDiaappicon_48KButton;

        public DialogUtilsInfo(Activity mDialogUniversalActivity) {
            this.mDialogUniversalInfoActivity = mDialogUniversalActivity;
        }

        public void showDialog(String content) {
            if (mDialog == null) {
                mDialog = new Dialog(mDialogUniversalInfoActivity, R.style.CustomDialogTheme);
            }
            mDialog.setContentView(R.layout.dialog_info);
            mDialog.setCancelable(true);
            mDialog.show();
            mDiaappicon_48KButton = (TextView) mDialog.findViewById(R.id.dialog_info_ok);

            initDialogButtons1();
        }


        private void initDialogButtons1() {

            mDiaappicon_48KButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                }
            });
        }

        public void dismissDialog() {
            mDialog.dismiss();
        }
    }