Button 和 EditText 之上的 Android View 不会遮挡它们

标签 android android-layout

这个有点出乎我的意料。我有以下布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <Button
    android:id="@+id/MyButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Hello" />
  <EditText
    android:id="@+id/MyEditText"
    android:layout_below="@id/MyButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Hello" />
  <LinearLayout
    android:layout_centerInParent="true"
    android:layout_width="100dp"
    android:layout_height="match_parent"
    android:background="#ffffffff">
  </LinearLayout>
</RelativeLayout>

我期望 LinearLayout 遮盖两个 View 的一部分(按钮和 EditText。模拟器中的结果正是这样:

emulator screenshot

但是在设备(第 3 代 Moto G)上,按钮显示在 LinearLayout 的顶部:

result on the device

此外,我仍然可以在应该被设备和模拟器上的 LinearLayout 遮盖的部分上点击按钮和 EditText。

clicking on the controls behind the linear layout

我的问题是:如何使线性布局显示在设备按钮的顶部?以及如何防止 LinearLayout 上的点击被重定向到按钮和编辑文本?

谢谢。

以防万一,我正在使用适用于 Android 的 Xamarin,但它不应该是相关的。

最佳答案

如何使线性布局显示在设备按钮的顶部?

Elevation 是导致 Button 出现在 LinearLayout 前面的原因。具有更高海拔的 View 出现在前面。它在模拟器中工作,因为您使用的是 Android 的前 Material 设计版本。

参见:http://developer.android.com/training/material/shadows-clipping.html

“要在布局定义中设置 View 的高度,请使用 android:elevation 属性。要在 Activity 代码中设置 View 的高度,请使用 View .setElevation() 方法。”

如何防止 LinearLayout 上的点击被重定向到按钮和编辑文本?

通过在 LinearLayout 上使用 android:clickable="true"。原因是,如果 LinearLayout 不可点击,点击最终会发送到更靠后的 View 。

关于Button 和 EditText 之上的 Android View 不会遮挡它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36943655/

相关文章:

android - 在我的案例中如何膨胀 Activity ?

java - 无法在 Android 应用程序中使用 javax 发送电子邮件

java.lang.IllegalStateException : Not connected. 调用 connect() - YouTube API

Android - 自定义抽屉导航背景列表滚动/聚焦

android - ViewHolder Android 的 onTouch 方法

android - ActionMode Bar 的所有图标都没有在 Android 中显示?

android - 如何设计自定义 Android 控件并在 Xml 中使用它?

android - 将 native 底部菜单添加到 phonegap 应用程序 for android

java - Retrofit 2 展开信封错误

android - 带有BottomSheet的windowSoftInputMode不起作用