繁体   English   中英

如何使用活动和片段在自定义工具栏中设置菜单项?

[英]How to set Menu Items in Custom Toolbar with a activity and a fragment?

之前我只使用活动并且有效,但是当我将片段添加到活动时,菜单图标消失了,我可以找到修复它

我的自定义工具栏

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/apptheme"
    app:theme="@style/AlertDialog.AppCompat.Light">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:weightSum="100">

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/actionbar_logo_click"
            android:paddingLeft="3dp"
            android:layout_weight="5">

            <ImageView
                android:id="@+id/actionbar_logo"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:paddingRight="10dp" />
        </FrameLayout>

        <TextView
            android:id="@+id/actionbar_titel"
            style="@style/AudioFileInfoOverlayText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingTop="7dp"
            android:textColor="@color/white"
            android:textSize="27dp" />
    </LinearLayout>
</android.support.v7.widget.Toolbar>

我的菜单项

<?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"
    android:icon="@drawable/ic_more_vert_white_24dp"
    app:showAsAction="always">
    <item
        android:id="@+id/menu_rate"
        android:title="@string/menu_rate"
        />
    <item
        android:id="@+id/menu_about"
        android:title="@string/menu_about" />

</menu>

我的活动 xml 布局,我在其中添加了带有 id 的自定义工具栏

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/app_bar"></include>

    <!-- Container for loading and the actual fragment-->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />



    <ProgressBar
        android:id="@+id/loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone"
        android:indeterminate="true" />
</LinearLayout>
</FrameLayout>

我的 java 活动,我在其中调用工具栏并增加菜单项

@EActivity(R.layout.activity_main_layout)
public class MainActivity extends BaseActivity {


    @ViewById(R.id.adview)
    MoPubView mMoPubView;

    @ViewById(R.id.app_bar)
    Toolbar toolbar;

    @ViewById(R.id.actionbar_logo)
    ImageView mAactionbarImage;

    @ViewById(R.id.actionbar_titel)
    TextView mActionbarTitel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSupportActionBar(toolbar);
    }

    @AfterViews
    protected void onViewsInitialized() {
        gotoFragment(MainFragment_.builder().build(), false);

        mAactionbarImage.setImageResource(R.mipmap.ic_launcher);

        //Set the titel of the actionbar
        mActionbarTitel.setText("How To Twitch");
    }


    @Override
    public void onBackPressed() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        menu.getItem(0).setIcon(R.drawable.ic_more_vert_white_24dp);
        return super.onCreateOptionsMenu(menu);

    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_rate:
                final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
                try {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
                } catch (android.content.ActivityNotFoundException anfe) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
                }
                break;
            case R.id.menu_about:
                Intent aboutIntent = new Intent(MainActivity.this, AboutActivity_.class);
                startActivity(aboutIntent);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

出于某种原因,我的菜单图标没有显示在工具栏中,所以如果有人知道我做错了什么,那会对我有很大帮助

我认为您缺少onPrepareOptionsMenu的重写方法

尝试添加这个覆盖的方法并检查:

@Override
public void onPrepareOptionsMenu(Menu menu) {
    menu.findItem(R.id. menu_rate).setVisible(true);
    menu.findItem(R.id. menu_about).setVisible(true);
    super.onPrepareOptionsMenu(menu);
}

并添加setHasOptionsMenu(true); 在片段的onCreateView()中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM