Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在具有自定义布局的片段中使用选项菜单_Java_Android_Android Layout_Android Fragments - Fatal编程技术网

Java 如何在具有自定义布局的片段中使用选项菜单

Java 如何在具有自定义布局的片段中使用选项菜单,java,android,android-layout,android-fragments,Java,Android,Android Layout,Android Fragments,如何在片段中使用选项菜单中的自定义布局 当我点击按钮时,什么也没发生(没有显示烤面包片),但当我移除时 app:actionLayout="@layout/layout_button" 一切正常(显示烤面包片)。如何解决这个问题 下面是我正在使用的代码 在onCreate()中: 在onCreateOptions菜单()中: 在选项ItemSelected()中 在XML菜单中: <menu xmlns:android="http://schemas.and

如何在片段中使用选项菜单中的自定义布局

当我点击按钮时,什么也没发生(没有显示烤面包片),但当我移除时

app:actionLayout="@layout/layout_button"
一切正常(显示烤面包片)。如何解决这个问题

下面是我正在使用的代码

onCreate()
中:

onCreateOptions菜单()中

在选项ItemSelected()中

在XML菜单中:

<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/itemPay"
            android:title="Pay"
            app:actionLayout="@layout/layout_button"
            app:showAsAction="always"/>
</menu>

在XML布局中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/r_l_pay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <com.google.android.material.button.MaterialButton
        android:id="@+id/btnPay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:layout_gravity="right"
        android:gravity="top|center_horizontal"
        android:text="Pay"
        android:textSize="11sp"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:theme="@style/AppThemeMatetrialButtonRed" />
</RelativeLayout>

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.itemPay:
            Toast.makeText(cxtx, "Hello", Toast.LENGTH_SHORT).show();
            return true;
        default:
            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/itemPay"
            android:title="Pay"
            app:actionLayout="@layout/layout_button"
            app:showAsAction="always"/>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/r_l_pay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <com.google.android.material.button.MaterialButton
        android:id="@+id/btnPay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:layout_gravity="right"
        android:gravity="top|center_horizontal"
        android:text="Pay"
        android:textSize="11sp"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:theme="@style/AppThemeMatetrialButtonRed" />
</RelativeLayout>