上下文操作模式不过滤触摸

时间:2015-04-18 06:59:26

标签: android android-5.0-lollipop android-toolbar contextual-action-bar appcompat-v7-r21

上下文操作模式,当重叠在android.support.v7.widget.Toolbar之上时,似乎不会过滤整个宽度上的触摸,但让触摸"通过"到工具栏上的(不可见)小部件。

我的工具栏包含一个自定义窗口小部件(工具栏声明的好处之一)。我将上下文操作模式设置为覆盖工具栏,并将其完全隐藏。但是,当我触摸(现在模糊的)自定义小部件所在的位置时,触摸会传递给它。我想知道这是不是一个错误/疏忽,但我可能没有把握这个权利,或者我的期望可能是崇高的。我假设如果隐藏的自定义组件的位置中的上下文操作覆盖有某些内容,则触摸不会通过。

我在Kitkat上测试这个。

以下是一些示例代码:

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.actionbar_toolbar);
    setSupportActionBar(toolbar);

    TextView barTextView = (TextView) toolbar.findViewById(R.id.textview_bar);
    barTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Bar TextView clicked", Toast.LENGTH_SHORT).show();
        }
    });

    mActionModeCallback = new ActionModeCallback();

    TextView mainTextView = (TextView) findViewById(R.id.textview_main);
    mainTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Main TextView clicked", Toast.LENGTH_SHORT).show();
            if (mActionMode != null) {
                return;
            }
            getSupportActionBar().startActionMode(mActionModeCallback);
        }
    });
}

activity_main.xml中

<LinearLayout 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="match_parent"
              android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/actionbar_toolbar"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
        <TextView
            android:id="@+id/textview_bar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Bar"
            android:textColor="#FF0000"/>
    </android.support.v7.widget.Toolbar>
    <TextView
        android:id="@+id/textview_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SomeTextToClick"/>
</LinearLayout>

styles.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="windowActionModeOverlay">true</item>
    </style>
</resources>

我还尝试使用以下替代方案设置操作模式,但行为是相同的:

Toolbar toolbar = (Toolbar) MainActivity.this.findViewById(R.id.actionbar_toolbar);
mActionMode = toolbar.startActionMode(mActionModeCallback);

此时我只能想到在上下文操作模式出现时显式禁用自定义组件,或者以某种方式用不可见的垃圾填充上下文操作栏以更严重地模糊工具栏。

下面的一些截图。

点击任何内容之前:

No contextual action mode before clicking

点击&#34; SomeTextToClick&#34; - 现在开启了上下文操作模式,并且工具栏中的文本被遮挡,但此时我可以单击上下文操作栏的中心,并且点击将由&#34; Bar&#34上的监听器处理;工具栏中的TextView:

Contextual action mode enabled after clicking "SomeTextToClick"

有人可以帮忙吗?

0 个答案:

没有答案
相关问题