Android中的PopupWindow的使用

来源:互联网 发布:大数据概述及其趋势 编辑:程序博客网 时间:2024/06/02 19:54

import com.example.qqpop.R;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;

/**
 * 主界面
 *
 * @author zihao
 *
 */
public class MainActivity extends Activity {

    public Button popBtn;
    private View mPopView;
    private RelativeLayout mCanversLayout;// 阴影遮挡图层
    private PopupWindow mPopupWindow;// 弹窗对象
    private ImageView mChats;// 弹窗上的选项

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

    private void initView() {
        
        //fragment_news_pop xml文件
        mPopView = LayoutInflater.from(MainActivity.this).inflate(
                R.layout.fragment_news_pop, null);
        
        //activity——main布局中的绝对布局
        mCanversLayout = (RelativeLayout) findViewById(R.id.rl_canvers);
        
        //初始化单床对象 参数设置   布局    宽  高   
        mPopupWindow = new PopupWindow(mPopView, LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT, true);
        
        
        //弹窗上的选项
        mChats = (ImageView) mPopView.findViewById(R.id.pop_chat);
        //监听
        mChats.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });

        
        //按钮
        popBtn = (Button) findViewById(R.id.popBtn);
        //事件
        popBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                
                // 点击出现
                mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color
                        .parseColor("#E9E9E9")));
                
                mPopupWindow.showAsDropDown(popBtn, 0, -15);
                
                //设置动画样式
                mPopupWindow.setAnimationStyle(R.style.popwin_anim_style);
                //设置手势
                mPopupWindow.setFocusable(true);
                
                mPopupWindow.setOutsideTouchable(true);
                
                mPopupWindow.update();
                //设置可见
                mCanversLayout.setVisibility(View.VISIBLE);
            }
        });

        
        mPopupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {// 点击消失
                mCanversLayout.setVisibility(View.GONE);
            }
        });
    }
}



activity_main.xml文件


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}" >

    <Button
        android:id="@+id/popBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />

    <RelativeLayout
            android:id="@+id/rl_canvers"
            android:layout_below="@id/popBtn"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0.3"
            android:visibility="gone"
            android:focusable="true"
            android:background="@color/black"/>
    
</RelativeLayout>






popxml弹窗上显示的文件

<?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="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/ll_group"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/pop_chat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:contentDescription="@string/app_name"
                android:scaleType="fitXY"
                android:src="@drawable/conversation_options_multichat" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/pop_chat"
                android:layout_centerHorizontal="true"
                android:text="@string/pop_chat"
                android:textColor="@color/gray_font" />
        </RelativeLayout>

        <View
            android:layout_width="0.5dp"
            android:layout_height="120dp"
            android:background="@color/devide_line" />

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/pop_sangzhao"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:contentDescription="@string/app_name"
                android:scaleType="fitXY"
                android:src="@drawable/conversation_options_share_photo" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/pop_sangzhao"
                android:layout_centerHorizontal="true"
                android:text="@string/pop_sangzhao"
                android:textColor="@color/gray_font" />
        </RelativeLayout>

        <View
            android:layout_width="0.5dp"
            android:layout_height="120dp"
            android:background="@color/devide_line" />

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/pop_camera"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:contentDescription="@string/app_name"
                android:scaleType="fitXY"
                android:src="@drawable/conversation_options_camera" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/pop_camera"
                android:layout_centerHorizontal="true"
                android:text="@string/pop_camera"
                android:textColor="@color/gray_font" />
        </RelativeLayout>

        <View
            android:layout_width="0.5dp"
            android:layout_height="120dp"
            android:background="@color/devide_line" />

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/pop_scan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:contentDescription="@string/app_name"
                android:scaleType="fitXY"
                android:src="@drawable/conversation_options_qr" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/pop_scan"
                android:layout_centerHorizontal="true"
                android:text="@string/pop_scan"
                android:textColor="@color/gray_font" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>



color.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="devide_line">#CBCED2</color>
    <color name="blue">#499BF7</color>
    <color name="whites">#ffffff</color>
    <color name="grays">#818181</color>
    <color name="gray_font">#818181</color>
    <color name="common_title">#F9F9F9</color>
    <color name="black">#000000</color>
    <color name="pull_refresh_textview">#000000</color>
    <color name="gold">#000000</color>
    <color name="transparent">#00000000</color>
    <color name="refresh_head_bg">#2B2B2B</color>
    <color name="red">#FF0000</color>
    <color name="backColor">#2000</color>
    <color name="detail_bgColor">#f5f5f5</color>
    <color name="defaultTextColor">#666</color>
    <color name="viewfinder_frame">#2477ab</color>
    <color name="viewfinder_mask">#60000000</color>
    <color name="possible_result_points">#c0ffff00</color>
    <color name="result_view">#b0000000</color>
    <color name="gray">#AFAFAF</color>
    <color name="yellow">#F99503</color>
    
    <color name="orange">#ff9900</color>
    <color name="deepcolor">#faaa5f</color>
    <color name="shadowcolor">#333333</color>
    <color name="naming">#ABABAB</color>
    <color name="background">#272727</color>
    <color name="orange_yellow">#9b9ba0</color>

    <drawable name="semi_black">#80000000</drawable>

    <color name="remote_connect_text">#FFe78018</color>
    <color name="remote_disconnect_text">#FF901821</color>
</resources>





0 0
原创粉丝点击