android之popupwindow
popwindow.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">
- <TextView
- android:id="@+id/textview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text=""
- />
- <Button
- android:id="@+id/test"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="测试"
- />
- </LinearLayout>
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/main"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <VideoView
- android:id="@+id/videoshow"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- </LinearLayout>
Activity01.java
- package com.overflow.testvideo;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Activity;
- import android.content.Context;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.Gravity;
- import android.view.KeyEvent;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.MediaController;
- import android.widget.PopupWindow;
- import android.widget.TextView;
- import android.widget.VideoView;
- public class Activity01 extends Activity {
- VideoView vv = null;
- Button btn = null;
- TextView textview = null;
- /** Called when the activity is first created. */
- private Handler mHandler = new Handler(){
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case 1:
- showPopupWindow();
- break;
- }
- };
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Timer timer = new Timer();
- timer.schedule(new initPopupWindow(), 100);
- //textview = (TextView) music_popunwindwow.
- //btn = (Button) this.findViewById(R.id.test);
- vv = (VideoView) this.findViewById(R.id.videoshow);
- vv.setVideoURI(Uri.parse("android.resource://com.overflow.testvideo/"+R.raw.underwater));
- vv.setMediaController(new MediaController(this));
- vv.start();
- vv.setOnTouchListener(new VideoView.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- return true;
- }
- });
- }
- private class initPopupWindow extends TimerTask{
- @Override
- public void run() {
- Message message = new Message();
- message.what = 1;
- mHandler.sendMessage(message);
- }
- }
- public void showPopupWindow() {
- Context mContext = Activity01.this;
- LayoutInflater mLayoutInflater = (LayoutInflater) mContext
- .getSystemService(LAYOUT_INFLATER_SERVICE);
- View music_popunwindwow = mLayoutInflater.inflate(
- R.layout.popwindow, null);
- PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow,
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
- mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0);
- btn = (Button) music_popunwindwow.findViewById(R.id.test);
- textview = (TextView) music_popunwindwow.findViewById(R.id.textview);
- btn.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- textview.setText("测试成功");
- }
- });
- }
- }
记得要dismiss(),否则返回会报错,难点在要触发popwindow上的事件,就必须是这个popwindow下来findviewbyid,否则就报错
本来这个东西的最初目的是写动态背景,但失败了,蛮多限制。贴上代码,以后可能需要。
还一些参考文章:http://www.cmd100.com/bbs/thread-111-1-1.html
http://mobile.51cto.com/android-254829.htm
浙公网安备 33010602011771号