PopupWindow懸浮框

一般情況我們用的都是dialog對話框,懸浮框用的比較少,但是有些地方還必須要用懸浮框。像qq的長按彈出的置頂刪除用的就是懸浮框。懸浮框與對話框唯一的區別在於它的位置是隨意的。用起來也簡單,加載一個view實例化後設置點東西就行了。
下面是代碼和解析
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    public Button button;
    public PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button= (Button) findViewById(R.id.button);
        button.setOnClickListener(this);

        poppu();

    }

    public void poppu(){
        View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.item_pop,null,false);
        final EditText editText= (EditText) view.findViewById(R.id.edit);
        Button bu= (Button) view.findViewById(R.id.bu);
        //實例化一個popupwindow。參數是加載view已經寬和高
        popupWindow=new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        //設置爲true則要它消失後才能響應其他事件
        popupWindow.setFocusable(true);
        //爲了點擊非懸浮框處或者按返回鍵懸浮框消失,需要如下設置。
        //而且必須設置一個背景纔有效。
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));


        bu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,editText.getText(),Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //讓懸浮框在按鈕下面x方向偏30位置顯示(默認無偏是在左下方)
                popupWindow.showAsDropDown(v,30,0);
                break;
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="@drawable/bg"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/edit"
        android:layout_width="120dp"
        android:layout_height="60dp" />
    <Button
        android:id="@+id/bu"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="按鈕"/>

</LinearLayout>


除此之外再介紹一些方法

setAnimationStyle(int) 這個是設置動畫效果的。裏面的參數是一個int。很明顯是傳一個anim佈局進去。

showAsDropDown(View anchor):相對某個控件的位置(正左下方),無偏移

showAtLocation(View parent, int gravity, int x, int y): 相對於父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設置偏移或無偏移 PS:parent這個參數只要是activity中的view就可以了!

一些構造函數

public PopupWindow (Context context)

public PopupWindow(View contentView, int width, int height)

public PopupWindow(View contentView)

public PopupWindow(View contentView, int width, int height, boolean focusable)


最後再提示一點。如果想要有上圖那種氣泡效果的話,把背景圖做成.9圖是個不錯的辦法。如何製作可以參考這篇文章

http://blog.csdn.net/lhp15575865420/article/details/75096560



發佈了42 篇原創文章 · 獲贊 55 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章