2015-06-13 78 views
1

我試圖隱藏某些佈局中的某些元素,當Android離開焦點時,這樣在「查看最近的應用程序」顯示中查看時,佈局元素將不可見。選擇應用程序並進入焦點後,這些元素將再次可見。如何在Android活動失焦時隱藏或顯示佈局元素?

我的下面的實現嘗試顯示應用程序焦點時的「內容」佈局,並通過onWindowFocusChanged()顯示焦點之外的「疊加」佈局。

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 
    <RelativeLayout 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="This is the content page"/> 
    </RelativeLayout> 
    <RelativeLayout 
     android:id="@+id/overlay" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="This is the overlay"/> 
    </RelativeLayout> 
</FrameLayout> 

MainActivity.java

public class MainActivity extends ActionBarActivity { 

    FrameLayout layoutContainer; 
    RelativeLayout layoutContent; 
    RelativeLayout layoutOverlay; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     layoutContainer = (FrameLayout) findViewById(R.id.container); 
     layoutContent = (RelativeLayout) findViewById(R.id.content); 
     layoutOverlay = (RelativeLayout) findViewById(R.id.overlay); 

    } 

    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 

     if (hasFocus) { 
      layoutContent.setVisibility(View.VISIBLE); 
      layoutOverlay.setVisibility(View.GONE); 
     } else { 
      layoutOverlay.setVisibility(View.VISIBLE); 
      layoutContent.setVisibility(View.GONE); 
     } 

    } 
} 
enter code here 

然而,我的實現不工作。任何人有任何建議來解決這個問題?謝謝!

回答

0

如果你想隱藏在最近用過的觀點在佈局嘗試這樣

getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,WindowManager.LayoutParams.FLAG_SECURE); 
+0

我想你的解決方案,它的工作原理,但它阻止用戶截屏。有沒有其他解決方案可以使用屏幕截圖?謝謝。 – zwift