当上下文操作模式覆盖在 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);
}
});
}
活动主文件
<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>
样式.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);
此时,我只能想到要么在上下文操作模式打开时显式禁用自定义组件,要么以某种方式用不可见的垃圾填充上下文操作栏以更严重地遮挡工具栏。
下面是一些截图。
在单击任何内容之前:
单击“SomeTextToClick”后,上下文操作模式现已打开,工具栏中的文本被遮挡,但此时我可以单击上下文操作栏的中心,并且该单击由“Bar”上的侦听器处理" 工具栏中的 TextView:
有人可以帮忙吗?