简单PopuWindow实现


简单的实现PopuWindow

对于PopuWindow的使用,有时候我们其实不需要那么复杂,简单的实现就好了.当然对于大神们的自定义,我还是比较钦佩的.

下面呢 我做了一个简单的小Dome.如果猿友们不想用自定义时,可以借鉴一下下.


1.布局文件

其实布局很简单,

<Button
    android:id="@+id/bt_mune"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="showPop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="24sp"
    android:text="菜单"/>
就一个Button的按钮

pop_item的布局也很简单

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_margin="10dp"
        android:id="@+id/tv_cumputer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算机"/>
    
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#e6e6e6"/>

    <TextView
        android:layout_margin="10dp"
        android:id="@+id/tv_jinRong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="金融"/>

    <TextView
        android:layout_margin="10dp"
        android:id="@+id/tv_manage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="管理"/>

</LinearLayout>
当然这里的布局看大家的意思,可以随意更改布局

2.代码部分

public void showPop(View view) {
    /**
     * 首先第一个参数,contentView提供一个展示的内容视图
     * 第二个参数是宽
     * 第三个是高
     * 这里我们做的是指定 popuwindow的视图
     */
    View contentView = View.inflate(this,R.layout.pop_item,null);
    PopupWindow popupWindow = new PopupWindow(contentView, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
   /*popuWindow的属性,设置成true代表在popuwindow外部可以点击且popwindow会消失*/
    popupWindow.setOutsideTouchable(true);

    /**
     * 在某一控件下边   anchor
     */
    popupWindow.showAsDropDown(findViewById(R.id.bt_mune),0,0);
    /*查找popitem中的ID*/
    TextView tv_cumputer = (TextView) contentView.findViewById(R.id.tv_cumputer);
    TextView tv_jinRong = (TextView) contentView.findViewById(R.id.tv_jinRong);
    TextView tv_manage = (TextView) contentView.findViewById(R.id.tv_manage);
    /*给查找到的ID设置点击事件*/
    tv_cumputer.setOnClickListener(this);
    tv_jinRong.setOnClickListener(this);
    tv_manage.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.tv_cumputer:
            //成功后的吐丝
            Toast.makeText(MainActivity.this, "点击了计算机", Toast.LENGTH_SHORT).show();
            break;

        case R.id.tv_jinRong:
            //成功后的吐丝
            Toast.makeText(MainActivity.this, "点击了金融", Toast.LENGTH_SHORT).show();
            break;

        case R.id.tv_manage:
            //成功后的吐丝
            Toast.makeText(MainActivity.this, "点击了管理", Toast.LENGTH_SHORT).show();
            break;
    }
}

 
  
 

猜你喜欢

转载自blog.csdn.net/mr_tianchen/article/details/71307851