標籤:android blog http java color os strong 檔案
PopupWindow是一個可以用來顯示一個任意的視圖的快顯視窗,他需要完全依賴layout布局。
它沒什麼介面,在彈出的視窗中完全顯示布局中的控制項。
上面兩個美女頭就是彈窗PopupWindow顯示的內容。是兩個Button。
具體實現:
注意:那三個Button不能和普通的Button一樣通過findViewById()方法獲得,必須首先說的Button所在的視圖,View popview = layoutInflater.inflate(R.layout.poplayout, null);
我的Button在poplayout.xml中。最後通過button1 = (Button) popview.findViewById(R.id.button1)獲得。
另外一點就是:不要在oncreate()中獲得Button,而是像我一樣在onclick方法下獲得,和popupwindow一起。這一點不一定正確。
為什麼要提呢?因為我在oncreate()中獲得Button時,button的點擊事件不能用,我也不是很清楚。那位大牛要是知道的話,可以告訴我一下。
showAtLocation(findViewById(R.id.edit_layout), Gravity.BOTTOM,0, 0);
設定彈窗的位置:
第一個參數是彈窗父控制項的布局;
第二個參數是位置如左,右,上部,下部等;
第三個參數是X方向的位移量;
第四個參數是Y方向的位移量。
[java] view plaincopy
- package xiaosi.popwindow;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.WindowManager.LayoutParams;
- import android.widget.Button;
- import android.widget.PopupWindow;
- import android.widget.Toast;
-
- public class PopwindowActivity extends Activity implements OnClickListener
- {
- /** Called when the activity is first created. */
- private Button button = null;
- private Button button1 = null;
- private Button button2 = null;
- private Button button3 = null;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button) findViewById(R.id.button);
- button.setOnClickListener(this);
- }
-
- public void onClick(View arg0)
- {
- if (arg0.getId() == R.id.button)
- {
- LayoutInflater layoutInflater = (LayoutInflater) (PopwindowActivity.this)
- .getSystemService(LAYOUT_INFLATER_SERVICE);
- // 擷取自訂布局檔案poplayout.xml的視圖
- View popview = layoutInflater.inflate(R.layout.poplayout, null);
- PopupWindow popWindow = new PopupWindow(popview,
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- //規定彈窗的位置
- popWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM,
- 0, 0);
- //PopupWindow裡的兩個Button
- button1 = (Button) popview.findViewById(R.id.button1);
- button1.setOnClickListener(this);
- button2 = (Button) popview.findViewById(R.id.button2);
- button2.setOnClickListener(this);
- }
- else if (arg0.getId() == R.id.button1)
- {
- Toast.makeText(PopwindowActivity.this, "button1", Toast.LENGTH_LONG)
- .show();
- }
- else if (arg0.getId() == R.id.button2)
- {
- Toast.makeText(PopwindowActivity.this, "button2", Toast.LENGTH_LONG)
- .show();
- }
- }
- }
poplayout.xml
[java] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="horizontal" >
-
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/colorframe_1" />
-
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/colorframe_3" />
-
- </LinearLayout>
main.xml
[java] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/main"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:background="@drawable/background">
-
- <Button
- android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="點擊查看效果" />
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/a" />
-
- </LinearLayout>
以上只是用來學習之用,拿出來和大家一起分享一下。
有想要原始碼的給我留言。
http://blog.csdn.net/sjf0115/article/details/7339914