2014-11-17 56 views
4

我尝试调整我的Activity的Android支持工具栏的方向更改大小,因为它在横向模式下太大。它不会自动调整大小,因为我正在使用我的活动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" /> 
在我我调整工具栏例如

像这样:

findViewById(R.id.my_awesome_toolbar).setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 114)); 

结果与预期不符。菜单项,其使用setSupportActionBar(toolbar);填充和onCreateOptionsMenu()不正确垂直对齐也不是导航图标(toolbar.setNavigationIcon(...)

任何人都知道一个更好的方式来调整工具栏或者我需要使用变通方法,如消除和readding工具栏视图栈?

+0

能你尝试改变这个'android:layout_height =“?attr/actionBarSize”'到'android:layout_height =“wrap_content”'? –

+0

@YeLinAung无法工作。如上所述,工具栏高度不会在旋转时自动更改,因此将其设置为wrap_content也不会改变任何内容。 – Boni2k

+0

你试过了吗? –

回答

2

只需拨打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); 
    } 
} 
+0

这个解决方法确实有效。谢谢。 – Boni2k

相关问题