菜单项在片段中不可见

时间:2018-11-19 13:36:56

标签: android android-fragments android-menu androidx

我想在我的片段中显示一个菜单。我已经完成了一百万遍了,但是由于某种原因,它现在不起作用。这是我第一次尝试使用新的 androidx库,也许会出错吗?

片段:

public class FragmentDogs extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_dogs, container, false);

    setHasOptionsMenu(true);

    return rootView;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    inflater.inflate(R.menu.menu_dogs, menu);

    }
}

活动:

public class ActivityMain extends AppCompatActivity {

Toolbar toolbar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FragmentDogs()).commit();
    }
}

菜单:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/dogs_add_menu_item"
        android:icon="@drawable/ic_plus"

        android:title="@string/dogs"  />

成绩:

 android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.platinum"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'


    implementation 'com.google.android.gms:play-services-maps:16.0.0'


    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

截屏:

enter image description here

2 个答案:

答案 0 :(得分:0)

您必须将自定义toolbar设置为片段布局,并设置为SupportActionBar

您的片段布局

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:elevation="5dp"
    android:minHeight="?attr/actionBarSize"
    app:contentInsetEnd="50dp"
    app:contentInsetRight="50dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:title="testtext"
    app:contentInsetStartWithNavigation="0dp">
</android.support.v7.widget.Toolbar>

您的片段类

public class YourFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.yourfragmentlayout, container, false);
    setHasOptionsMenu(true);

    Toolbar toolbar = (Toolbar)rootView.findViewById(R.id.toolbar);
    ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
    ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("YourFragment");

    return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_dogs, menu);
    super.onCreateOptionsMenu(menu, inflater);
}
}

如果 YourFragment()带红色下划线

enter image description here

确保您正在使用

  

import android.support.v4.app.Fragment;

代替

  

import android.app.Fragment;

答案 1 :(得分:0)

您缺少menu.xml上的结束标记,该标记可能以</menu>结尾。

testInstrumentationRunner和其他测试依赖项都不是androidx的正确测试运行程序。 androidx.fragment:fragment:1.0.0Fragment类的适当依赖项。请参阅migration guide了解更多androidx依赖关系,您可能会缺少这些依赖关系;与androidx.appcompat:appcompat:1.0.2相同AppCompatActivity

它可能是X/Y problem ...首先修复依赖关系-然后修复菜单。