簡體   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