【问题标题】:Android Studio ActionBar width / icon locationsAndroid Studio ActionBar 宽度/图标位置
【发布时间】:2017-11-06 05:25:08
【问题描述】:

我的ActionBar 有问题,导致我的菜单图标被按到屏幕边缘!

下面是我调整的样式和声明的一些代码 sn-ps:

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>

除了包含 gov 徽标之外,我不记得触摸过 ActionBar 的格式布局,但我无法弄清楚为什么会出现这个歪斜的菜单图标。

我已经考虑过使用Toolbar 方法,但不希望必须转换:P

编码愉快:)

【问题讨论】:

  • toolbarNavigationButtonStyle 采用一种风格,而不仅仅是一种颜色。我猜想它失败了。至少,它没有获得默认样式中设置的最小宽度值。如果要修改该样式,则应创建自己的样式,并将默认样式作为父样式,并在其中设置所需的属性; this answer 的第一个代码块中显示了类似的内容。
  • 没问题。顺便说一句,我不确定您要在哪个部分上色,但是动画汉堡箭头可绘制对象有自己的样式,您也可以修改。 this answer 中有一个例子。
  • @MikeM。即使没有调整汉堡包/箭头!我会试一试:)也 - 很抱歉打扰你,但我试图将你的第一条评论标记为正确答案,但我什至不能投票给你 - 是因为我是新手还是..?
  • 是的,我认为在达到 15 个代表之前你不能投票。此外,您不能将评论标记为答案。不过,给我一点,我会在下面整理一个实际答案,你可以用复选标记接受。如果这确实是您要修改的内容,我将包含您可以在可绘制对象上设置的属性列表。很高兴你修好了它。干杯!

标签: java android menu android-actionbar icons


【解决方案1】:

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>

如果您实际上要修改的是汉堡箭头可绘制对象,它有自己的样式,您可以“子样式”,并更改其中的某些功能。例如:

<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"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多