利用FrameLayout+PopupWindow 實作兩個介面可以互相修修改狀態的範例
由於工作上需要,所以練習如何實作PopupWindow,歡迎有興趣的同好,一起來C/P一下。
資料來源:http://code.eoe.cn/169
package com.example.framelayout_popupwindow_talking;
import com.example.framelayout_popupwindow_talking.MainActivity.ButListener;
import android.os.Bundle; import android.app.Activity; import android.view.Gravity; import android.view.Menu;
/////////////////////////////////////// import android.view.View; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.Log; import android.view.Window; import android.view.WindowManager; import android.widget.AbsoluteLayout; import android.widget.Button; import android.widget.FrameLayout; import android.widget.TextView; import android.widget.VideoView; import android.view.*; import android.view.View.*; import android.widget.*; import android.view.ViewGroup.MarginLayoutParams; import android.app.ActionBar.LayoutParams; import android.content.Context; import android.graphics.drawable.BitmapDrawable;
public class MainActivity extends Activity { TextView tv_main; Button bt_main; Button[] bt_Popup; FrameLayout FrameLayout2; FrameLayout FrameLayout1; DisplayMetrics metrics; VideoView VideoView1; PopupWindow popupWindow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.onCreate(savedInstanceState); ///////////////////////////////////////// requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); metrics = new DisplayMetrics();//抓取螢幕大小 getWindowManager().getDefaultDisplay().getMetrics(metrics); ///////////////////////////////////////// FrameLayout1 = new FrameLayout(this); FrameLayout.LayoutParams lparam1 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels); FrameLayout1.setLayoutParams(lparam1); this.setContentView(FrameLayout1); //////////////////////////////////////// tv_main=new TextView(this); tv_main.setText(""); tv_main.setWidth(metrics.widthPixels/2); tv_main.setHeight(metrics.heightPixels/2); FrameLayout.LayoutParams layoutParams0 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2); layoutParams0.gravity = Gravity.LEFT|Gravity.TOP; layoutParams0.leftMargin = 0; layoutParams0.topMargin = 0; tv_main.setLayoutParams(layoutParams0); FrameLayout1.addView(tv_main);
bt_main=new Button(this); bt_main.setText("Run PopupWindow"); bt_main.setWidth(metrics.widthPixels/2); bt_main.setHeight(metrics.heightPixels/2); bt_main.setOnClickListener(new ButListener()); FrameLayout.LayoutParams layoutParams1 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2); layoutParams1.gravity = Gravity.LEFT|Gravity.TOP; layoutParams1.leftMargin = metrics.widthPixels/2; layoutParams1.topMargin = 0; bt_main.setLayoutParams(layoutParams1); FrameLayout1.addView(bt_main); //////////////////////////////////////////// bt_Popup=new Button [4]; FrameLayout2 = new FrameLayout(this); FrameLayout.LayoutParams lparam2 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels/2); lparam2.gravity = Gravity.LEFT|Gravity.TOP; lparam2.leftMargin = 0;//X lparam2.topMargin = metrics.heightPixels/2;//Y FrameLayout2.setLayoutParams(lparam2); bt_Popup[0]=new Button(this); bt_Popup[0].setText("PopupWindow0"); bt_Popup[0].setWidth(metrics.widthPixels/2); bt_Popup[0].setHeight(metrics.heightPixels/4); bt_Popup[0].setOnClickListener(new ButListener()); FrameLayout.LayoutParams layoutParams_po0 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/4); layoutParams_po0.gravity = Gravity.LEFT|Gravity.TOP; layoutParams_po0.leftMargin = 0; layoutParams_po0.topMargin = 0; bt_Popup[0].setLayoutParams(layoutParams_po0); FrameLayout2.addView(bt_Popup[0]); bt_Popup[1]=new Button(this); bt_Popup[1].setText("PopupWindow1"); bt_Popup[1].setWidth(metrics.widthPixels/2); bt_Popup[1].setHeight(metrics.heightPixels/4); bt_Popup[1].setOnClickListener(new ButListener()); FrameLayout.LayoutParams layoutParams_po1 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/4); layoutParams_po1.gravity = Gravity.LEFT|Gravity.TOP; layoutParams_po1.leftMargin = metrics.widthPixels/2; layoutParams_po1.topMargin = 0; bt_Popup[1].setLayoutParams(layoutParams_po1); FrameLayout2.addView(bt_Popup[1]); bt_Popup[2]=new Button(this); bt_Popup[2].setText("PopupWindow2"); bt_Popup[2].setWidth(metrics.widthPixels/2); bt_Popup[2].setHeight(metrics.heightPixels/4); bt_Popup[2].setOnClickListener(new ButListener()); FrameLayout.LayoutParams layoutParams_po2 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/4); layoutParams_po2.gravity = Gravity.LEFT|Gravity.TOP; layoutParams_po2.leftMargin = 0; layoutParams_po2.topMargin = metrics.heightPixels/4; bt_Popup[2].setLayoutParams(layoutParams_po2); FrameLayout2.addView(bt_Popup[2]); bt_Popup[3]=new Button(this); bt_Popup[3].setText("PopupWindow3"); bt_Popup[3].setWidth(metrics.widthPixels/2); bt_Popup[3].setHeight(metrics.heightPixels/4); bt_Popup[3].setOnClickListener(new ButListener()); FrameLayout.LayoutParams layoutParams_po3 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/4); layoutParams_po3.gravity = Gravity.LEFT|Gravity.TOP; layoutParams_po3.leftMargin = metrics.widthPixels/2; layoutParams_po3.topMargin = metrics.heightPixels/4; bt_Popup[3].setLayoutParams(layoutParams_po3); FrameLayout2.addView(bt_Popup[3]); }
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class ButListener implements OnClickListener { public void onClick(View v) { if(v==bt_main) { tv_main.setText("Create/Open PopupWindow"); //////////////////////////////////////////////// popupWindow = new PopupWindow(MainActivity.this); popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.setWidth(metrics.widthPixels); popupWindow.setHeight(metrics.heightPixels/2); popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.setContentView(FrameLayout2); popupWindow.showAtLocation(FrameLayout1, Gravity.LEFT| Gravity.TOP, 0, metrics.heightPixels/2); } if(v==bt_Popup[0]) { tv_main.setText("PopupWindow 0 click..."); popupWindow.dismiss(); popupWindow = null; } if(v==bt_Popup[1]) { tv_main.setText("PopupWindow 1 click..."); popupWindow.dismiss(); popupWindow = null; } if(v==bt_Popup[2]) { tv_main.setText("PopupWindow 2 click..."); popupWindow.dismiss(); popupWindow = null; } if(v==bt_Popup[3]) { tv_main.setText("PopupWindow 3 click..."); popupWindow.dismiss(); popupWindow = null; } } } }
|
沒有留言:
張貼留言