代码之家  ›  专栏  ›  技术社区  ›  Danielle Orth

Android Studio操作栏宽度/图标位置

  •  3
  • Danielle Orth  · 技术社区  · 8 年前

    我的手机有问题 ActionBar 这导致我的菜单图标被压在屏幕边缘! image

    下面是我调整过的样式和声明的一些代码片段:

    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>
    

    我不记得触摸过 操作栏 除了包含政府标志外,我不知道为什么我会得到这个歪斜的菜单图标。

    我已经考虑过做一个 Toolbar 方法,但不希望转换:P

    快乐编码:)

    1 回复  |  直到 8 年前
        1
  •  1
  •   Mike M.    8 年前

    ActionBarDrawerToggle 将其切换图标设置为 ActionBar 的导航按钮。在一个 AppCompatActivity 这个 实际上是一个 Toolbar 下面,导航按钮的样式为 style 主题上的资源集 toolbarNavigationButtonStyle 属性

    在你的主题中,你设置了一个 color 资源,而不是 默认样式中的所有值都将丢失,包括 minWidth 值,这就是为什么您的切换被包装到可绘制的宽度。

    如果要修改导航按钮上的某些样式值,则应创建自己的样式值 风格 资源,默认为 风格 作为其 parent ,在那里设置所需的属性,并指定 风格 作为你的主题 工具栏导航按钮样式 . 例如:

    <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"/>
    
    推荐文章