繁体   English   中英

带有底部导航栏的应用中框架布局内的AlertDialog

[英]AlertDialog inside a frame layout in an app with bottom navigation bar

我做了一个带有底部导航栏的简单应用。 我希望栏上的按钮之一显示一个片段,当它使用setCancelable(false)加载时,上面会显示一个alertdialog,但是我仍然可以在其他片段之间进行切换。 但是,当我在fragment类的onCreate()中使用警报对话框时,播放应用程序并选择带有警报对话框的按钮,警报对话框将覆盖整个屏幕,而我无法使用底部导航栏中的其他按钮。 因此,基本上我想做的是仅在框架布局(容器)中显示警报对话框,而不在整个屏幕上显示警报对话框,以便当警报对话框仍在屏幕上时,我仍然可以使用导航栏上的其他按钮。 假设您有一个具有两个单独片段布局的应用程序。 我希望警报对话框仅以一种布局显示。

activity_main.xml中

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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="abdul.com.myapp1.MainActivity">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/holo_orange_dark">
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />

</LinearLayout>

MainActivity.java

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView bottomNavigationView = (BottomNavigationView)
                findViewById(R.id.navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener
                (new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Fragment selectedFragment = null;
                        switch (item.getItemId()) {
                            case R.id.navigation_home:
                                selectedFragment = second.newInstance();
                                break;
                            case R.id.navigation_dashboard:
                                selectedFragment = Third.newInstance();
                                break;
                            case R.id.navigation_notifications:
                                selectedFragment = Fourth.newInstance();
                                break;
                            case R.id.navigation_fifth:
                                selectedFragment = Fifth.newInstance();
                                break;

                        }
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout, selectedFragment);
                        transaction.commit();
                        return true;
                    }
                });

        //Manually displaying the first fragment - one time only
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, second.newInstance());
        transaction.commit();

        //Used to select an item programmatically
        //bottomNavigationView.getMenu().getItem(2).setChecked(true);
    }
}

Third.java(这是我要使用alertdialog的地方)

    public class Third extends Fragment {
    public static Third newInstance() {
        Third fragment = new Third();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

 AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
                //LayoutInflater factory = LayoutInflater.from(v.getContext());
               View mView2 = getActivity().getLayoutInflater().inflate(popup_onpressed, null);
               mBuilder.setView(mView2);
               final AlertDialog dialog = mBuilder.create();
               dialog.show();
               dialog.setCancelable(false);
               dialog.setCanceledOnTouchOutside(false);

    }

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

我也尝试将其粘贴在onCreateView()中,但是它也没有起作用。

首先,您应该自定义alartdialog,然后可以使用此代码在屏幕的任何位置显示AlertDialog。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM