popupwindow

layout    popuwindow.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >




    <TextView

        android:id="@+id/groupname"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignBaseline="@+id/group_name"

        android:layout_alignBottom="@+id/group_name"

        android:layout_alignLeft="@+id/addgroup"

        android:text="分组名称:"

        android:textSize="17sp" />




    <ImageView

        android:id="@+id/imageView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_below="@+id/addgroup"

        android:layout_marginTop="15dp"

        android:src="@drawable/baseline" />




    <TextView

        android:id="@+id/addgroup"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_marginLeft="18dp"

        android:layout_toRightOf="@+id/tableRow1"

        android:text="添加分组"

        android:textSize="25sp" />




    <EditText

        android:id="@+id/group_name"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentRight="true"

        android:layout_below="@+id/imageView1"

        android:layout_marginRight="16dp"

        android:layout_marginTop="24dp"

        android:layout_toRightOf="@+id/textView2"

        android:layout_weight="1"

        android:ems="10"

        android:hint="@string/groupHint"

        android:maxLength="10" />




    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/textView2"

        android:layout_alignRight="@+id/group_name"

        android:layout_below="@+id/group_name"

        android:layout_marginTop="28dp"

        android:gravity="center_horizontal" >




        <Button

            android:id="@+id/ensure"

            android:layout_width="142dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="确定" />




        <Button

            android:id="@+id/cancel"

            android:layout_width="142dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="取消" />

    </LinearLayout>



</RelativeLayout> 


drawable popuwindow_style
<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    

    <!-- solid 设置stroke设置的边框以内的颜色 -->

    <solid android:color="#ffffff"/>

    <!-- stroke主要设置组件的边框。width为边框宽度,color为边框颜色 -->

    <stroke android:width="2dp" android:color="#c5c1c1" />

    <!-- corners 设置边框四角弧度 -->

    <corners android:radius="10dp"/>

    <!-- padding主要设置组件里内容距离组件内边框的间距 -->

    <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp"/>

    

</shape>


.java
private class addGroupListerner implements OnClickListener {




@SuppressLint("NewApi")

@Override

public void onClick(View v) {



getPopupWindow();

PopupWindow.showAsDropDown(v);

}

}




private void getPopupWindow() {

if (null != PopupWindow) {

PopupWindow.dismiss();

return;

} else {

initPopupWindow();

}

}




private void initPopupWindow() {

// 得到屏幕的宽度和高度

screenWidth = this.getWindowManager().getDefaultDisplay().getWidth();

screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();

// 获取自定义布局文件popupwindow.xml

View popupWindow_view = getLayoutInflater().inflate(

R.layout.popupwindow, null, false);

// 设置其背景

popupWindow_view.setBackgroundResource(R.drawable.popupwindow_style);

// 创建popupWindow实例

PopupWindow = new PopupWindow(popupWindow_view, screenWidth-40,

screenHeight/3-20, true);

// 获取视图中的按钮与编辑框

TextAddName = (TextView)popupWindow_view.findViewById(R.id.addgroup);

TextGroupName = (TextView)popupWindow_view.findViewById(R.id.groupname);

button_ensure = (Button) popupWindow_view

.findViewById(R.id.ensure);

button_cancel = (Button) popupWindow_view

.findViewById(R.id.cancel);

group_name = (EditText) popupWindow_view

.findViewById(R.id.group_name);

button_ensure.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// 设置标签中的内容

contextgroupName = group_name.getText().toString().trim();// 取得分组名字




Grouplistadapter.addListItem(contextgroupName);

PopupWindow.dismiss();

}

});

button_cancel.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {




PopupWindow.dismiss();

}

});



} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
嗨!对于你关于 Android PopupWindow 的问题,我可以给你一些基本的信息。PopupWindowAndroid 中的一个弹出式窗口,它可以覆盖在其他视图之上,提供一种临时性的用户界面。你可以使用 PopupWindow 来显示额外的内容、菜单或者交互提示。 要创建一个 PopupWindow,首先你需要实例化一个 PopupWindow 对象,并为其设置一些基本属性,例如宽度、高度、背景等。然后,你可以将任何视图或布局添加到 PopupWindow 中,并通过设置位置参数来控制它的显示位置。 下面是一个简单的示例代码,展示如何创建和显示一个 PopupWindow: ```java // 创建一个 PopupWindow 对象 PopupWindow popupWindow = new PopupWindow(context); // 设置 PopupWindow 的宽度和高度 popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); // 设置 PopupWindow 的内容视图 View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); // 设置 PopupWindow 的背景 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 显示 PopupWindow popupWindow.showAtLocation(anchorView, Gravity.CENTER, 0, 0); ``` 在上面的示例中,我们创建了一个 PopupWindow 对象,并设置了宽度和高度为包裹内容。然后,我们通过调用 `setContentView` 方法将一个自定义的布局文件 `R.layout.popup_layout` 添加到 PopupWindow 中。最后,我们使用 `showAtLocation` 方法将 PopupWindow 显示在屏幕中央。 希望这些信息对你有帮助!如果你对 PopupWindow 有更多的问题,或者需要更详细的示例代码,请随时告诉我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值