工具栏内的图标无法点击

时间:2018-02-09 08:53:14

标签: android

这是我的main.xml文件 -

<RelativeLayout 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"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.pawan.googlesignin.SongsActivity">
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:titleTextColor="@color/colorAccent"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.design.widget.TabItem
            android:id="@+id/tabItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="live music" />
        <android.support.design.widget.TabItem
            android:id="@+id/tabItem1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="scheduled" />
    </android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /></RelativeLayout>

这是我的items.xml

<?xml version="1.0" encoding="utf-8"?>

<item android:id="@+id/share"
    android:title="share"
    android:orderInCategory="100"
    app:showAsAction="ifRoom" />

<item android:id="@+id/feedback"
    android:title="feedback"
    android:orderInCategory="100"
    app:showAsAction="ifRoom" />

<item android:id="@+id/profile"
    android:title="profile"
    android:orderInCategory="100"
    app:showAsAction="ifRoom" />

这是我的MainActivity.xml -

public class SongsActivity extends AppCompatActivity {
private static final String TAG="MainActivity";
private SectionsPageAdapter mSectionPageAdapter;
private ViewPager mViewpager;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_songs);
    Log.d(TAG, "onCreate:starting");
    mSectionPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
    mViewpager =findViewById(R.id.container);
    setupViewPager(mViewpager);
    TabLayout tabLayout = findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewpager);
    toolbar =findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setTitle("My app");

}



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent intent = new Intent(this, VenuesActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
public void setupViewPager(ViewPager viewPager){
    SectionsPageAdapter adapter=new SectionsPageAdapter(getSupportFragmentManager());
    adapter.addFragment(new live(),"live music");
    adapter.addFragment(new scheduled(),"scheduled");
    viewPager.setAdapter(adapter);
}

}

enter image description here

朋友们,我无法点击后退导航按钮,也无法点击工具栏右上角的三个点。 请给出一个解决方案,我已经尝试了这里提供的所有解决方案。 我在这里找不到任何错误。请帮帮我......

2 个答案:

答案 0 :(得分:-1)

使用此功能来处理操作栏上的后退按钮。

case android.R.id.home:
    super.onBackPressed(); 
    return true;

答案 1 :(得分:-1)

  

您好,我找到了您的查询解决方案

     

请更新您的代码

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            System.out.println("back button pressed");

            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
            return true;
        case R.id.share:
            System.out.println("share button pressed");
        case R.id.feedback:
            System.out.println("feedback button pressed");
        case R.id.profile:
            System.out.println("profile button pressed");
        default:
            return super.onOptionsItemSelected(item);
    }
}

您忘记调用finish()方法并定义菜单项的点击监听器。 如果您需要更多帮助,请询问我。