簡體   English   中英

Android片段與以前的視圖和按鈕偵聽器重疊

[英]Android fragments overlap previous view and button listeners

我有一個活動A,里面有一個片段A。

活動A使用布局X,而片段A使用布局A。

布局X的代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<fragment
    android:id="@+id/fragment1"
    android:name="android.app.DialogFragment"
    android:layout_width="wrap_content"
    android:layout_height="500dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="113dp"
    class="com.example.fragtester.FragA" />

</RelativeLayout>

布局A只是textview + linearlayout。

我設置了另一個使用布局B的片段B。

現在,我在活動A中使用以下代碼來更改片段:

Fragment f = new FragB();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment1, f);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null); 
ft.commit();

我最終在布局A下顯示了布局B。

所以我使用FrameLayout將片段包裝在布局X中並使用

ft.replace(R.id.FrameLayout1, f);

現在,視圖運行良好。 但是,出現另一個問題。

雖然布局B覆蓋了布局A,但是按鈕仍然處於活動狀態。

這意味着當我查看布局B時,即使我沒有看到它,也仍然可以單擊布局A上的按鈕。

即使我添加片段C / D / E .....(布局C / D / E ....),布局A上的按鈕仍然處於活動狀態。

有人可以解釋為什么嗎? 我是否錯誤地使用了片段? 謝謝!

一種解決方法是將布局A設為空白,然后使用其他布局覆蓋它。 但這似乎不是“正確”的方式??

刪除片段並添加一個FrameLayout

   <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" >
    </FrameLayout>

然后以編程方式添加片段。

在android片段按鈕中,單擊通過片段(我不知道片段是否應該像這樣工作)。 在這種情況下,我通常要做的是使片段的布局可點擊。 因此點擊不會通過。

不要在xml中包含片段,而應嘗試為片段創建空容器。 例如空框架布局。 然后以編程方式將片段放入其中。

將以下屬性添加到位於頂部的片段的XML根布局中。

android:clickable="true"

這將確保觸摸事件不會傳播到頂層以外。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM