2015-04-22 61 views
1

填寫頁面我有一個TRANSPARENT覆蓋在我的android應用程序,當它的用戶點擊,它褪色,但它不能填補所有活動像下面的圖片如何使用覆蓋

enter image description here

MainActivity:

<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" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="This is Original View" /> 

</RelativeLayout> 

OverlayActivity:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<ImageView 
    android:id="@+id/over_lay_image" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#50220000" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="30dp" 
    android:text="This is Overlay View" /> 

</RelativeLayout> 

Java:

public class MainActivity extends Activity { 

private ImageView mOverLayImage; 

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

    final Dialog overlayInfo = new Dialog(MainActivity.this); 
    overlayInfo.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    overlayInfo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
    overlayInfo.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 
    overlayInfo.setContentView(R.layout.overlay_view); 
    overlayInfo.show(); 

    mOverLayImage = (ImageView) overlayInfo.findViewById(R.id.over_lay_image); 
    mOverLayImage.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      overlayInfo.cancel(); 
     } 
    }); 


} 
} 
+0

您不需要創建其他活動來製作疊加層。使用FrameLayout –

+0

@BojanKseneman:請給我看一個示例 –

回答

1

刪除您覆蓋的活動,和您的主要活動中使用這個代碼:

<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" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="This is Original View" /> 

<!-- This is your overlay --> 
<RelativeLayout android:id="@+id/over_lay_page" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ImageView 
    android:id="@+id/over_lay_image" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#50220000" 
    android:onClick="clickedOverlay" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="30dp" 
    android:text="This is Overlay View" /> 

</RelativeLayout> 

</RelativeLayout> 

請注意,我說你的ImageView線,當點擊,現在你的java文件中添加此功能運行的功能:

//The onClick on xml requires a function of signature void(View) which is the clicked view (in this case the ImageView) 
public void clickedOverlay(View view) 
{ 
    //ImageView is clicked 
    RelativeLayout rlLayout = (RelativeLayout) findViewById(R.id.over_lay_page); 
    rlLayout.setVisibility(View.GONE); 
} 

這將使包含覆蓋意見(包括被點擊ImageView)不僅是無形的,但不與任何干擾RelativeLayout。它也忽略了對它的投入。

如果我誤解了你的問題,請隨時糾正我(我不確定我完全理解了這一點)。

此外,如果你想它淡入或淡出或類似的東西,你可以用AlphaAnimation做到這一點。

+0

謝謝you.it的工作 –

2

使用FrameLayout。添加到FrameLayout的每個項目都位於前一個項目的頂部,就像本例中的第二個TextView位於第一個項目的頂部,但由於它不完全不透明,您可以同時看到它們!

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center_horizontal" 
     android:text="Blablabla" 
     android:background="#FFFFFFFF"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#50220000" 
     android:textColor="#FFFFFF" 
     android:text="I am on top" 
     android:gravity="center" 
     /> 

</FrameLayout> 

現在,您只需顯示/隱藏覆蓋的物品,即可輕鬆前往。