ListView项工具栏的显示溢出菜单,但不显示活动工具栏

时间:2017-05-23 23:56:53

标签: android

在ActionBar上,右侧有一个溢出菜单(三点式弹出菜单)。我不想为活动本身显示ActionBar。事实上,我从所有活动中删除了ActionBar:

RES / styles.xml:

<resources>
  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  </style>
</resources>

取而代之的是我的Activity,我使用ToolBar(我自定义):

activity_main.xml中:

<RelativeLayout ... android:fitsSystemWindows="true" >
      <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary">
        <ImageView android:layout_alignParentTop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/yourStory" android:src="@drawable/ic_face_black_24dp" />
    </android.support.v7.widget.Toolbar>

我不希望主活动的工具栏显示溢出菜单,所以我注释掉这一行:

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        MenuInflater inflater = getMenuInflater();
//        inflater.inflate(R.menu.main_menu, menu);
//        return true;
//    }   

现在在ToolBar下面,我有一个ListView:

<ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:layout_centerHorizontal="true" android:id="@+id/listView" />

我为每个ListView项目都有一个自定义布局,它有自己的ToolBar:

item_list.xml

<android.support.v7.widget.Toolbar
        android:id="@+id/listToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/white">


    <ImageView
            android:id="@+id/usericon"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
    <TextView
            android:id="@+id/username"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
</android.support.v7.widget.Toolbar>

问题是我想要显示这些项目的溢出菜单。但它没有显示,因为我认为我注释了主工具栏。如何为ListView项目而不是主活动工具栏显示溢出菜单? (查看Instagram上的ListView。每个ListView项目都有弹出菜单。)

1 个答案:

答案 0 :(得分:0)

您不需要使用<div class="card"> <div class="tag">New</div> </div>来实现此目的。工具栏不能用作可重复列表项。

您可以使用PopupMenu来实现您正在寻找的效果。

Toolbar可用于附加任何给定视图的菜单。因此,在PopupMenu内删除工具栏,只需使用item_list.xml创建常规版面以显示菜单

ImageButton

创建弹出菜单

   <LinearLayout>
    <ImageView
            android:id="@+id/usericon"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
    <TextView
            android:id="@+id/username"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />

    <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_overflow_holo_dark"
            android:contentDescription="@string/descr_overflow_button"
            android:id="@+id/overflow_menu" />
   </LinearLayout>

接下来将imageButton = findViewById(R.id.overflow_menu); PopupMenu popup = new PopupMenu(this, imageButton); MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(R.menu.actions, popup.getMenu()); 附加到OnClickListener并在其中调用ImageButton