安卓中PopupWindows的使用

PopupWindows使用起来特别简单,就不再给大家分析了,直接上代码了

功能代码

public class PopupWindows extends ActionBarActivity {

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


   public void popupwindows(View v){
       //实例化popupwindos中的子布局文件
       View view = getLayoutInflater().inflate(R.layout.popupwindows_layout,null);

       //给弹窗中的按钮设置点击事件
       Button popupButton = (Button) view.findViewById(R.id.popup_btn);
       popupButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Toast.makeText(PopupWindows.this,"哦,吼吼,它被点击啦...",Toast.LENGTH_SHORT).show();
           }
       });

       //popupwindows中的view对象需要自定义xml文件
       PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
                                                        ViewGroup.LayoutParams.MATCH_PARENT); //设置弹窗的宽高度
//       PopupWindow popupWindow = new PopupWindow(view, 200,200);    //设置弹窗的自定义宽高度



       //为方便测试,popupwindows中使用的资源引用安卓系统自带的资源
       popupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_star_big_on));   //设置背景图片
       popupWindow.setAnimationStyle(android.R.style.Animation_Translucent);    //设置弹出的动画效果(可以自定义效果)
       popupWindow.getBackground().setAlpha(100);   //设置透明度(0-255之间)
       popupWindow.setOutsideTouchable(true);       //设置点击边缘区域windows消失
       popupWindow.setFocusable(true);
       popupWindow.setTouchable(true);
       popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);    //设置软键盘的弹出自适应


       popupWindow.showAtLocation(v, Gravity.BOTTOM,0,0);   //设置popupWindows的显示位置,0,0代表偏移量


       //获取屏幕尺寸
       DisplayMetrics dm =new DisplayMetrics();
       getWindowManager().getDefaultDisplay().getMetrics(dm);

       //获取宽高
       int width = dm.widthPixels;
       int height = dm.heightPixels;
//
//       //第二种方式,获取当前activity的宽高度,如果当前的activity不是全屏显示,获取的数据可能会有误差
//      int width =  getWindowManager().getDefaultDisplay().getWidth();
//      int height = getWindowManager().getDefaultDisplay().getHeight();
   }
}

布局文件需要单独拿出来说一下,因为popupWindows弹出的窗口是一个独立的layout布局文件,所以需要使用 

View view = getLayoutInflater().inflate(R.layout.popupwindows_layout,null);

实例化一个View对象填充popupWindows

布局分两部分,一个主activity的布局简单的一个按钮

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.zhuandian.msuic.PopupWindows">
<Button
    android:onClick="popupwindows"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="测试弹窗"
    />
</RelativeLayout>


popupWIndows的布局文件,里面的内容可根据需要自己定义,当然对里面控件的操作也可以根据需求具体自己实现

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/a" />

    <Button
        android:id="@+id/popup_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:text="点击"/>
</FrameLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 Android 使用 `ShapeCheckBox` 需要进行以下步骤: 1. 在 `res/drawable` 目录下创建一个 XML 文件,例如 `shape_checkbox.xml`,并添加以下代码: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/checkbox_checked" android:state_checked="true"/> <item android:drawable="@drawable/checkbox_unchecked"/> </selector> ``` 在上面的代码,我们使用了 `selector` 标签来定义状态列表,当 `CheckBox` 处于选状态时,显示 `checkbox_checked` 的图片,否则显示 `checkbox_unchecked` 的图片。 2. 在 `res/drawable` 目录下创建两个 XML 文件,分别为 `checkbox_checked.xml` 和 `checkbox_unchecked.xml`,并添加以下代码: checkbox_checked.xml: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FF4081"/> <size android:width="20dp" android:height="20dp"/> </shape> ``` checkbox_unchecked.xml: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:color="#9E9E9E" android:width="2dp"/> <size android:width="20dp" android:height="20dp"/> </shape> ``` 在上面的代码,我们使用了 `shape` 标签来定义形状,当 `CheckBox` 处于选状态时,显示一个红色的矩形,否则显示一个带有灰色边框的矩形。 3. 在布局文件使用 `ShapeCheckBox`,例如: ```xml <com.example.ShapeCheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/shape_checkbox"/> ``` 在上面的代码,我们使用了 `ShapeCheckBox` 控件,并设置了 `button` 属性为我们刚才创建的 `shape_checkbox.xml` 文件。 至此,我们就使用 `ShapeCheckBox` 成功地创建了一个自定义的 `CheckBox`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxpr_ybgg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值