全屏DialogFragment-在AppBar下面显示带有事务,但可与简单的.show()一起使用

时间:2019-03-13 13:33:31

标签: android fragment android-dialogfragment

我遵循了this tutorial(4。全屏对话框),它接近于Material Design准则。

但是有趣的是,如果我像这样使用official code

FragmentManager fragmentManager = getSupportFragmentManager();
FullscreenDialogFragment newFragment = new FullscreenDialogFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(android.R.id.content, 
newFragment).addToBackStack(null).commit();

我得到:

enter image description here

但是使用“简单”方法

DialogFragment dialog = new FullscreenDialogFragment();
dialog.show(getSupportFragmentManager(), TAG);

它按预期工作:

enter image description here

那么这是怎么回事?


我的代码

我的对话框类基本上相同,只是我没有在教程中使用它

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    return dialog;
}

但是我有一个指定的样式文件:

<!-- SA suggestion dialog theme. -->
<style name="FullscreenDialogTheme" parent="Base.Theme.AppCompat.DialogWhenLarge">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/colorLightBackground</item>
    <item name="android:windowIsFloating">false</item>

    <item name="android:windowEnterAnimation">@android:anim/slide_in_left</item>
    <item name="android:windowExitAnimation">@android:anim/slide_out_right</item>
</style>

我设置为

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NORMAL, R.style.FullscreenDialogTheme);
}

这是对话框布局xml(我将支持小部件替换为AndroidX)

<?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:background="@android:color/white"
     android:fitsSystemWindows="true"
     android:orientation="vertical">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/sa_dialog_close_btn"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:tint="@android:color/white"
            android:src="@drawable/ic_close_white_24dp"
            android:contentDescription="@string/sa_dialog_close_btn_content_desc" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="@string/dialog_title"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Title"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/sa_dialog_confirm_btn"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:paddingLeft="2dp"
            android:paddingRight="2dp"
            android:text="@string/sa_dialog_confirm"
            android:textColor="@android:color/white" />

    </LinearLayout>

</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:id="@+id/nested_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    android:scrollingCache="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="Full Screen Dialog"/>

</androidx.core.widget.NestedScrollView>

和对话框类

public class FullscreenDialogFragment extends DialogFragment {

@BindView(R.id.sa_dialog_close_btn)
ImageButton close_btn;
@BindView(R.id.sa_dialog_confirm_btn)
Button confirm_btn;
//Necessary in fragments
private Unbinder unbinder;

Context mContext;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NORMAL, R.style.FullscreenDialogTheme);
}

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

 //        mContext = Objects.requireNonNull(container).getContext();
    unbinder = ButterKnife.bind(this, view);

    close_btn.setOnClickListener(listener -> cancelDialog());
    confirm_btn.setOnClickListener(listener -> confirmDialog());

    return view;
}

private void cancelDialog() {
    dismiss();
}

private void confirmDialog() {
//        Toast.makeText(mContext, "Confirmed", Toast.LENGTH_SHORT).show();
}

@Override
public void onDestroy() {
    super.onDestroy();
    unbinder.unbind();
 }
}

我猜这与这条线有关,如以下答案所示:
https://stackoverflow.com/a/44085523/1972372

transaction.add(android.R.id.content, fragment)

在哪里应该从创建片段的活动中引用布局?
我尝试使用活动的根目录布局进行尝试,但只出现了奇怪的内容重叠:

transaction.add(R.id.sa_activity_parent_layout, newFragment).addToBackStack(null).commit();

enter image description here

1 个答案:

答案 0 :(得分:1)

有一些区别。show()方法使用对话框作为容器,另一个方法将片段添加到android.R.layout.content