之前准备了很多的东西存储一些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出现了.