Android Studio ActionBar宽度/图标位置 [英] Android Studio ActionBar width / icon locations
问题描述
我的 ActionBar
出现问题导致我的菜单图标被按到屏幕边缘!
I have a problem with my ActionBar
that results in my menu icon being pressed up against the edge of the screen!
以下是一些我调整过的样式的代码片段和声明:
Below is a few code snippets of the styles I've tweaked and the declarations:
HomeActivity.xml
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
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>
我不记得了除了包含gov徽标之外, ActionBar
的格式化布局,但我不知道为什么我会得到这个歪斜的菜单图标。
I don't remember touching the formatting layout for the ActionBar
apart from the inclusion of the gov logo but I can't otherwise figure out why I'm getting this askew menu icon.
我已经考虑过做一个工具栏
的方法但不想转换:P
I have already considered doing a Toolbar
approach but would prefer not to have to convert :P
快乐编码:)
推荐答案
ActionBarDrawerToggle
集它在 ActionBar
的导航按钮上的切换图标。在 AppCompatActivity
中, ActionBar
实际上是工具栏
下面,导航按钮的样式为主题 toolbarNavigationButtonStyle
属性设置的样式
资源。
ActionBarDrawerToggle
sets its toggle icon on the ActionBar
's navigation button. In an AppCompatActivity
, the ActionBar
is actually a Toolbar
underneath, and that navigation button is styled with the style
resource set on the theme's toolbarNavigationButtonStyle
attribute.
在您的主题中,您已在该属性上设置颜色
资源,而不是样式
资源,并且默认样式中的所有值都将丢失,包括 minWidth
值,这就是您的切换被包装到drawable的宽度的原因。
In your theme, you've set a color
resource on that attribute, rather than a style
resource, and all of the values in the default style are lost, including the minWidth
value, which is why your toggle is wrapped to the drawable's width.
如果要在导航按钮上修改某些样式值,则应创建自己的样式
资源,并使用默认值 style
作为 parent
,在那里设置所需的属性,并指定 style
作为主题的 toolbarNavigationButtonStyle
。例如:
If you want to modify some style values on the navigation button, you should create your own style
resource, with the default style
as its parent
, set the desired attributes there, and specify that style
as your theme's toolbarNavigationButtonStyle
. For example:
<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>
如果你真正试图修改的是汉堡包箭头可绘制,它有自己的风格,你可以子风格,并改变其中的某些功能。例如:
If what you're actually trying to modify is the hamburger-arrow drawable, it has its own style that you can "sub-style", and change certain features in that. For example:
<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中可修改的属性的完整列表
,如果您想自定义任何其他属性。
Below is the full list of attributes available for modification in a drawerArrowStyle
, should you want to customize any other of its properties.
<!-- 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"/>
这篇关于Android Studio ActionBar宽度/图标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!