Android Studio ActionBar 宽度/图标位置
Android Studio ActionBar width / icon locations
我的 ActionBar
出现问题,导致我的菜单图标被压在屏幕边缘!
下面是我调整过的样式和声明的一些代码片段:
HomeActivity.xml
private TextView tvViewAll;
DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//Nav Drawer
mDrawerLayout = findViewById(R.id.drawer_layout);
//custom shadow for menu drawer
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle (this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mDrawerToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:statusBarColor">@color/colorBackgroundBlack</item>
<item name="android:navigationBarColor">@color/colorBackgroundBlack</item>
<item name="actionMenuTextColor">@color/colorBackgroundBlackDark</item>
<item name="colorPrimary">@color/colorBackgroundBlackDark</item>
<item name="colorAccent">@color/colorPrimaryDark</item>
<item name="colorButtonNormal">@color/ipBlue</item>
<item name="toolbarNavigationButtonStyle">@color/ipGreen</item>
</style>
<style name="ActionBar.Solid.TMSA.NoTitle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="displayOptions">useLogo|showHome</item>
<item name="logo">@drawable/ic_ipaustralialogo</item>
<item name="android:contentDescription">@string/ip_logo</item>
</style>
<style name="AppTheme.TMSA" parent="@style/AppTheme">
<item name="actionBarStyle">@style/ActionBar.Solid.TMSA.NoTitle</item>
</style>
除了包含政府徽标外,我不记得触摸过 ActionBar
的格式布局,但我无法弄清楚为什么我会看到这个歪斜的菜单图标。
我已经考虑过采用 Toolbar
方法,但不想转换 :P
编码愉快:)
ActionBarDrawerToggle
在 ActionBar
的导航按钮上设置其切换图标。在 AppCompatActivity
中,ActionBar
实际上是下方的 Toolbar
,并且该导航按钮的样式设置为在主题的 toolbarNavigationButtonStyle
属性上设置的 style
资源。
在您的主题中,您在该属性上设置了 color
资源,而不是 style
资源,默认样式中的所有值都将丢失,包括 minWidth
值,这就是为什么你的切换被包裹到 drawable 的宽度。
如果你想修改导航按钮上的一些样式值,你应该创建自己的style
资源,默认style
作为它的parent
,设置所需的属性在那里,并将 style
指定为您的主题 toolbarNavigationButtonStyle
。例如:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="toolbarNavigationButtonStyle">@style/Toolbar.Button.Navigation</item>
</style>
<style name="Toolbar.Button.Navigation" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="android:background">@color/ipGreen</item>
</style>
如果您实际要修改的是汉堡包箭头可绘制对象,它有自己的样式,您可以 "sub-style" 并更改其中的某些功能。例如:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="drawerArrowStyle">@style/DrawerArrowToggle</item>
</style>
<style name="DrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="color">@color/ipGreen</item>
</style>
下面是 drawerArrowStyle
中可修改的完整属性列表,如果您想自定义其任何其他属性。
<!-- The drawing color for the bars -->
<attr name="color" format="color"/>
<!-- Whether bars should rotate or not during transition -->
<attr name="spinBars" format="boolean"/>
<!-- The total size of the drawable -->
<attr name="drawableSize" format="dimension"/>
<!-- The max gap between the bars when they are parallel to each other -->
<attr name="gapBetweenBars" format="dimension"/>
<!-- The length of the arrow head when formed to make an arrow -->
<attr name="arrowHeadLength" format="dimension"/>
<!-- The length of the shaft when formed to make an arrow -->
<attr name="arrowShaftLength" format="dimension"/>
<!-- The length of the bars when they are parallel to each other -->
<attr name="barLength" format="dimension"/>
<!-- The thickness (stroke size) for the bar paint -->
<attr name="thickness" format="dimension"/>
我的 ActionBar
出现问题,导致我的菜单图标被压在屏幕边缘!
下面是我调整过的样式和声明的一些代码片段:
HomeActivity.xml
private TextView tvViewAll;
DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//Nav Drawer
mDrawerLayout = findViewById(R.id.drawer_layout);
//custom shadow for menu drawer
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle (this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mDrawerToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:statusBarColor">@color/colorBackgroundBlack</item>
<item name="android:navigationBarColor">@color/colorBackgroundBlack</item>
<item name="actionMenuTextColor">@color/colorBackgroundBlackDark</item>
<item name="colorPrimary">@color/colorBackgroundBlackDark</item>
<item name="colorAccent">@color/colorPrimaryDark</item>
<item name="colorButtonNormal">@color/ipBlue</item>
<item name="toolbarNavigationButtonStyle">@color/ipGreen</item>
</style>
<style name="ActionBar.Solid.TMSA.NoTitle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="displayOptions">useLogo|showHome</item>
<item name="logo">@drawable/ic_ipaustralialogo</item>
<item name="android:contentDescription">@string/ip_logo</item>
</style>
<style name="AppTheme.TMSA" parent="@style/AppTheme">
<item name="actionBarStyle">@style/ActionBar.Solid.TMSA.NoTitle</item>
</style>
除了包含政府徽标外,我不记得触摸过 ActionBar
的格式布局,但我无法弄清楚为什么我会看到这个歪斜的菜单图标。
我已经考虑过采用 Toolbar
方法,但不想转换 :P
编码愉快:)
ActionBarDrawerToggle
在 ActionBar
的导航按钮上设置其切换图标。在 AppCompatActivity
中,ActionBar
实际上是下方的 Toolbar
,并且该导航按钮的样式设置为在主题的 toolbarNavigationButtonStyle
属性上设置的 style
资源。
在您的主题中,您在该属性上设置了 color
资源,而不是 style
资源,默认样式中的所有值都将丢失,包括 minWidth
值,这就是为什么你的切换被包裹到 drawable 的宽度。
如果你想修改导航按钮上的一些样式值,你应该创建自己的style
资源,默认style
作为它的parent
,设置所需的属性在那里,并将 style
指定为您的主题 toolbarNavigationButtonStyle
。例如:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="toolbarNavigationButtonStyle">@style/Toolbar.Button.Navigation</item>
</style>
<style name="Toolbar.Button.Navigation" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="android:background">@color/ipGreen</item>
</style>
如果您实际要修改的是汉堡包箭头可绘制对象,它有自己的样式,您可以 "sub-style" 并更改其中的某些功能。例如:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="drawerArrowStyle">@style/DrawerArrowToggle</item>
</style>
<style name="DrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="color">@color/ipGreen</item>
</style>
下面是 drawerArrowStyle
中可修改的完整属性列表,如果您想自定义其任何其他属性。
<!-- The drawing color for the bars -->
<attr name="color" format="color"/>
<!-- Whether bars should rotate or not during transition -->
<attr name="spinBars" format="boolean"/>
<!-- The total size of the drawable -->
<attr name="drawableSize" format="dimension"/>
<!-- The max gap between the bars when they are parallel to each other -->
<attr name="gapBetweenBars" format="dimension"/>
<!-- The length of the arrow head when formed to make an arrow -->
<attr name="arrowHeadLength" format="dimension"/>
<!-- The length of the shaft when formed to make an arrow -->
<attr name="arrowShaftLength" format="dimension"/>
<!-- The length of the bars when they are parallel to each other -->
<attr name="barLength" format="dimension"/>
<!-- The thickness (stroke size) for the bar paint -->
<attr name="thickness" format="dimension"/>