我尝试在方向更改时调整我的活动的
Android支持工具栏,因为它在横向模式下太大了.它不会像我使用时那样自动调整大小
android:configChanges="orientation|screenSize"
在我的活动上,因此不会重新创建活动.
工具栏XML是这样的:
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
在我的onConfigurationChanged()我正在调整工具栏的大小,例如像这样:
findViewById(R.id.my_awesome_toolbar).setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 114));
结果不如预期.菜单项,使用setSupportActionBar(工具栏)填充;并且onCreateOptionsMenu()未正确垂直对齐,导航图标也不是(toolbar.setNavigationIcon(…):
任何人都知道更好的方法来调整工具栏的大小,还是我需要使用解决方法,如删除和读取工具栏到视图堆栈?
最佳答案 只需为工具栏调用setMinimumHeight即可.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toolbar tb = getActionBarToolbar();
if (tb != null) {
int size = UIUtils.calculateActionBarSize(this);
tb.setMinimumHeight(size);
ViewGroup.LayoutParams lp = tb.getLayoutParams();
lp.height = size;
tb.setLayoutParams(lp);
}
}