自定义下拉弹窗ppw

在我们的日常开发中 ,很多时候系统给我的控件美感不足,灵活性不强,很难满足我们的需求。这时候我们就需要去自定义一些控件,这次我带来的是应用性很高的下拉弹窗的空间,在更换头像的时候,我们就会经常用到这个来自屏幕下方的弹窗控件。

如果我们需要设置按钮的动画呢 可以在res文件夹下面新建一个animation的文件夹 在下面添加我们需要的动画,如

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="200"
        android:fromYDelta="0"
        android:toYDelta="50%p" />

    <alpha
        android:duration="200"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

</set>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/pop_first_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/pop_first_normal"/>

</selector>

详细的可以看一下 我上传的demo。由于我还没有掌握绕罗翔截图法,没有办法去上传一些动图,让大家看下效果,哎,对不起抗压吧的蛆虫们孜孜不倦的教诲了。
在布局方面我们要建一个主页面的xml和一个关于ppw的item页面 代码如下,这种方法跟listview与item非常相似:
主main
<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" >

    <TextView
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="25sp"
        android:background="@drawable/background"
        />

</RelativeLayout>
ppw.XML布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="12dp"
            android:layout_marginLeft="8dp"
            android:text="@string/pay"
            android:textColor="#008B8B"
            android:textSize="15sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:layout_marginBottom="12dp"
            android:layout_marginLeft="8dp"
            android:onClick="onClick"
            android:text="60"
            android:textColor="#BFBFBF"
            android:textSize="15sp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:layout_marginBottom="8dp"
            android:src="@drawable/masonry" />
        <ImageButton
            android:id="@+id/goto_Pay_Activity"
            android:layout_width="20dp"
            android:layout_height="25dp"
            android:src="@drawable/next"
            android:layout_marginLeft="6dp"
           
            />

       
    </LinearLayout>
    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/Send"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:background="#b8b8b8"
                android:gravity="center"
                android:text="@string/send"
                android:textColor="#fefefe" />
              <TextView
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="#b8b8b8"
                android:text="dddd"
                android:layout_above="@+id/Send"
               
                />
        </RelativeLayout>
    
</RelativeLayout>


java代码:
package com.example.picpopupwindow;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  findViewById(R.id.button).setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    showPopwindow();
   }
  });

 }

 private void showPopwindow() {
  //获得他们的点击时间
  View parent = ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
  View popView = View.inflate(this, R.layout.gift_pop_menu, null);
  //Button btnCamera = (Button) popView.findViewById(R.id.btn_camera_pop_camera);
  //获得其弹窗的高度跟宽度
  int width = getResources().getDisplayMetrics().widthPixels;
  int height = getResources().getDisplayMetrics().heightPixels/2;
//参数传入对象,宽,高
  final PopupWindow popWindow = new PopupWindow(popView,width,height);
  //这是我们自定义的弹窗动画
  popWindow.setAnimationStyle(R.style.AnimBottom);
  popWindow.setFocusable(true);
  popWindow.setOutsideTouchable(true);// 设置允许在外点击消失

  OnClickListener listener = new OnClickListener() {
   public void onClick(View v) {
    switch (v.getId()) {
    case R.id.Send:
     
     break;
    
    }
    popWindow.dismiss();
   }
  };

  //btnCamera.setOnClickListener(listener);
  
       //设置他的背景颜色
  ColorDrawable dw = new ColorDrawable(0x30000000);
  popWindow.setBackgroundDrawable(dw);
  popWindow.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
 }

}

demo下载地址: http://download.csdn.net/detail/ningzhouxu/9532439

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值