Android的支持工具栏:调整不会重新排列菜单项 [英] Android Support Toolbar: Resizing won't realign menu items
问题描述
我尝试调整我的活动
的方向变化Android的支持工具栏,因为它是在横向模式下太大。它是我使用的不是自动调整
I try to resize the Android Support Toolbar of my Activity
on orientation change as it is too big in landscape mode. It's not automatically resized as I'm using
android:configChanges="orientation|screenSize"
在我的活性,因此所述活动将不被重新创建。
工具栏XML是这样的:
on my activity and thus the activity won't be recreated. The toolbar XML is like this:
<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()
我调整工具栏例如像这样的:
in my onConfigurationChanged()
I'm resizing the toolbar e.g. like this:
findViewById(R.id.my_awesome_toolbar).setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 114));
并不如预期的结果。菜单项,其中使用 setSupportActionBar(工具栏)填入;
和 onCreateOptionsMenu()
不正确垂直对齐既不是导航图标( toolbar.setNavigationIcon(...)
:
The result is not as expected. The menu items, which are populated using setSupportActionBar(toolbar);
and the onCreateOptionsMenu()
aren't correctly vertically aligned and neither is the navigation icon (toolbar.setNavigationIcon(...)
:
任何人都知道一个更好的方式来调整工具栏或我需要使用变通方法如消除和readding工具栏视图栈?
Anyone know of a better way to resize the toolbar or do I need to use workarounds like removing and readding the toolbar to the view stack?
推荐答案
就叫 setMinimumHeight
工具栏,它会正常工作。
Just call setMinimumHeight
for toolbar and it will work.
@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);
}
}
这篇关于Android的支持工具栏:调整不会重新排列菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!