工具栏自定义视图无法点击

时间:2015-11-10 08:00:37

标签: android android-custom-view android-toolbar android-actionbar-compat

我需要你的帮助!我正在进行this post

我有一个layout_header

<RelativeLayout
    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="?attr/actionBarSize"
    android:background="@drawable/bg_shadow_blue">

    <com.andexert.library.RippleView
        android:id="@+id/rippleLeft"
        style="@style/ripple"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        app:rv_type="rectangle">

        <ImageView
            android:id="@+id/ivBack"
            style="@style/imageview_normal"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:src="@mipmap/ic_back"
            android:visibility="gone"/>
    </com.andexert.library.RippleView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/rippleRight"
        android:layout_toRightOf="@+id/rippleLeft">

        <TextView
            android:id="@+id/tvHeader"
            style="@style/text_header"
            android:layout_centerInParent="true"
            android:gravity="center"/>

        <ImageView
            android:id="@+id/ivHeader"
            style="@style/imageview_normal"
            android:layout_centerInParent="true"
            android:paddingBottom="5dp"
            android:src="@mipmap/ic_logo"
            android:visibility="gone"/>

    </RelativeLayout>

    <com.andexert.library.RippleView
        android:id="@+id/rippleRight"
        style="@style/ripple"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        app:rv_type="rectangle">

        <TextView
            android:id="@+id/tvMore"
            style="@style/imageview_normal"
            android:layout_height="match_parent"
            android:drawableLeft="@mipmap/ic_more"
            android:gravity="center_vertical"
            android:padding="10dp"
            android:textColor="@color/white"
            android:visibility="invisible"/>
    </com.andexert.library.RippleView>

</RelativeLayout>

然后将其包含在主要布局中:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/blue"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <include layout="@layout/layout_header"/>
    </android.support.v7.widget.Toolbar>

然后在Main class中,我有:

setContentView(R.layout.activity_main_1);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.slidingMenu);
        drawerView = (ScrollView) findViewById(R.id.sv_slidingMenu);
        mDrawerToggle = new BXHActionBarDrawerToggle(this, drawerLayout, 0, 0);

        // Set the drawer toggle as the DrawerListener
        drawerLayout.setDrawerListener(mDrawerToggle);

        //------Custom ActionBar
        ActionBar actionBar = getSupportActionBar();

        mIvHeader = (ImageView) findViewById(R.id.ivHeader);
        mTvMore = (TextView) findViewById(R.id.tvMore);
        mTvHeader = (TextView) findViewById(R.id.tvHeader);
        mIvBack = (ImageView) findViewById(R.id.ivBack);
        mIvBack.setVisibility(View.GONE);

        actionBar.setHomeAsUpIndicator(R.mipmap.ic_menu);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
mTvMore.setOnClickListener(this);

Main class中的一些相关代码:

@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (mFragmentStack.size() > 1) {
                    onBackPressed();
                    return true;
                }
        }
        // Pass the event to ActionBarDrawerToggle, if it returns true, then it has handled the app icon touch event
        return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
    }

我的问题是,mTvMore未收到onClickListener事件。我尝试了许多方式"toolbar.setNavigationOnClickListener();mDrawerToggle.setToolbarNavigationClickListener();",但它无效。请帮帮我,谢谢

2 个答案:

答案 0 :(得分:0)

将return语句转换为`switch(item.getItemId()){         case R.id.action_add:             //在这里调用方法或直接执行功能             //你的代码             return true;

    default:
        return super.onOptionsItemSelected(item);
    }`

希望这对你有用

答案 1 :(得分:0)

也许您的事件被您使用的RippleView消耗。试着用rippleRight来接收onclick事件?