这个有点出乎我的意料。我有以下布局文件:
<?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。模拟器中的结果正是这样:
但是在设备(第 3 代 Moto G)上,按钮显示在 LinearLayout 的顶部:
此外,我仍然可以在应该被设备和模拟器上的 LinearLayout 遮盖的部分上点击按钮和 EditText。
我的问题是:如何使线性布局显示在设备按钮的顶部?以及如何防止 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/