Toolbar is hidden in nested PreferenceScreen

前端 未结 4 619
一生所求
一生所求 2021-02-02 13:36

I use PreferenceFragment in ActionBarActivity from support-v7 library.
In the Activity I have Toolbar. Everything goes okay, until I o

4条回答
  •  孤独总比滥情好
    2021-02-02 14:15

    As the issue comes from the part that you are still in the same activity/fragment and the nested pref screen is just a dialog you can do the following:

    • You can set preference click listener
    • Get the root view from the dialog: (PreferenceScreen)preference).getDialog().getWindow() .getDecorView().getRootView());

    • Recursively search until find a stub view (there is one, unfortunately I do not know the android.R.id.xxxxx) and set what layout you need as title which will look like the toolbar(You can inflate toolbar):

      private Toolbar toolbar;
      
          public void findViewStub(ViewGroup viewGroup) {
          int childCount = viewGroup.getChildCount();
          for (int i = 0; i < childCount; i++) {
              View childView = viewGroup.getChildAt(i);
              if( childView instanceof ViewStub){
                  ((ViewStub)childView).setLayoutResource(R.layout.your_title_layout);
                 toolbar =  ((ViewStub)childView).inflate();
              }
              if (childView instanceof ViewGroup) {
                  findViewStub((ViewGroup) childView);
              }
          }
      
        }
      
      toolbar.setNavigationIcon();
      toolbar.setNavigationOnClickListener();
      toolbar.setTitle();
      
    • In the layout you can put only a toolbar. And set the back icon. Register for click on it and having reference to the fragment, on click you can dismiss the dialog. You have set title and etc.

提交回复
热议问题