【问题标题】:ToolBar is Empty & inactive工具栏为空且不活动
【发布时间】:2015-11-13 20:45:20
【问题描述】:

使用此代码,

布局:

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true" >

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    />

  </RelativeLayout>

动作栏活动:

    final Toolbar ToolB = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(ToolB);
    ToolB.setTitle("ToolBar Test");
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

输出只是一个蓝色的空矩形,它不会对触摸事件做出反应,它就像一个图片。我也通过按菜单按钮(正在工作)获得一个菜单,但右上角没有三个点,什么都没有。

我遗漏了什么或做错了什么?

谢谢

【问题讨论】:

    标签: android material-design android-appcompat


    【解决方案1】:

    如果您想在按下主页按钮时转到上一个活动,您必须覆盖活动上的方法“onOptionsItemSelected(MenuItem item)”。示例:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
    
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    对于设置按钮(这三个点),您必须像这样创建一个菜单(文件):

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
    
        <item
             android:id="@+id/action_settings"
             android:orderInCategory="100"
             android:title="@string/action_settings"
             app:showAsAction="never" />
    </menu>
    

    并在活动上充气:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    

    希望这些例子对你有所帮助。

    【讨论】:

    • 谢谢,但这不是我想要的,我解决了我的问题,那是因为我有一种启动画面并且代码没有在刷新的视图中执行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多