Android - ActionBar not resizing with onConfigurationChanged ( AppCompat )

前端 未结 3 1419
夕颜
夕颜 2020-12-11 17:31

In my application manifest I\'ve add android:configChanges to prevent activity reload/restart on rotate



        
3条回答
  •  盖世英雄少女心
    2020-12-11 18:26

    As others have pointed out you should save and restore the instance state instead of handling configuration changes yourself if possible. If you have good reason not to do that you can try to update the toolbar's height and text appearance after the configuration change.

    The following code should work for the support library version of Toolbar. The attributes actionBarSize, titleTextAppearance and subtitleTextAppearance are provided by the support library.

    The code assumes that you have a custom attribute appToolbarStyle declared in attrs.xml. If you don't need that you can adapt the code to use R.style.Widget_AppCompat_Toolbar directly instead.

    import android.support.v7.widget.Toolbar;
    
    ...
    
    private Toolbar toolbar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.main_activity);
    
        toolbar = findViewById(R.id.toolbar);
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        updateToolbar();
    }
    
    private void updateToolbar() {
        if (toolbar == null)
            return;
    
        final Context context = toolbar.getContext();
    
        int[] attr = new int[] { R.attr.actionBarSize, R.attr.appToolbarStyle };
        int idxActionBarSize = 0;
        int idxAppToolbarStyle = 1;
        TypedArray a = context.obtainStyledAttributes(attr);
        int actionBarSize = a.getDimensionPixelSize(idxActionBarSize, 0);
        int appToolbarStyle = a.getResourceId(idxAppToolbarStyle, R.style.Widget_AppCompat_Toolbar);
        a.recycle();
    
        if (actionBarSize != 0) {
            ViewGroup.LayoutParams layoutParams = toolbar.getLayoutParams();
            if (layoutParams != null) {
                layoutParams.height = actionBarSize;
            }
    
            toolbar.setMinimumHeight(actionBarSize);
        }
    
        attr = new int[] { R.attr.titleTextAppearance, R.attr.subtitleTextAppearance };
        int idxTitleTextAppearance = 0;
        int idxSubtitleTextAppearance = 1;
        a = context.obtainStyledAttributes(appToolbarStyle, attr);
        int titleTextAppearance = a.getResourceId(idxTitleTextAppearance, 0);
        int subtitleTextAppearance = a.getResourceId(idxSubtitleTextAppearance, 0);
        a.recycle();
    
        if (titleTextAppearance != 0) {
            toolbar.setTitleTextAppearance(context, titleTextAppearance);
        }
    
        if (subtitleTextAppearance != 0) {
            toolbar.setSubtitleTextAppearance(context, subtitleTextAppearance);
        }
    
        toolbar.requestLayout();
    }
    

提交回复
热议问题