Android:GridView下面的按钮不会调用onClick

时间:2016-02-10 11:58:40

标签: android button gridview onclick

我有一个AppCompatActivity来实现NavigationView.OnNavigationItemSelectedListener

我的.xml布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activities.DocumentsActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@drawable/header"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

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

<android.support.v4.widget.DrawerLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/drawer_layout"
    tools:context=".activities.DocumentsActivity">

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>

我的content_documents.xml文件包含一个包含GridView的LinearLayout和一个底部的Button:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".activities.DocumentsActivity"
tools:showIn="@layout/activity_documents"
android:orientation="vertical"
android:weightSum="100">

<GridView
    android:id="@+id/gridView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="90"
    android:columnWidth="100dp"
    android:drawSelectorOnTop="true"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="5dp"/>

<Button
    android:id="@+id/button_add_document"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="10"
    android:text="ADD DOCUMENT"
    android:textColor="@android:color/white"
    android:textSize="18sp"
    android:textAlignment="center"
    android:gravity="center_vertical"
    android:background="@drawable/grid_color_selector"/>
</LinearLayout>

在我的活动中,我将onClickListener设置为Button,如下所示:

Button addDocument = (Button)findViewById(R.id.button_add_document);
    addDocument.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("Button", "Clicked");
        }
    });

但永远不会调用onClick()方法。

这可能是什么原因?

0 个答案:

没有答案