我按如下方式打开PopupMenu:
@Override
public void onClick(View v) {
if(v.getId() == R.id.action_reading) {
PopupMenu popup = new PopupMenu(context, v);
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem arg0) {
System.out.println("------ click menu");
return false;
}
});
popup.getMenuInflater().inflate(R.menu.action_reading_menu, popup.getMenu());
popup.show();
}
}
在action_reading
xml:
<Button android:id="@+id/action_reading"
android:layout_width="30dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
在action_reading_menu
xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/delete_item_reading"
android:title="Delete"
android:visible="true"></item>
</menu>
我为action_reading
设置了高度,但仍然出现以下错误:
java.lang.RuntimeException: Binary XML file line #17: You must supply a layout_height attribute.
任何人都知道为什么我收到此错误消息并解决它?
更新所有xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="2dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="2dp" >
<android.support.v7.widget.CardView
android:id="@+id/cv_benefit"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="110dp"
android:background="@color/white"
android:padding="5dp"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="@+id/title_story_list_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/action_reading"
android:layout_toRightOf="@+id/cover_image_reading"
android:paddingLeft="5dp"
android:text="title story"
android:textColor="@color/black"
android:textSize="16sp" />
<ImageView
android:id="@+id/cover_image_reading"
android:layout_width="80dp"
android:layout_height="100dp"
android:src="@drawable/img_bg" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/title_story_list_menu"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/cover_image_reading"
android:src="@drawable/ic_account_circle_grey_36dp" />
<TextView
android:id="@+id/author_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/title_story_list_menu"
android:layout_below="@+id/title_story_list_menu"
android:layout_toRightOf="@+id/imageView3"
android:paddingLeft="5dp"
android:text="ss"
android:textColor="@color/grey700" />
<TextView
android:id="@+id/parts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="78"
android:textColor="@color/black"
android:textSize="14sp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/cover_image_reading"
android:src="@drawable/ic_visibility_grey_36dp" />
<TextView
android:id="@+id/readed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignParentBottom="true"
android:layout_marginLeft="25dp"
android:layout_toLeftOf="@+id/imageView2"
android:text="123456789"
android:textColor="@color/black"
android:textSize="14sp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/parts"
android:src="@drawable/ic_book_grey_36dp" />
<Button
android:id="@+id/action_reading"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/ic_more_vert_grey600_18dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>