安卓 - 全屏透明按钮

4

有没有可能制作一个全屏透明的按钮,使得无论用户在哪里点击,按钮都会被激活?

下面是一些基本的Java代码,显示了一个按钮,当按下按钮时启动一个新的Intent。我还添加了main.xml布局,但我不知道如何更新它,使按钮填充整个屏幕并且透明。

package com.MeetingManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MeetingManager extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);



    layout.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent e) {
            Intent myIntent = new Intent(MeetingManager.this,CreateMeeting.class);
            startActivityForResult(myIntent, 0);
        }
    });

}



}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/meetingbg"
    >
    <TableRow android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tableRow5">
        <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </TableRow>

</LinearLayout>
2个回答

8
如果你的目标是通过屏幕上的轻触来监听用户交互,我建议将触摸监听器添加到你的LinearLayout中。这样就不需要使用按钮了。
例如:
LinearLayout layout = (LinearLayout) findViewById(R.id.your_layout);

layout.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return false;
        }
    });
});

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/your_layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/meetingbg"
    >
    <TableRow android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tableRow5">
        <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </TableRow>

</LinearLayout>

您可能需要包含以下导入:

import android.view.View.OnTouchListener;
import android.view.MotionEvent;


谢谢!+1 我现在正在实现。 - Denoteone
当我尝试添加上述代码时,它试图重写我的R.java文件(这是不可能的)。我有什么遗漏吗?我已经更新了我的代码。 - Denoteone
我已经编辑了我的源代码,展示了如何在XML中向布局添加ID以及在添加onTouchListener之前如何通过编程方式获取它。希望这可以帮到你。 - Ljdawson
感谢您的所有帮助:您的协助使得我的“布局”问题得到了解决,但现在我的View.OnTouchListener出现了错误:The type new View.OnTouchListener(){} must implement the inherited abstract method View.OnTouchListener.onTouch(View, MotionEvent),而我的MotionEvent也显示了错误:The type new View.OnTouchListener(){} must implement the inherited abstract method View.OnTouchListener.onTouch(View, MotionEvent)。 - Denoteone
我已经再次编辑了源代码,包括返回类型和您可能需要的导入。 - Ljdawson

1

尝试在背景中使用 null android:background="@null"


那么如何移除按钮的背景呢?我该如何让按钮覆盖页面上的所有其他元素? - Denoteone
如果你只有一个按钮,可以使用RelativeLayout。同时请查看bringChildToFront()方法 - salahy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接