【问题标题】:ToolBar AppCompat & Action Bar backStackToolBar AppCompat & Action Bar backStack
【发布时间】:2015-02-19 23:43:47
【问题描述】:

我目前遇到了 AppCompat 工具栏的问题。

如果我将 Theme 设置为 NoActionBar,则不会显示任何操作。 如果我正常设置主题,我不能像这样设置操作栏:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
    setSupportActionBar(toolbar);
}

因为如果已经设置了动作栏,你就没有权限设置。

我只是想在我的片段导航上获得返回箭头,例如:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

我不知道该怎么做:/

【问题讨论】:

    标签: android material-design android-support-library android-toolbar android-appcompat


    【解决方案1】:

    试试下面的代码:

        getSupportActionBar().setHomeButtonEnabled(true);
        toolbar.setNavigationIcon(R.drawable.ic_back); 
    

    【讨论】:

    • 谢谢,但没有,问题是工具栏和操作栏都没有显示
    • 1)"Theme.AppCompat.Light.NoActionBar." 2)扩展ActionBarActivity。
    • 是的,这正是我正在做的。
    • 我使用的是 Theme.AppCompat.Light 和 false。
    【解决方案2】:

    【讨论】:

      【解决方案3】:

      你必须在你的风格中设置这个属性。

      <style name="AppTheme" parent="Theme.AppCompat.Light">
              <item name="windowActionBar">false</item>
      </style>
      

      然后在您的活动中:

      public class BaseActivity extends AppCompatActivity {
          @Override
          protected void onCreate(Bundle savedInstanceState) {   
               Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
               if (mActionBarToolbar != null) {
                  setSupportActionBar(mActionBarToolbar);
                  getSupportActionBar().setHomeButtonEnabled(true);
               }
          }
      }
      

      【讨论】:

      • 谢谢,但这正是我正在做的事情(以及所有互联网和文档所说的),但如果我这样做,当我调用 setDisplayHomeAsUpEnabled(true) 时,操作栏和工具栏都不会显示);
      • @ThomasLeduc 你确定 (mActionBarToolbar != null) 吗?
      • 嗯,其实不,这似乎太容易了,但我会尝试。你也许是对的:)
      猜你喜欢
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 2014-04-13
      • 1970-01-01
      • 2014-12-17
      相关资源
      最近更新 更多