1

我在跟隨these instructions在我的Android應用中實現AppCompat Toolbar。我正在使用AppCompat版本7:22.0.1另外,除了actionBar之外,我還使用了此工具欄(它並不代替ActionBar,正如我在許多其他示例中看到的那樣)。Android AppCompat工具欄不響應觸摸動作項目

工具欄出現了,它甚至顯示了我指定的圖標,但當我觸摸圖標時它不響應。有沒有人有與AppCompat Toolbar類似的問題?有關下一步嘗試的建議?

下面是從我的瀏覽我的代碼()方法MainActivity.Java:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); 

// Set an OnMenuItemClickListener to handle menu item clicks 
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { 
    @Override 
    public boolean onMenuItemClick(MenuItem item) { 
     // Handle the menu item 
     switch (item.getItemId()) { 
      case R.id.browse_back_button: 
       Toast toast = Toast.makeText(context, text, duration); 
       toast.show(); 
       restart(view); 
      default: 
       return false; 
     } 
    } 
}); 

// Inflate a menu to be displayed in the toolbar 
toolbar.inflateMenu(R.menu.toolbar_menu); 

我把這個代碼片段在XML佈局文件,作爲第一個項目在RelativeLayout元素。

<android.support.v7.widget.Toolbar 
    android:id="@+id/my_awesome_toolbar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:minHeight="?attr/actionBarSize" 
    android:background="#FFFF00" /> 

Toolbar是從菜單佈局xml文件中誇大的。它由一個單一的「項目」元素的菜單元素:

<item android:title="Back" 
    android:id="@+id/browse_back_button" 
    android:icon="@drawable/ic_action_back" 
    app:showAsAction="always" /> 
+0

將工具欄上的佈局,所以它畫了一切LAST。相鄰ListView的填充是ListView的一部分,並消耗觸摸事件。 –

+0

@EugenPechanec你是對的。解決了這個問題。謝謝! –

回答

2

ListView控件繪製工具欄上的視圖的頂部,因此這是將得到的觸摸事件的看法。您應該將Toolbar作爲佈局中的最後一個元素,或者應該使用垂直LinearLayout,或者應該使用android:layout_below

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 

    <ListView 
     android:id="@+id/listViewBrowse" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="?attr/actionBarSize" /> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/my_awesome_toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:minHeight="?attr/actionBarSize" 
     android:background="#FFFF00" /> 

</RelativeLayout> 

 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/my_awesome_toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:minHeight="?attr/actionBarSize" 
     android:background="#FFFF00" /> 

    <ListView 
     android:id="@+id/listViewBrowse" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     /> 

</LinearLayout> 

 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/my_awesome_toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:minHeight="?attr/actionBarSize" 
     android:background="#FFFF00" /> 

    <ListView 
     android:id="@+id/listViewBrowse" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:layout_below="@id/my_awesome_toolbar" /> 

</RelativeLayout> 

 

我會用一個LinearLayout這裏,因爲它是更方便在這裏。它做你想做的事。將視圖旁邊對齊,而不是其他視圖。

(編輯:我明白了,這已經評論說,不知道爲什麼因爲這是一個問題的答案)

+0

我沒有意識到LinearyLayout在這裏更方便。它是有道理的,它運作良好。謝謝。 –