讓Android組件懸浮所有頁面之上

jopen 10年前發布 | 50K 次閱讀 Android Android開發 移動開發
import android.app.Activity;  
import android.graphics.Color;  
import android.os.Bundle;  
import android.view.WindowManager;  
import android.view.WindowManager.LayoutParams;  
import android.widget.TextView;  

public class MainActivity extends Activity {  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        WindowManager wm = (WindowManager) getApplicationContext()  
                .getSystemService(WINDOW_SERVICE);  
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();  
        // 懸浮所有頁面之上  
        lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;  
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;  
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;  
        // 失去焦點  
        lp.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL  
                | LayoutParams.FLAG_NOT_FOCUSABLE;  
        TextView tv = new TextView(this);  
        tv.setText("我是不是你最疼愛的人,你為什么不說話,握住是你冰冷的手動也不動讓我好難過");  
        tv.setBackgroundColor(Color.WHITE);  
        wm.addView(tv, lp);  
    }  

}  
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!