2015-04-22 58 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> 

现在,您只需显示/隐藏覆盖的物品,即可轻松前往。