2013-05-02 16 views
0

我的佈局xml文件如下:如果可以做到像對話的setCanceledOnTouchOutside行爲在Android

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

    <ListView 
     android:id="@+id/action_bar_item_list" 
     android:layout_above="@+id/mybutton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

    <Button 
     android:id="@+id/mybutton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:text="Button" /> 

    <LinearLayout 
     android:id="@+id/messagelayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/message" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Large Text" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

    </LinearLayout> 

</RelativeLayout> 

如果myButton的被點擊,messagelayout將被設置爲可見。
而雖然messagelayout是可見的,我想做一個函數,如對話框的setCanceledOnTouchOutside做。
當用戶在messagelayout旁邊觸摸屏幕時,它將設置messagelayout消失,而不會做任何其他事情。
如果我不想使用對話框,我該怎麼做?

回答

1

您可以通過周圍的messageLayout

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

    <ListView 
     android:id="@+id/action_bar_item_list" 
     android:layout_above="@+id/mybutton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

    <Button 
     android:id="@+id/mybutton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:text="Button" /> 

    <RelativeLayout 
     android:id="@+id/messageWrapper" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#00000000"> 

     <LinearLayout 
      android:id="@+id/messagelayout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/message" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Large Text" 
       android:textAppearance="?android:attr/textAppearanceLarge" /> 

     </LinearLayout> 

    </RelativeLayout> 

</RelativeLayout> 

創建另一個佈局創建周圍的消息佈局的包裝,並將它覆蓋所有區域做到這一點。注意背景顏色是透明的(#00000000不是#000000)。現在在包裝器上設置onClickListener以捕獲messageLayout外的所有點擊。

wrapper = (RelativeLayout) findViewById(R.id.wrapper); 
wrapper.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     // Hide the view 
    } 
}); 

這將捉對包裝的所有點擊但不幸的是它也將捉對messageLayout的點擊,所以你需要創建在messageLayout一個onClickListener。

messageLayout = (LinearLayout) findViewById(R.id.messagelayout); 
messageLayout.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     // If you do not need to anything here, just leave it blank but it has to be implemented. 
    } 
}); 

這將使您的包裝行爲像一個對話框。要顯示/隱藏messageLayout,您需要在包裝器上設置可變性,而不是messageLayout。

相關問題