2015-01-31 105 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); 

,如果你想看到的可点击区域的形状,您可以添加匹配它的父的大小在布局视图