2015-01-31 109 views
-1

我使用LinearLayout和我的佈局,這樣的設置背景:進行佈局的背景圖像的可點擊區域

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".MainActivity" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/back"> 

和背面圖像: enter image description here

作爲背景圖片顯示我設計一些地方按鈕,現在我想讓該區域可點擊,我該怎麼辦?日Thnx

回答

0

這也許不會回答你的問題,但我有一個建議:

你爲什麼不只是將背景設置爲藍色,然後將按鈕添加到自己的佈局(也許你需要考慮使用的TableLayout代替)?

0

你可以使用windowManager來做到這一點,但我認爲Thanos說的更好。

//get the instance of WindowManager 
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
     WindowManager.LayoutParams params = new WindowManager.LayoutParams(
       <WIDTH OF THE BUTTON>, 
       <HEIGHT OF THE BUTTON>, 
       WindowManager.LayoutParams.TYPE_APPLICATION_PANEL, 
       WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH| 
       WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, 
       PixelFormat.TRANSPARENT); 
    //make sure (0;0) is in the top left corner 
     params.gravity = Gravity.TOP | Gravity.LEFT; 
    params.x = <POSITION X OF THE BUTTON> 
    params.y = <POSITION Y OF THE BUTTON> 
//make an empty layout so you can get the click event 
RelativeLayout layout = new RelativeLayout(this/*or your context here*/); 
layout.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       //do stuff 
      } 
     }); 
windowManager.addView(layout, params); 

,如果你想看到的可點擊區域的形狀,您可以添加匹配它的父的大小在佈局視圖