android paopao窗口+gridview 的实现底部菜单

PopupWindow是一个可以显示在当前Activity之上的浮动容器,PopupWindow弹出的位置是能够改变的,按照有无偏移量,可以分为无偏移和有便宜两种;按照参照对象的不同又可以分为两种:相对某个控件(Anchor锚点)的位置和在父容器内部的相对位置。

我写的是泡泡窗口和grideview 结合的底部菜单效果,你也可以和listview结合从左侧或者右侧出现实现侧滑导航效果等。也可以直接布局写button 泡泡窗口我理解的就是一个view 容器,对于手机屏幕小做的扩展手机屏幕空间一种手段。
效果图


不多说了案例代码我已上传资源页 。
[泡泡窗口+gridview Demo] (http://download.csdn.net/detail/u012373815/9012299)

代码:

public class MainActivity extends Activity {
    private  PopupWindow popupWindow;
    private View parent;
    private int[] images={R.drawable.icon25,R.drawable.icon25,R.drawable.icon25,R.drawable.icon25,
            R.drawable.icon25,R.drawable.icon25,R.drawable.icon25};
    private String[] names={"1","2","3","4","5","6","7"};

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

        View contentView=  getLayoutInflater().inflate(R.layout.popwindow, null);//定义泡泡视图
        GridView gridView= (GridView)contentView.findViewById(R.id.gridView1);//泡泡窗口的布局
        gridView.setAdapter(getAdapter()); 
        gridView.setOnItemClickListener(new ItemClickListener1());

        popupWindow=new PopupWindow(contentView, 
               ViewGroup.LayoutParams.MATCH_PARENT,//width

               ViewGroup.LayoutParams.WRAP_CONTENT);//higth
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
       popupWindow.setAnimationStyle(R.style.animation);
        parent=this.findViewById(R.id.main_activity);

    }
    private final class ItemClickListener1 implements OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            TextView tixt=(TextView)findViewById(R.id.text);
            if(popupWindow.isShowing())
            {   
                switch (position)
                 {                  
                   case 0:  tixt.setText("您点击了0");break;
                   case 1:  String str0="您点击了1";tixt.setText(str0);break;
                   case 2:  String str1="您点击了2";tixt.setText(str1);break;
                   case 3:  String str2="您点击了3";tixt.setText(str2);break;
                   case 4:  String str3="您点击了4";tixt.setText(str3);break;
                   case 5:  String str4="您点击了5";tixt.setText(str4);break;
                   case 6:  String str5="您点击了6";tixt.setText(str5);break;
                   case 7:  String str6="您点击了7";tixt.setText(str6);break;                                    

                 }
                popupWindow.dismiss();
                //消失

            }
            // popupWindow end

        }

    }

      private ListAdapter getAdapter() {
            List<HashMap<String, Object>>data=new ArrayList<HashMap<String,Object>>();
            for(int i=0; i<images.length;i++)
            {
                HashMap<String, Object> item=new HashMap<String, Object>();
                item.put("image", images[0]);
                item.put("name", names[i]);
                data.add(item);
            }

            SimpleAdapter simpleAdapter=new SimpleAdapter(this, data, R.layout.grid_item,
                new String[]{"image","name"}, new int []{R.id.imageViewpopo,R.id.textViewpopo});



            return simpleAdapter;
        }

    public void paopaowindow(View v)//点击泡泡窗口出现
           {        
               popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0); //确定在界面中出现的位置 

           }

}

activity_main.xml 中的代码 (注意布局id 和button id)

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/main_activity"
    tools:context="com.example.paopaowindow.MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="paopaowindow"
        android:layout_marginTop="40dp"
        android:onClick="paopaowindow"/>

</RelativeLayout>

这个是结合gridview 的grideview 的items 布局 控制泡泡窗口的每个小按钮的布局

<?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:gravity="center">



    <ImageView  android:id="@+id/imageViewpopo" android:layout_width="wrap_content" android:layout_height="wrap_content" />
 <TextView  android:id="@+id/textViewpopo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="16dp" android:textColor="#000099" android:text="TextView" />


</LinearLayout>

这个是paopao窗口的xml 文件 是一个view 里面写了GridView

<?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="#a0a0a0" >
    <GridView  android:id="@+id/gridView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numColumns="4" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:text="我是泡泡窗口" />

</LinearLayout>

这个是paopao窗口出现和消失的动画效果 (这一部分是为了美观)
在value文件夹下 新建 xml命名为style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <style name="animation"> <item name="android:windowEnterAnimation">@anim/enter</item> <item name="android:windowExitAnimation">@anim/out</item> <!-- 窗口 进出的动画--> </style>
</resources>

在res文件夹下新建anim 文件夹 并新建两个写动画效果的xml
进入动画 enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false">
    <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="500" /><!-- Y轴平移 -->

<alpha  android:fromAlpha="0.7" android:toAlpha="1.0" android:duration="500" /><!-- 透明度从0.5到1 -->
</set>

退出动画 out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false">
    <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="1000" /><!-- Y轴平移 -->

<alpha  android:fromAlpha="1.0" android:toAlpha="0.5" android:duration="1000" /><!-- 透明度从0.5到1 -->
</set>

你可能感兴趣的:(paopao窗口)