1

我们知道,纵向(56dp)和横向(48dp)的工具栏高度应该不同。有没有办法让工具栏改变方向变化的高度(以及缩放标题和图像)?Android响应式工具栏高​​度

我强烈希望保存在AndroidManifest这一行,因为它是用于其他用途非常有用:

android:configChanges="orientation|screenSize" 

(在搜索菜单的工具栏住宿是非常有用的我真的不知道一个好的方法做到这一点不android:configChanges

回答

0

有一个丑陋的方式做到这一点,你知道会的高度,通过使用自定义TextViewToolBar里面的标题是什么,后来扩展一切,当用户旋转手机。

<android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" > 

     <TextView 
      android:textColor="?android:textColorPrimary" 
      android:id="@+id/titleTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 

</android.support.v7.widget.Toolbar> 

在这里,你Activity直接的onConfigurationChanged,我们改变了ToolBar的高度并设置了自定义标题的文本大小;

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    View tb = findViewById(R.id.toolbar); 
    TextView titleTextVIew = (TextView) findViewById(R.id.titleTextView); 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     tb.getLayoutParams().height = (int) Tool.convertDpToPixel(48, this); 
     titleTextVIew.setTextSize(TypedValue.COMPLEX_UNIT_SP,15); 
    }else { 
     tb.getLayoutParams().height = (int) Tool.convertDpToPixel(56, this); 
     titleTextVIew.setTextSize(TypedValue.COMPLEX_UNIT_SP,20); 
    } 
    super.onConfigurationChanged(newConfig); 
}