制作可点击的布局背景图像区域

时间:2015-01-31 21:20:01

标签: android android-layout android-linearlayout clickable

我使用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

2 个答案:

答案 0 :(得分:0)

这可能无法解答您的问题,但我有一个建议:

为什么不将背景设置为蓝色,然后将按钮放置到布局中(可能需要考虑使用TableLayout)?

答案 1 :(得分:0)

你可以使用windowManager来做到这一点,但我认为比塔斯说的更好。

//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);

如果您想查看可点击区域的形状,可以在布局中添加与其父级尺寸相匹配的视图