【问题标题】:Android - full screen transparent buttonAndroid - 全屏透明按钮
【发布时间】:2011-10-08 14:22:37
【问题描述】:

是否可以制作一个透明的全屏按钮,这样无论用户在哪里点击按钮都会被激活?

下面是一些基本的java,它显示了一个按钮,按下时会启动一个新的意图。我还添加了 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>

【问题讨论】:

    标签: java android button android-layout


    【解决方案1】:

    如果您的目标是通过点击屏幕来监听用户交互,我建议您在 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 我现在正在实施。
    • 当我尝试添加上面的代码时,它试图重写我的 R.java 文件(这是不可能的)我错过了什么吗?我更新了上面的代码。
    • 我已经编辑了我的源代码,以展示如何在 XML 中将 ID 添加到您的布局,以及如何在添加 onTouchListener 之前以编程方式获取它。希望这会有所帮助
    • 感谢您的帮助:您的帮助使我的“布局”得以解决,但现在我的 View.OnTouchListener 显示错误:类型 new View.OnTouchListener(){} 必须实现继承的抽象方法 View.OnTouchListener.onTouch(View, MotionEvent) 和我的 MotionEvent 显示错误:类型 new View.OnTouchListener(){} 必须实现继承的抽象方法 View.OnTouchListener.onTouch(View, MotionEvent)
    • 我再次编辑了源代码以包含返回类型以及您可能需要的导入。
    【解决方案2】:

    尝试在后台使用 null android:background="@null"

    【讨论】:

    • 那么要去掉按钮的背景吗?如何让按钮覆盖页面上的所有其他元素?
    • 如果你只有一个按钮,你可以使用RelativeLayout 也检查方法bringChildToFront()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 2021-02-18
    相关资源
    最近更新 更多