2016-04-29 88 views
1

這對我來說有點意外。我有以下佈局文件:Button和EditText上的Android視圖不會遮擋它們

<?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到的兩種觀點(該按鈕和EditText上的仿真結果是模糊的部分正是:

emulator screenshot

然而該裝置(A摩托3G第三代),該按鈕顯示在的LinearLayout的頂部:

result on the device

此外,我仍然可以在設備和仿真器上的LinearLayout應該遮擋的部分點擊按鈕和EditText。

clicking on the controls behind the linear layout

我的問題,然後是:我怎樣才能使線性佈局出現在設備上的按鈕的頂部?我怎樣才能防止LinearLayout上的水龍頭被重定向到按鈕並編輯文本?

謝謝。

爲了以防萬一,我使用Xamarin for Android,但它不應該是相關的。

回答

2

如何讓線性佈局出現在設備上的按鈕上?

高程是什麼導致Button出現在LinearLayout的前面。海拔較高的視圖出現在前面。它在模擬器中工作,因爲您正在使用Android的預先材料設計版本。

參見:http://developer.android.com/training/material/shadows-clipping.html

「設置在一個佈局定義視圖的標高,使用android:elevation屬性要設置在一個活動的代碼的圖的高度,可使用​​方法。」

如何防止LinearLayout上的輕按被重定向到按鈕並編輯文本?

通過在LinearLayout上使用android:clickable="true"。原因是,如果LinearLayout無法點擊,則點擊最終會被分配到更遠的視圖。

+0

謝謝。向LinearLayout添加android:elevation =「10dp」可修復按鈕重疊。但是線性佈局上的點擊仍然由按鈕或編輯文本處理。你知道如何解決這個問題嗎? –

+0

很確定問題在於'LinearLayout'沒有捕獲點擊事件,所以他們被分派到更遠的視圖。你有沒有試過在LinearLayout上設置'android:clickable =「true」'? – Nathanael

+0

是的,添加這兩個屬性解決了這兩個問題。非常感謝你。 –