2015-06-13 40 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