PopupWindow的最基本使用

之前准备了很多的东西存储一些Android开发的技术,用来在自己需要的时候直接查看,但是因为电脑系统有问题,部分文件丢失,U盘也因为要做系统盘导致我对这种离线储存方式好感不剩多少了,准备自己开一个博客,没事就添加一些基础技术代码,等哪天要用的时候查起来也方便.  这是第一篇博客


鉴于本人也不喜欢看那种没图的博客,所以先上效果图:


先是布局文件,MainActivity里的


<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.littlespider.animationview.MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:text="Hello World!"/>
</RelativeLayout>

很简单,在RelativeLayout里面放了一个id为text的TextView.  然后是popupWindow的布局:
<?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">
    <ImageView
        android:background="#fff000"
        android:layout_width="match_parent"
        android:layout_height="100dp"/>
</RelativeLayout>

然后核心的java代码来了:
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity {

    PopupWindow mPopupWindow; //全局的popupWindow对象
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text);
        //给textView添加点击监听
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View contentView = View.inflate(MainActivity.this,R.layout.layout_popwindow,null);
                mPopupWindow = new PopupWindow(contentView, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT,true);
                mPopupWindow.showAsDropDown(mTextView);
            }
        });
    }
}

 然后一点击textView就有一个黄黄的popupWindow出现了.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值