BottomSheetDialogFragment在android软键(后退,首页)上设置动画

时间:2019-05-15 17:25:08

标签: android android-fragments android-animation android-dialogfragment android-dialog

我在项目中使用了一个BottomSheetDialogFragment,并注意到BottomSheetDialog正在对软键进行动画处理,效果不佳(See Fig 1)
但是,在上下拖动对话框时,动画不会与软键(Fig. 2)
重叠。 有没有办法避免在演出/解雇时出现这种重叠?

我设置了一个最小的Android项目进行测试,并将“ bottom_sheet_slide_duration”设置为500,以使问题更明显。 代码很简单:

活动:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity_activity);
    findViewById(R.id.container).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            (new DialogFragment()).show(getSupportFragmentManager(), null);
        }
    });
}}

片段:

public class DialogFragment extends BottomSheetDialogFragment {

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

我不明白为什么这是默认行为...如果看到此,我的眼睛就会受伤:)

1 个答案:

答案 0 :(得分:0)

您可以改为将片段添加到活动容器布局中,使其与父底部对齐,以达到预期效果。

以下是工作代码段:

主要活动:

 boolean fragVisible = false;
    private DialogFragment newFragment;

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


        findViewById(R.id.container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fragVisible) {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.bottom_to_top, 0);
                    fragmentTransaction.remove(newFragment);
                    fragmentTransaction.commit();
                    fragVisible = false;
                } else {
                    newFragment = new DialogFragment();
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.top_to_bottom, 0);
                    fragmentTransaction.replace(R.id.frag_container, newFragment, "h");
                    fragmentTransaction.commit();
                    fragVisible = true;
                }
            }
        });

    }

activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.jawadzeb2.socketlibrary.NativeActivityTest">


    <FrameLayout
        android:id="@+id/frag_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />


</RelativeLayout>