android中popwindow的使用

jopen 8年前發布 | 16K 次閱讀 Android開發 移動開發

android開發中popwindow的使用,首先需要定義popwindow的布局,這里定義的比較簡單

pop_item.xml

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

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="確定" />

    <Button
        android:id="@+id/cancle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消" />

</LinearLayout>

在MainActivity中獲得自定義的popWindow布局,實例化popwindow對象,然后定義其顯示位置,顯示動畫等

MainActivity.java

package com.example.popwindow;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button open = (Button) findViewById(R.id.open);
        open.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_item,null);
                Button ok = (Button) view.findViewById(R.id.ok);
                Button cancle = (Button) view.findViewById(R.id.cancle);

                final PopupWindow popWindow = new PopupWindow(view, 100,100);
                popWindow.setAnimationStyle(R.anim.pop);
                popWindow.showAsDropDown(arg0);
                ok.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this,"you click ok",1000).show();
                    }
                });
                cancle.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        popWindow.dismiss();
                    }
                });
            }
        });

    }

}

pop.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0"
           android:toAlpha="1"
           android:duration="2000">
    </alpha>
</set>


來自: http://blog.csdn.net//mockingbirds/article/details/40260437

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